Class: LazyRelation

Inherits:
Object
  • Object
show all
Defined in:
lib/app/models/searchable.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, table_name, command_hash) ⇒ LazyRelation

Returns a new instance of LazyRelation.



10
11
12
13
14
# File 'lib/app/models/searchable.rb', line 10

def initialize(klass, table_name, command_hash)
  @klass = klass
  @table_name = table_name
  @command_hash = command_hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



42
43
44
45
# File 'lib/app/models/searchable.rb', line 42

def method_missing(method_name, *args)
  obj = execute
  obj[0].send(method_name, *args)
end

Instance Method Details

#executeObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/app/models/searchable.rb', line 29

def execute
  all_attrs = DBConnection.execute("    SELECT\n      \#{@table_name}.*\n    FROM\n      \#{@table_name}\n    WHERE\n      \#{where_str}\n  SQL\n\n  all_attrs.map { |atts| @klass.new(atts) }\nend\n")

#where(params) ⇒ Object



16
17
18
19
# File 'lib/app/models/searchable.rb', line 16

def where(params)
  @command_hash = params.merge(@command_hash)
  self
end

#where_strObject



21
22
23
24
25
26
# File 'lib/app/models/searchable.rb', line 21

def where_str
  @command_hash.map do |col, val|
    val = "'#{val}'" if val.is_a?(String)
    "#{@table_name}.#{col} = #{val}"
  end.join(" AND ")
end