Class: Infuser::Collections::Proxy
- Inherits:
-
Object
- Object
- Infuser::Collections::Proxy
- Includes:
- Enumerable
- Defined in:
- lib/infuser/collections/proxy.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
- #<<(item) ⇒ Object
- #can_add_more? ⇒ Boolean
- #data ⇒ Object
- #each(&block) ⇒ Object
- #find(id) ⇒ Object
- #find_or_create(id) ⇒ Object
-
#initialize(klass) ⇒ Proxy
constructor
A new instance of Proxy.
- #parse(dump) ⇒ Object
- #remove(item) ⇒ Object
- #schema ⇒ Object
- #set ⇒ Object
Constructor Details
#initialize(klass) ⇒ Proxy
Returns a new instance of Proxy.
11 12 13 |
# File 'lib/infuser/collections/proxy.rb', line 11 def initialize klass @klass = Infuser.const_get(klass.to_s.classify) end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
9 10 11 |
# File 'lib/infuser/collections/proxy.rb', line 9 def klass @klass end |
Instance Method Details
#<<(item) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/infuser/collections/proxy.rb', line 39 def << item if set.size == klass.mappings.keys.max raise(Infuser::ArgumentError, "The collection is full, you can not add another item.") elsif item.class.name != klass.name raise(Infuser::ArgumentError, "The item you are adding does not belong in this collection.") else item.id = ( (1..klass.mappings.keys.max).to_a - set.map(&:id) ).sort.first set << item end end |
#can_add_more? ⇒ Boolean
35 36 37 |
# File 'lib/infuser/collections/proxy.rb', line 35 def can_add_more? set.size < klass.mappings.keys.max end |
#data ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/infuser/collections/proxy.rb', line 66 def data set.each_with_object({}) do |item, hash| item.data.each do |key, value| hash[key] = value end end end |
#each(&block) ⇒ Object
50 51 52 |
# File 'lib/infuser/collections/proxy.rb', line 50 def each &block set.each(&block) end |
#find(id) ⇒ Object
54 55 56 |
# File 'lib/infuser/collections/proxy.rb', line 54 def find id set.find { |item| item.id == id } end |
#find_or_create(id) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/infuser/collections/proxy.rb', line 58 def find_or_create id find(id) || begin item = klass.new(id: id) set << item item end end |
#parse(dump) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/infuser/collections/proxy.rb', line 15 def parse dump dump.each do |key, value| if id = klass.inverted_mappings[key.underscore.to_sym] find_or_create(id).send("#{key.underscore}=", value) end end end |
#remove(item) ⇒ Object
31 32 33 |
# File 'lib/infuser/collections/proxy.rb', line 31 def remove item set.delete item end |
#schema ⇒ Object
23 24 25 |
# File 'lib/infuser/collections/proxy.rb', line 23 def schema klass.schema end |
#set ⇒ Object
27 28 29 |
# File 'lib/infuser/collections/proxy.rb', line 27 def set @set ||= [] end |