Class: Netfira::WebConnect::HashOfModels
- Inherits:
-
Hash
- Object
- Hash
- Netfira::WebConnect::HashOfModels
show all
- Defined in:
- lib/netfira/web_connect/components/hash_of_models.rb
Instance Method Summary
collapse
Constructor Details
Returns a new instance of HashOfModels.
4
5
6
|
# File 'lib/netfira/web_connect/components/hash_of_models.rb', line 4
def initialize
super { |hash, key| hash[key] = [] }
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
17
18
19
20
21
22
23
24
|
# File 'lib/netfira/web_connect/components/hash_of_models.rb', line 17
def method_missing(sym, *args, &block)
if respond_to_missing? sym
klass = class_for_key(sym)
-> { self[klass] }.tap { |method| self.class.__send__ :define_method, sym, method }.call
else
super sym, *args, &block
end
end
|
Instance Method Details
#<<(value) ⇒ Object
12
13
14
15
|
# File 'lib/netfira/web_connect/components/hash_of_models.rb', line 12
def <<(value)
raise 'Only models are acceptable' unless Model === value
self[value.class] << value
end
|
#[](key) ⇒ Object
8
9
10
|
# File 'lib/netfira/web_connect/components/hash_of_models.rb', line 8
def [](key)
super class_for_key(key)
end
|
#as_json(options = nil) ⇒ Object
30
31
32
33
34
35
36
37
38
|
# File 'lib/netfira/web_connect/components/hash_of_models.rb', line 30
def as_json(options = nil)
map do |klass, records|
is_relation = klass < Model::Relation
[
klass.name.demodulize.to_sym,
records.map { |record| is_relation ? record.records.map(&:origin_id) : record.origin_id }
]
end.to_h
end
|
#respond_to_missing?(sym) ⇒ Boolean
26
27
28
|
# File 'lib/netfira/web_connect/components/hash_of_models.rb', line 26
def respond_to_missing?(sym)
sym !~ /[^\w]/ && !!class_for_key(sym)
end
|