Class: ScopedSerializer::Scope
- Inherits:
-
Object
- Object
- ScopedSerializer::Scope
- Defined in:
- lib/scoped_serializer/scope.rb
Constant Summary collapse
- METHODS =
[:root, :attributes, :association, :belongs_to, :has_one, :has_many]
Instance Attribute Summary collapse
-
#associations ⇒ Object
Returns the value of attribute associations.
-
#attributes(*attrs) ⇒ Object
Defines attributes.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#_association(args, default_options = {}) ⇒ Object
Actually defines the association but without default_options.
-
#association(*args) ⇒ Object
(also: #belongs_to, #has_one, #has_many)
Defines an association.
-
#dup ⇒ Object
Duplicates scope.
-
#initialize(name, default = nil, &block) ⇒ Scope
constructor
A new instance of Scope.
-
#merge!(scope) ⇒ Object
Merges data with given scope.
-
#root(key) ⇒ Object
Defines the root key.
Constructor Details
#initialize(name, default = nil, &block) ⇒ Scope
Returns a new instance of Scope.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/scoped_serializer/scope.rb', line 8 def initialize(name, default=nil, &block) @name = name @options = {} @attributes = [] @associations = {} # Merge defaults merge!(default) if default self.instance_eval &block if block_given? end |
Instance Attribute Details
#associations ⇒ Object
Returns the value of attribute associations.
6 7 8 |
# File 'lib/scoped_serializer/scope.rb', line 6 def associations @associations end |
#attributes(*attrs) ⇒ Object
Defines attributes.
57 58 59 |
# File 'lib/scoped_serializer/scope.rb', line 57 def attributes @attributes end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/scoped_serializer/scope.rb', line 6 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/scoped_serializer/scope.rb', line 6 def @options end |
Instance Method Details
#_association(args, default_options = {}) ⇒ Object
Actually defines the association but without default_options.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/scoped_serializer/scope.rb', line 93 def _association(args, ={}) return if .nil? = args.first if .is_a?(Hash) = {}.merge() name = .keys.first properties = .delete(name) @associations[name] = .merge({ :include => properties }).merge() elsif .is_a?(Array) .each do |option| association option end else @associations[] = args[1] || {} end end |
#association(*args) ⇒ Object Also known as: belongs_to, has_one, has_many
Defines an association.
75 76 77 |
# File 'lib/scoped_serializer/scope.rb', line 75 def association(*args) _association(args, { :preload => true }) end |
#dup ⇒ Object
Duplicates scope.
85 86 87 88 |
# File 'lib/scoped_serializer/scope.rb', line 85 def dup clone = Scope.new(name) clone.merge!(self) end |
#merge!(scope) ⇒ Object
Merges data with given scope.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/scoped_serializer/scope.rb', line 26 def merge!(scope) @options.merge!(scope.) @attributes += scope.attributes @associations.merge!(scope.associations) @attributes.uniq! self end |
#root(key) ⇒ Object
Defines the root key.
45 46 47 |
# File 'lib/scoped_serializer/scope.rb', line 45 def root(key) @options.merge!({ :root => key }) end |