Class: Yarel::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/yarel/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name) ⇒ Table

Returns a new instance of Table.



8
9
10
11
12
13
# File 'lib/yarel/table.rb', line 8

def initialize(table_name)
  @table_name = table_name
  @projections = "*"
  @conditions = []
  @limit_to = @offset = :default
end

Instance Attribute Details

#conditionsObject

Returns the value of attribute conditions.



6
7
8
# File 'lib/yarel/table.rb', line 6

def conditions
  @conditions
end

#limit_toObject

Returns the value of attribute limit_to.



6
7
8
# File 'lib/yarel/table.rb', line 6

def limit_to
  @limit_to
end

#offsetObject

Returns the value of attribute offset.



6
7
8
# File 'lib/yarel/table.rb', line 6

def offset
  @offset
end

#projectionsObject

Returns the value of attribute projections.



6
7
8
# File 'lib/yarel/table.rb', line 6

def projections
  @projections
end

#sort_columnsObject

Returns the value of attribute sort_columns.



6
7
8
# File 'lib/yarel/table.rb', line 6

def sort_columns
  @sort_columns
end

#table_nameObject

Returns the value of attribute table_name.



6
7
8
# File 'lib/yarel/table.rb', line 6

def table_name
  @table_name
end

Instance Method Details

#allObject

Raises:



15
16
17
18
19
20
21
# File 'lib/yarel/table.rb', line 15

def all
  yql = to_yql
  Logger.info "Generated YQL: #{yql.inspect}\n"
  response = Connection.get(yql)
  raise Exception.new(response["error"]["description"]) if response["error"]
  response["query"]["results"] ? [response["query"]["results"].first[1]].flatten : []
end

#from(table_name) ⇒ Object



23
24
25
# File 'lib/yarel/table.rb', line 23

def from(table_name)
  modify_clone { self.table_name = table_name }
end

#limit(*options) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/yarel/table.rb', line 49

def limit(*options)
  lim = options[0]
  off, lim = options[0..1] unless options.size == 1
  modify_clone {
    self.limit_to = lim.to_i if lim
    self.offset = off.to_i if off
  }
end

#project(*field_names) ⇒ Object Also known as: select



27
28
29
# File 'lib/yarel/table.rb', line 27

def project(*field_names)
  modify_clone { self.projections = field_names.join(", ") }
end

#sort(*columns) ⇒ Object Also known as: order



58
59
60
# File 'lib/yarel/table.rb', line 58

def sort(*columns)
  modify_clone { self.sort_columns = columns.try(:join, ", ") }
end

#to_yqlObject Also known as: to_s



64
65
66
67
68
69
70
71
# File 'lib/yarel/table.rb', line 64

def to_yql
  yql = ["SELECT #{projections} FROM #{table_name}"]
  yql << "WHERE #{conditions.join(' AND ')}" unless conditions.empty?
  yql << "LIMIT #{limit_to}" if limit_to != :default
  yql << "OFFSET #{offset}" if offset != :default
  yql << "| sort(field='#{sort_columns}')" if sort_columns
  yql.join " "
end

#where(condition) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/yarel/table.rb', line 33

def where(condition)
  new_condition =
  case
  when condition.kind_of?(Hash)
    condition.map do |key, value|
      condition_string = value.kind_of?(self.class) ? "#{key} in ( #{value.to_yql} )" : "#{key} = '#{value}'"
    end
  when condition.kind_of?(String)
    condition
  when condition.kind_of?(Array)
    send :sprintf, condition[0].gsub("?", "'%s'"), *condition[1..-1]
  end

  modify_clone { self.conditions << new_condition }
end