class ScopedSerializer::Scope
Constants
- METHODS
Attributes
associations[RW]
attributes[RW]
name[RW]
options[RW]
Public Class Methods
from_hash(data={})
click to toggle source
Initializes a scope from hash.
@example
Scope.from_hash({ :attributes => [:title, :created_at] })
# 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
new(name, default=nil, &block)
click to toggle source
# 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
Public Instance Methods
_association(args, default_options={})
click to toggle source
Actually defines the association but without default_options.
# File lib/scoped_serializer/scope.rb, line 114 def _association(args, default_options={}) return if options.nil? options = args.first if options.is_a?(Hash) options = {}.merge(options) name = options.keys.first properties = options.delete(name) @associations[name] = default_options.merge({ :include => properties }).merge(options) elsif options.is_a?(Array) options.each do |option| association option end else @associations[options] = args[1] || {} end end
association(*args)
click to toggle source
Defines an association.
@example
scope :collection association :customer association :posts => :user, :serializer => UserPostSerializer, :root => :user_posts end
# File lib/scoped_serializer/scope.rb, line 96 def association(*args) _association(args, { :preload => true }) end
dup()
click to toggle source
Duplicates scope.
# File lib/scoped_serializer/scope.rb, line 106 def dup clone = Scope.new(name) clone.merge!(self) end
merge!(scope)
click to toggle source
Merges data with given scope.
@example
scope.merge!(another_scope)
# File lib/scoped_serializer/scope.rb, line 47 def merge!(scope) @options.merge!(scope.options) @attributes += scope.attributes @associations.merge!(scope.associations) @attributes.uniq! self end
root(key)
click to toggle source
Defines the root key.
@example
scope :collection root :reservations end
# File lib/scoped_serializer/scope.rb, line 66 def root(key) @options.merge!({ :root => key }) end