Class: Transcriber::Resource::Association
- Inherits:
-
Key
- Object
- Key
- Transcriber::Resource::Association
show all
- Defined in:
- lib/transcriber/resource/key/association.rb
Instance Attribute Summary collapse
Attributes inherited from Key
#model, #name, #options, #summarize
Instance Method Summary
collapse
Methods inherited from Key
#convert_input_keys, #input_path, #present?, #root_path?, #visible?
Constructor Details
#initialize(name, options = {}) ⇒ Association
Returns a new instance of Association.
6
7
8
9
10
|
# File 'lib/transcriber/resource/key/association.rb', line 6
def initialize(name, options = {})
super
@many = options.fetch(:many, false)
@class_name = options.fetch(:class_name, default_class_name).to_s.camelize
end
|
Instance Attribute Details
#many ⇒ Object
Returns the value of attribute many.
4
5
6
|
# File 'lib/transcriber/resource/key/association.rb', line 4
def many
@many
end
|
Instance Method Details
#create_from_hash(attrs) ⇒ Object
33
34
35
36
|
# File 'lib/transcriber/resource/key/association.rb', line 33
def create_from_hash(attrs)
one? ? resource_class.new(attrs)
: Array(attrs).map {|item| resource_class.new(item)}
end
|
#default_class_name ⇒ Object
20
21
22
|
# File 'lib/transcriber/resource/key/association.rb', line 20
def default_class_name
one? ? name : name.to_s.singularize
end
|
#from_hash(attrs, resource = nil) ⇒ Object
28
29
30
31
|
# File 'lib/transcriber/resource/key/association.rb', line 28
def from_hash(attrs, resource = nil)
return nil if attrs.nil? or attrs.empty?
resource ? update_from_hash(resource, attrs) : create_from_hash(attrs)
end
|
16
17
18
|
# File 'lib/transcriber/resource/key/association.rb', line 16
def many?
@many
end
|
12
13
14
|
# File 'lib/transcriber/resource/key/association.rb', line 12
def one?
!many?
end
|
#resource_class ⇒ Object
24
25
26
|
# File 'lib/transcriber/resource/key/association.rb', line 24
def resource_class
@class_name.to_s.camelize.constantize
end
|
#update_from_hash(resource, attrs) ⇒ Object
38
39
40
41
|
# File 'lib/transcriber/resource/key/association.rb', line 38
def update_from_hash(resource, attrs)
one? ? resource.update_attributes(attrs)
: resource.map {|item| item.update_attributes(item)}
end
|