Class: Embedded::Scope

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

Instance Method Summary collapse

Constructor Details

#initialize(scope, attributes) ⇒ Scope

Returns a new instance of Scope.



3
4
5
6
# File 'lib/embedded/scope.rb', line 3

def initialize(scope,attributes)
  @attributes = attributes
  @scope = scope
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



36
37
38
# File 'lib/embedded/scope.rb', line 36

def method_missing(method, *args, &block)
  @scope.send(method,*args,&block)
end

Instance Method Details

#embedded_attributes_for(embeddable_attr, value = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/embedded/scope.rb', line 8

def embedded_attributes_for(embeddable_attr, value = nil)
  attrs = @attributes[embeddable_attr][:attrs]

  if attrs.is_a?(Array)
    attrs.inject({}) do |a,attr|
      a.merge(:"#{embeddable_attr}_#{attr}" => value ? value.send(attr) : nil)
    end
  elsif attrs.is_a?(Hash)
    attrs.inject({}) do |a,(attr,column)| 
      a.merge(column => value ? value.send(attr) : nil)
    end
  end
end

#where(opts = :chain, *rest) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/embedded/scope.rb', line 22

def where(opts = :chain, *rest)
  if opts.is_a?(Hash)
    opts = opts.inject({}) do |h,(k,v)|
      if @attributes[k]
        h.merge(embedded_attributes_for(k,v))
      else
        h
      end
    end
  end

  self.class.new(@scope.where(opts, *rest), @attributes)
end