Class: Hashme::CastedArray
- Inherits:
-
Array
- Object
- Array
- Hashme::CastedArray
- Defined in:
- lib/hashme/casted_array.rb
Overview
The Hashme CastedArray is a special Array that typecasts each item according to a given property
Instance Attribute Summary collapse
-
#property ⇒ Object
readonly
Returns the value of attribute property.
Instance Method Summary collapse
- #<<(val) ⇒ Object
- #[]=(*args) ⇒ Object
- #collect!(&block) ⇒ Object (also: #map!)
- #concat(*arrays) ⇒ Object
- #fill(*args, &block) ⇒ Object
-
#initialize(property, vals = []) ⇒ CastedArray
constructor
A new instance of CastedArray.
- #insert(index, *vals) ⇒ Object
- #push(*vals) ⇒ Object (also: #append)
- #replace(array) ⇒ Object
- #unshift(*vals) ⇒ Object (also: #prepend)
Constructor Details
#initialize(property, vals = []) ⇒ CastedArray
Returns a new instance of CastedArray.
9 10 11 12 |
# File 'lib/hashme/casted_array.rb', line 9 def initialize(property, vals = []) @property = property super build_all(vals) end |
Instance Attribute Details
#property ⇒ Object (readonly)
Returns the value of attribute property.
7 8 9 |
# File 'lib/hashme/casted_array.rb', line 7 def property @property end |
Instance Method Details
#<<(val) ⇒ Object
14 15 16 |
# File 'lib/hashme/casted_array.rb', line 14 def <<(val) super build(val) end |
#[]=(*args) ⇒ Object
42 43 44 45 46 |
# File 'lib/hashme/casted_array.rb', line 42 def []=(*args) args = args.dup args[-1] = build(args[-1]) super *args end |
#collect!(&block) ⇒ Object Also known as: map!
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/hashme/casted_array.rb', line 61 def collect!(&block) if block super do |element| val = block.call(element) build(val) end else super end end |
#concat(*arrays) ⇒ Object
24 25 26 |
# File 'lib/hashme/casted_array.rb', line 24 def concat(*arrays) super *arrays.map { |array| build_all(array) } end |
#fill(*args, &block) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/hashme/casted_array.rb', line 48 def fill(*args, &block) if block super *args do |index| val = block.call(index) build(val) end else args = args.dup args[0] = build(args[0]) super *args end end |
#insert(index, *vals) ⇒ Object
28 29 30 |
# File 'lib/hashme/casted_array.rb', line 28 def insert(index, *vals) super index, *build_all(vals) end |
#push(*vals) ⇒ Object Also known as: append
18 19 20 |
# File 'lib/hashme/casted_array.rb', line 18 def push(*vals) super *build_all(vals) end |
#replace(array) ⇒ Object
38 39 40 |
# File 'lib/hashme/casted_array.rb', line 38 def replace(array) super build_all(array) end |
#unshift(*vals) ⇒ Object Also known as: prepend
32 33 34 |
# File 'lib/hashme/casted_array.rb', line 32 def unshift(*vals) super *build_all(vals) end |