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

Class Method Summary collapse

Instance Method Summary collapse

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

#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


78
79
80
# File 'lib/scoped_serializer/scope.rb', line 78

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

Class Method Details

.from_hash(data = {}) ⇒ Object

Initializes a scope from hash.

Examples:

Scope.from_hash({ :attributes => [:title, :created_at] })


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, 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


96
97
98
# File 'lib/scoped_serializer/scope.rb', line 96

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

#dupObject

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.

Examples:

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


66
67
68
# File 'lib/scoped_serializer/scope.rb', line 66

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