Class: ScopedSerializer::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/scoped_serializer/scope.rb

Constant Summary collapse

METHODS =
[:root, :attributes, :association, :belongs_to, :has_one, :has_many]

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#associationsObject

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.

Examples:

scope :collection
  attributes :status
end


57
58
59
# File 'lib/scoped_serializer/scope.rb', line 57

def attributes
  @attributes
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/scoped_serializer/scope.rb', line 6

def name
  @name
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/scoped_serializer/scope.rb', line 6

def options
  @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, 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) ⇒ Object Also known as: belongs_to, has_one, has_many

Defines an association.

Examples:

scope :collection
  association :customer
  association :posts => :user, :serializer => UserPostSerializer, :root => :user_posts
end


75
76
77
# File 'lib/scoped_serializer/scope.rb', line 75

def association(*args)
  _association(args, { :preload => true })
end

#dupObject

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.

Examples:

scope.merge!(another_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.options)

  @attributes += scope.attributes
  @associations.merge!(scope.associations)

  @attributes.uniq!

  self
end

#root(key) ⇒ Object

Defines the root key.

Examples:

scope :collection
  root :reservations
end


45
46
47
# File 'lib/scoped_serializer/scope.rb', line 45

def root(key)
  @options.merge!({ :root => key })
end