Class: Rubord::Collection
- Inherits:
-
Object
- Object
- Rubord::Collection
- Includes:
- Enumerable
- Defined in:
- lib/rubord/models/collection.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete(key) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #filter(&block) ⇒ Object
- #find(&block) ⇒ Object
- #get(key) ⇒ Object
-
#initialize ⇒ Collection
constructor
A new instance of Collection.
- #keys ⇒ Object
- #map(&block) ⇒ Object
- #set(key, value) ⇒ Object
- #size ⇒ Object
- #to_h ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize ⇒ Collection
Returns a new instance of Collection.
5 6 7 |
# File 'lib/rubord/models/collection.rb', line 5 def initialize @store = {} end |
Instance Method Details
#[](key) ⇒ Object
9 10 11 |
# File 'lib/rubord/models/collection.rb', line 9 def [](key) @store[key] end |
#[]=(key, value) ⇒ Object
13 14 15 |
# File 'lib/rubord/models/collection.rb', line 13 def []=(key, value) @store[key] = value end |
#delete(key) ⇒ Object
26 27 28 |
# File 'lib/rubord/models/collection.rb', line 26 def delete(key) @store.delete(key) end |
#each(&block) ⇒ Object
30 31 32 |
# File 'lib/rubord/models/collection.rb', line 30 def each(&block) @store.each_value(&block) end |
#empty? ⇒ Boolean
62 63 64 |
# File 'lib/rubord/models/collection.rb', line 62 def empty? @store.empty? end |
#filter(&block) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/rubord/models/collection.rb', line 38 def filter(&block) self.class.new.tap do |col| @store.each do |k, v| col.set(k, v) if block.call(v) end end end |
#find(&block) ⇒ Object
46 47 48 |
# File 'lib/rubord/models/collection.rb', line 46 def find(&block) @store.values.find(&block) end |
#get(key) ⇒ Object
17 18 19 |
# File 'lib/rubord/models/collection.rb', line 17 def get(key) @store[key] end |
#keys ⇒ Object
54 55 56 |
# File 'lib/rubord/models/collection.rb', line 54 def keys @store.keys end |
#map(&block) ⇒ Object
34 35 36 |
# File 'lib/rubord/models/collection.rb', line 34 def map(&block) @store.values.map(&block) end |
#set(key, value) ⇒ Object
21 22 23 24 |
# File 'lib/rubord/models/collection.rb', line 21 def set(key, value) @store[key] = value self end |
#size ⇒ Object
58 59 60 |
# File 'lib/rubord/models/collection.rb', line 58 def size @store.size end |
#to_h ⇒ Object
66 67 68 |
# File 'lib/rubord/models/collection.rb', line 66 def to_h @store.dup end |
#values ⇒ Object
50 51 52 |
# File 'lib/rubord/models/collection.rb', line 50 def values @store.values end |