class LazyRelation

Public Class Methods

new(klass, table_name, command_hash) click to toggle source
# 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

Public Instance Methods

execute() click to toggle source
# File lib/app/models/searchable.rb, line 29
  def execute
    all_attrs = DBConnection.execute(<<-SQL)
      SELECT
        #{@table_name}.*
      FROM
        #{@table_name}
      WHERE
        #{where_str}
    SQL

    all_attrs.map { |atts| @klass.new(atts) }
  end
method_missing(method_name, *args) click to toggle source
# File lib/app/models/searchable.rb, line 42
def method_missing(method_name, *args)
  obj = execute
  obj[0].send(method_name, *args)
end
where(params) click to toggle source
# File lib/app/models/searchable.rb, line 16
def where(params)
  @command_hash = params.merge(@command_hash)
  self
end
where_str() click to toggle source
# 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