class Embedded::Scope

Public Class Methods

new(scope,attributes) click to toggle source
# File lib/embedded/scope.rb, line 3
def initialize(scope,attributes)
  @attributes = attributes
  @scope = scope
end

Public Instance Methods

embedded_attributes_for(embeddable_attr, value = nil) click to toggle source
# 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
method_missing(method, *args, &block) click to toggle source
# File lib/embedded/scope.rb, line 36
def method_missing(method, *args, &block)
  @scope.send(method,*args,&block)
end
where(opts = :chain, *rest) click to toggle source
# 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