Class: Carl::Base

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/carl/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



6
7
8
# File 'lib/carl/base.rb', line 6

def config
  @config
end

Instance Attribute Details

#where_bindingsObject

Define “additive” methods



41
42
43
# File 'lib/carl/base.rb', line 41

def where_bindings
  @where_bindings
end

#where_sqlObject

Define “additive” methods



41
42
43
# File 'lib/carl/base.rb', line 41

def where_sql
  @where_sql
end

Class Method Details

.connectionObject



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

#eachObject



109
110
111
112
113
# File 'lib/carl/base.rb', line 109

def each
  to_hash.each do |_,v|
    yield v
  end
end

#executeObject



101
102
103
# File 'lib/carl/base.rb', line 101

def execute
  self.class.connection.execute(*query)
end

#queryObject

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_hashObject



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