Class: RestModel::Association
- Defined in:
- lib/rest_model/key/association.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#many ⇒ Object
readonly
Returns the value of attribute many.
Attributes inherited from Key
#model, #name, #options, #summarize
Instance Method Summary collapse
- #default_class_name ⇒ Object
- #from_hash(attrs, resource = nil) ⇒ Object
-
#initialize(name, options = {}) ⇒ Association
constructor
A new instance of Association.
- #many? ⇒ Boolean
- #one? ⇒ Boolean
- #resource_class ⇒ Object
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, = {}) super @many = .fetch(:many, false) @class_name = .fetch(:class_name, default_class_name).to_s.camelize end |
Instance Attribute Details
#many ⇒ Object (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_name ⇒ Object
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 |
#resource_class ⇒ Object
23 24 25 |
# File 'lib/rest_model/key/association.rb', line 23 def resource_class @class_name.to_s.camelize.constantize end |