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



28
29
30
# File 'lib/embedded/scope.rb', line 28

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
# File 'lib/embedded/scope.rb', line 8

def embedded_attributes_for(embeddable_attr, value = nil)
  @attributes[embeddable_attr][:attrs].inject({}) do |a,attr|
    a.merge(:"#{embeddable_attr}_#{attr}" => value ? value.send(attr) : nil)
  end
end

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



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/embedded/scope.rb', line 14

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