Class: Carl::Base
Class Attribute Summary collapse
-
.config ⇒ Object
Returns the value of attribute config.
Instance Attribute Summary collapse
-
#where_bindings ⇒ Object
Define “additive” methods.
-
#where_sql ⇒ Object
Define “additive” methods.
Class Method Summary collapse
- .connection ⇒ Object
- .establish_connection(config = {:keyspace => 'carl', :hosts => ['localhost:9160']}) ⇒ Object
Instance Method Summary collapse
- #each ⇒ Object
- #execute ⇒ Object
-
#query ⇒ Object
Build query.
- #to_hash ⇒ Object
- #where(*conditions) ⇒ Object (also: #and)
Class Attribute Details
.config ⇒ Object
Returns the value of attribute config.
6 7 8 |
# File 'lib/carl/base.rb', line 6 def config @config end |
Instance Attribute Details
#where_bindings ⇒ Object
Define “additive” methods
41 42 43 |
# File 'lib/carl/base.rb', line 41 def where_bindings @where_bindings end |
#where_sql ⇒ Object
Define “additive” methods
41 42 43 |
# File 'lib/carl/base.rb', line 41 def where_sql @where_sql end |
Class Method Details
.connection ⇒ Object
8 9 10 |
# File 'lib/carl/base.rb', line 8 def connection @connection ||= establish_connection end |
.establish_connection(config = {:keyspace => 'carl', :hosts => ['localhost:9160']}) ⇒ Object
12 13 14 15 |
# File 'lib/carl/base.rb', line 12 def establish_connection(config={:keyspace => 'carl', :hosts => ['localhost:9160']}) @config = config @connection = CassandraCQL::Database.new config[:hosts], :keyspace => config[:keyspace] end |
Instance Method Details
#each ⇒ Object
109 110 111 112 113 |
# File 'lib/carl/base.rb', line 109 def each to_hash.each do |_,v| yield v end end |
#execute ⇒ Object
101 102 103 |
# File 'lib/carl/base.rb', line 101 def execute self.class.connection.execute(*query) end |
#query ⇒ Object
Build query
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/carl/base.rb', line 63 def query @sql = [] @bindings = [] # SELECT add_to_sql "SELECT" if select_values add_to_sql (["?"] * select_values.count).join(", "), *select_values.map(&:to_s) elsif count_value add_to_sql "COUNT(*)" else add_to_sql "FIRST ?", column_limit_value if column_limit_value add_to_sql "REVERSED" if reversed_value if range_values add_to_sql (["?"] * range_values.count).join(" .. "), *range_values else add_to_sql "*" end end # FROM add_to_sql "FROM ?", from_value if from_value # USING CONSISTENCY add_to_sql "USING CONSISTENCY ?", using_consistency_value.to_s if using_consistency_value # WHERE if where_sql && !where_sql.empty? add_to_sql "WHERE" add_to_sql where_sql.join(" AND "), *where_bindings end # LIMIT add_to_sql "LIMIT ?", limit_value if limit_value [@sql.join(" ")] + @bindings end |
#to_hash ⇒ Object
105 106 107 |
# File 'lib/carl/base.rb', line 105 def to_hash execute.result.fetch_hash end |
#where(*conditions) ⇒ Object Also known as: and
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/carl/base.rb', line 42 def where(*conditions) clone.tap do |c| c.where_sql ||= [] c.where_bindings ||= [] # Simple conditions: {:condition => 42} if conditions.first.is_a?(Hash) conditions.first.each do |k,v| c.where_sql << "#{k} = ?" c.where_bindings << v end elsif conditions.first.is_a?(String) c.where_sql << conditions.shift c.where_bindings += conditions end end end |