Class: Gimlet::Queryable::Query

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/gimlet/queryable.rb

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Query

Returns a new instance of Query.



29
30
31
32
# File 'lib/gimlet/queryable.rb', line 29

def initialize(model)
  @model = model
  @where = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



52
53
54
55
56
57
58
# File 'lib/gimlet/queryable.rb', line 52

def method_missing(method, *args, &block)
  if @model.respond_to?(method)
    scoping { @model.send(method, *args, &block) }
  else
    super
  end
end

Instance Method Details

#allObject



48
49
50
# File 'lib/gimlet/queryable.rb', line 48

def all
  @model.select(:where => @where)
end

#scopingObject



60
61
62
63
64
65
# File 'lib/gimlet/queryable.rb', line 60

def scoping
  previous, @model.current_scope = @model.current_scope, self
  yield
ensure
  @model.current_scope = previous
end

#where(hash) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gimlet/queryable.rb', line 34

def where(hash)
  hash.each do |attribute, value|
    case value
    when Array
      @where.push([attribute, :in?, value]) # should be :== ?
    when Regexp
      @where.push([attribute, :=~, value])
    else
      @where.push([attribute, :==, value])
    end
  end
  self
end