Class: Kilt::ObjectCollection
- Inherits:
-
Object
- Object
- Kilt::ObjectCollection
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/kilt/object_collection.rb
Instance Attribute Summary collapse
-
#values ⇒ Object
Returns the value of attribute values.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #empty? ⇒ Boolean
- #group(key) ⇒ Object
-
#initialize(values = []) ⇒ ObjectCollection
constructor
A new instance of ObjectCollection.
- #order(key = 'name', direction = 'ASC') ⇒ Object
Constructor Details
#initialize(values = []) ⇒ ObjectCollection
Returns a new instance of ObjectCollection.
11 12 13 |
# File 'lib/kilt/object_collection.rb', line 11 def initialize(values = []) @values = values end |
Instance Attribute Details
#values ⇒ Object
Returns the value of attribute values.
7 8 9 |
# File 'lib/kilt/object_collection.rb', line 7 def values @values end |
Instance Method Details
#[](key) ⇒ Object
15 16 17 |
# File 'lib/kilt/object_collection.rb', line 15 def [](key) @values[key] end |
#empty? ⇒ Boolean
53 54 55 |
# File 'lib/kilt/object_collection.rb', line 53 def empty? @values.empty? end |
#group(key) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/kilt/object_collection.rb', line 29 def group(key) # create an empty hash ret = {} # loop through all items in @values @values.each do |object| # for each item, check to see if the hash already has a value for this key if ret[object[key]] # if it does, add it to the value, which should be an array a = ret[object[key]] a << object else # if it doesn't, create an array first, add this loop item to it, and add the # array as the value for that hash key a = [] a << object ret[object[key]] = a end end # return the hash ret end |
#order(key = 'name', direction = 'ASC') ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/kilt/object_collection.rb', line 19 def order(key = 'name', direction = 'ASC') @values = @values.sort_by { |hash| is_int?(hash[key]) ? hash[key].to_i : hash[key] } if direction == 'DESC' @values.reverse! end return self end |