Module: Sandra::ClassMethods

Defined in:
lib/sandra.rb

Instance Method Summary collapse

Instance Method Details

#allObject



68
69
70
71
72
# File 'lib/sandra.rb', line 68

def all
  connection.get_range(self.to_s).map do |key, attributes|
    new_object(key, attributes)
  end
end

#column(col_name, type) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sandra.rb', line 74

def column(col_name, type)
  define_method col_name do
    attr = col_name.to_s
    attributes[attr]
  end
  define_method "#{col_name}=" do |val|
    attr = col_name.to_s
    attributes[attr] = val
  end
  @registered_columns ||= []
  @registered_columns << col_name
end

#connectionObject



98
99
100
# File 'lib/sandra.rb', line 98

def connection
  @connection || establish_connection
end

#create(columns = {}) ⇒ Object



140
141
142
143
144
# File 'lib/sandra.rb', line 140

def create(columns = {})
  obj = self.new(columns)
  obj.save
  obj
end

#establish_connection(options = {}) ⇒ Object



91
92
93
94
95
96
# File 'lib/sandra.rb', line 91

def establish_connection(options = {})
  connection_options = YAML.load_file("#{::Rails.root.to_s}/config/sandra.yml")[::Rails.env].merge(options)
  keyspace = connection_options["keyspace"]
  host = "#{connection_options["host"]}:#{connection_options["port"]}"
  @connection = Cassandra.new(keyspace, host)
end

#find(key) ⇒ Object



112
113
114
# File 'lib/sandra.rb', line 112

def find(key)
  self.get(key)
end

#get(key) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/sandra.rb', line 102

def get(key)
  return nil unless key
  hash = connection.get(self.to_s, key.to_s)
  unless hash.empty?
    self.new_object(key, hash)
  else
    nil
  end
end

#insert(key, columns = {}) ⇒ Object



123
124
125
# File 'lib/sandra.rb', line 123

def insert(key, columns = {})
  connection.insert(self.to_s, key, columns)
end

#keyObject



136
137
138
# File 'lib/sandra.rb', line 136

def key
  @key
end

#key_attribute(name, type) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/sandra.rb', line 127

def key_attribute(name, type)
  @key = name
  validates name, :presence => true, :key => true
  column name, type
  define_method :to_param do
    attributes[name.to_s]
  end
end

#list(name, type) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/sandra.rb', line 152

def list(name, type)
  define_method name do
    var_name = "@__#{name}_list"
    unless instance_variable_get(var_name)
      instance_variable_set(var_name, Sandra::List.new(name, type, self))
    end
    instance_variable_get(var_name)
  end
end

#multi_get(keys) ⇒ Object



162
163
164
165
# File 'lib/sandra.rb', line 162

def multi_get(keys)
  collection = connection.multi_get(self.to_s, keys)
  collection.map {|key, attrs| self.new_object(key, attrs) }
end

#new_object(key, attributes) ⇒ Object



116
117
118
119
120
121
# File 'lib/sandra.rb', line 116

def new_object(key, attributes)
  obj = self.new(attributes)
  obj.send("#{@key}=", key)
  obj.new_record = false
  obj
end

#range(options) ⇒ Object



146
147
148
149
150
# File 'lib/sandra.rb', line 146

def range(options)
  connection.get_range(self.to_s, options).map do |key, value|
    self.new_object(key, value)
  end
end

#registered_columnsObject



87
88
89
# File 'lib/sandra.rb', line 87

def registered_columns
  @registered_columns + Array(@key)
end