Class: Yarel::Table
- Inherits:
-
Object
- Object
- Yarel::Table
- Defined in:
- lib/yarel/table.rb
Instance Attribute Summary collapse
-
#conditions ⇒ Object
Returns the value of attribute conditions.
-
#limit_to ⇒ Object
Returns the value of attribute limit_to.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#projections ⇒ Object
Returns the value of attribute projections.
-
#sort_columns ⇒ Object
Returns the value of attribute sort_columns.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
Instance Method Summary collapse
- #all ⇒ Object
- #from(table_name) ⇒ Object
-
#initialize(table_name) ⇒ Table
constructor
A new instance of Table.
- #limit(*options) ⇒ Object
- #project(*field_names) ⇒ Object (also: #select)
- #sort(*columns) ⇒ Object (also: #order)
- #to_yql ⇒ Object (also: #to_s)
- #where(condition) ⇒ Object
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
#conditions ⇒ Object
Returns the value of attribute conditions.
6 7 8 |
# File 'lib/yarel/table.rb', line 6 def conditions @conditions end |
#limit_to ⇒ Object
Returns the value of attribute limit_to.
6 7 8 |
# File 'lib/yarel/table.rb', line 6 def limit_to @limit_to end |
#offset ⇒ Object
Returns the value of attribute offset.
6 7 8 |
# File 'lib/yarel/table.rb', line 6 def offset @offset end |
#projections ⇒ Object
Returns the value of attribute projections.
6 7 8 |
# File 'lib/yarel/table.rb', line 6 def projections @projections end |
#sort_columns ⇒ Object
Returns the value of attribute sort_columns.
6 7 8 |
# File 'lib/yarel/table.rb', line 6 def sort_columns @sort_columns end |
#table_name ⇒ Object
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
#all ⇒ Object
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(*) lim = [0] off, lim = [0..1] unless .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_yql ⇒ Object 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 |