Class: Wallaby::ClassArray
- Inherits:
-
Object
- Object
- Wallaby::ClassArray
- Includes:
- Classifier
- Defined in:
- lib/wallaby/class_array.rb
Overview
This is a constant-safe array that stores Class value as String.
Instance Attribute Summary collapse
-
#internal ⇒ Array
readonly
The array to store Class values as String.
-
#origin ⇒ Array
readonly
The original array.
Instance Method Summary collapse
-
#<<(item) ⇒ ClassArray
Self.
-
#==(other) ⇒ Object
Compare ##origin with other.
-
#[](index) ⇒ Object
Return the value for the given index.
-
#[]=(index, value) ⇒ Object
Save the value to the #internal array at the given index, and convert the Class value to String.
- #blank? ⇒ Object
-
#concat(other) ⇒ ClassArray
New Class array.
-
#each(&block) ⇒ ClassArray
Self.
- #each_with_object(object) ⇒ Object
-
#freeze ⇒ ClassArray
Ensure to freeze the #internal.
-
#initialize(*array) ⇒ ClassArray
constructor
A new instance of ClassArray.
-
#to_a ⇒ Object
Get the array of ##origin.
- #to_sentence ⇒ Object
Methods included from Classifier
Constructor Details
#initialize(*array) ⇒ ClassArray
Returns a new instance of ClassArray.
9 10 11 12 13 14 |
# File 'lib/wallaby/class_array.rb', line 9 def initialize(*array) @internal = (array || []).flatten return if @internal.blank? @internal.map! { |klass| class_name_of(klass) }.compact! end |
Instance Attribute Details
#internal ⇒ Array (readonly)
Returns The array to store Class values as String.
18 19 20 |
# File 'lib/wallaby/class_array.rb', line 18 def internal @internal end |
#origin ⇒ Array (readonly)
Returns The original array.
22 23 24 25 |
# File 'lib/wallaby/class_array.rb', line 22 def origin # NOTE: DO NOT cache it using instance variable! @internal.map { |klass| to_class(klass) }.compact end |
Instance Method Details
#<<(item) ⇒ ClassArray
Returns self.
45 46 47 48 |
# File 'lib/wallaby/class_array.rb', line 45 def <<(item) @internal << class_name_of(item) self end |
#==(other) ⇒ Object
Compare ##origin with other.
58 |
# File 'lib/wallaby/class_array.rb', line 58 delegate :==, to: :origin |
#[](index) ⇒ Object
Return the value for the given index
33 34 35 |
# File 'lib/wallaby/class_array.rb', line 33 def [](index) to_class @internal[index] end |
#[]=(index, value) ⇒ Object
Save the value to the #internal array at the given index, and convert the Class value to String
28 29 30 |
# File 'lib/wallaby/class_array.rb', line 28 def []=(index, value) @internal[index] = class_name_of value end |
#blank? ⇒ Object
65 |
# File 'lib/wallaby/class_array.rb', line 65 delegate :blank?, to: :internal |
#concat(other) ⇒ ClassArray
Returns new Class array.
39 40 41 |
# File 'lib/wallaby/class_array.rb', line 39 def concat(other) self.class.new origin.concat(other.try(:origin) || other) end |
#each(&block) ⇒ ClassArray
Returns self.
51 52 53 54 |
# File 'lib/wallaby/class_array.rb', line 51 def each(&block) origin.each(&block) self end |
#each_with_object(object) ⇒ Object
68 |
# File 'lib/wallaby/class_array.rb', line 68 delegate :each_with_object, to: :origin |
#freeze ⇒ ClassArray
Ensure to freeze the #internal
75 76 77 78 |
# File 'lib/wallaby/class_array.rb', line 75 def freeze @internal.freeze super end |
#to_a ⇒ Object
Get the array of ##origin.
62 |
# File 'lib/wallaby/class_array.rb', line 62 delegate :to_a, to: :origin |
#to_sentence ⇒ Object
71 |
# File 'lib/wallaby/class_array.rb', line 71 delegate :to_sentence, to: :origin |