Class: Infuser::Collections::Proxy

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/infuser/collections/proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#klassObject (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

Returns:



35
36
37
# File 'lib/infuser/collections/proxy.rb', line 35

def can_add_more?
  set.size < klass.mappings.keys.max
end

#dataObject



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

#schemaObject



23
24
25
# File 'lib/infuser/collections/proxy.rb', line 23

def schema
  klass.schema
end

#setObject



27
28
29
# File 'lib/infuser/collections/proxy.rb', line 27

def set
  @set ||= []
end