Module: Sandra::ClassMethods

Defined in:
lib/sandra.rb

Instance Method Summary collapse

Instance Method Details

#column(col_name, type) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sandra.rb', line 60

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



84
85
86
# File 'lib/sandra.rb', line 84

def connection
  @connection || establish_connection
end

#create(columns = {}) ⇒ Object



126
127
128
129
130
# File 'lib/sandra.rb', line 126

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

#establish_connection(options = {}) ⇒ Object



77
78
79
80
81
82
# File 'lib/sandra.rb', line 77

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



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

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

#get(key) ⇒ Object



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

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



109
110
111
# File 'lib/sandra.rb', line 109

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

#keyObject



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

def key
  @key
end

#key_attribute(name, type) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/sandra.rb', line 113

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



138
139
140
141
142
143
144
145
146
# File 'lib/sandra.rb', line 138

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



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

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



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

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

#range(options) ⇒ Object



132
133
134
135
136
# File 'lib/sandra.rb', line 132

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

#registered_columnsObject



73
74
75
# File 'lib/sandra.rb', line 73

def registered_columns
  @registered_columns + Array(@key)
end