Class: RestModel::Association

Inherits:
Key
  • Object
show all
Defined in:
lib/rest_model/key/association.rb

Direct Known Subclasses

Embeddable, Relation

Instance Attribute Summary collapse

Attributes inherited from Key

#model, #name, #options, #summarize

Instance Method Summary collapse

Methods inherited from Key

#convert_input_keys, #present?, #root_path?, #source_path, #visible?

Constructor Details

#initialize(name, options = {}) ⇒ Association

Returns a new instance of Association.



5
6
7
8
9
# File 'lib/rest_model/key/association.rb', line 5

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

#manyObject (readonly)

Returns the value of attribute many.



3
4
5
# File 'lib/rest_model/key/association.rb', line 3

def many
  @many
end

Instance Method Details

#default_class_nameObject



19
20
21
# File 'lib/rest_model/key/association.rb', line 19

def default_class_name
  one? ? name : name.to_s.singularize
end

#from_hash(attrs, resource = nil) ⇒ Object



27
28
29
30
31
32
# File 'lib/rest_model/key/association.rb', line 27

def from_hash(attrs, resource = nil)
  return nil if attrs.nil? or attrs.empty?

  one? ? resource_class.new(attrs)
       : Array(attrs).map {|item| resource_class.new(item)}
end

#many?Boolean

Returns:



15
16
17
# File 'lib/rest_model/key/association.rb', line 15

def many?
  @many
end

#one?Boolean

Returns:



11
12
13
# File 'lib/rest_model/key/association.rb', line 11

def one?
  !many?
end

#resource_classObject



23
24
25
# File 'lib/rest_model/key/association.rb', line 23

def resource_class
  @class_name.to_s.camelize.constantize
end