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.
Class Method Summary collapse
-
.from_hash(data = {}) ⇒ Object
Initializes a scope from hash.
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.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/scoped_serializer/scope.rb', line 29 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.
78 79 80 |
# File 'lib/scoped_serializer/scope.rb', line 78 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 |
Class Method Details
.from_hash(data = {}) ⇒ Object
Initializes a scope from hash.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/scoped_serializer/scope.rb', line 16 def from_hash(data={}) scope = new self scope.attributes *data[:attributes] (data[:associations] || []).each do |association| scope.association association end scope end |
Instance Method Details
#_association(args, default_options = {}) ⇒ Object
Actually defines the association but without default_options.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/scoped_serializer/scope.rb', line 114 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.
96 97 98 |
# File 'lib/scoped_serializer/scope.rb', line 96 def association(*args) _association(args, { :preload => true }) end |
#dup ⇒ Object
Duplicates scope.
106 107 108 109 |
# File 'lib/scoped_serializer/scope.rb', line 106 def dup clone = Scope.new(name) clone.merge!(self) end |
#merge!(scope) ⇒ Object
Merges data with given scope.
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/scoped_serializer/scope.rb', line 47 def merge!(scope) @options.merge!(scope.) @attributes += scope.attributes @associations.merge!(scope.associations) @attributes.uniq! self end |
#root(key) ⇒ Object
Defines the root key.
66 67 68 |
# File 'lib/scoped_serializer/scope.rb', line 66 def root(key) @options.merge!({ :root => key }) end |