Class: Fracassandra::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/fracassandra/model.rb

Overview

This class covers a regular column family. If you need super column family support, you will want to look for the SuperModel class.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(defaults = {}) ⇒ Model

Returns a new instance of Model.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fracassandra/model.rb', line 39

def initialize(defaults={})
  r = attributes.select { |k,v| v[:key] == true }[0]
  raise "Invalid key error. No key was given in either the model or as an argument to new" if r.nil?
  @key = r[0]

  @update_operation = false

  defaults.each_pair do |attribute, value|
    self.send(:"#{attribute}=", value)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &blk) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/fracassandra/model.rb', line 170

def method_missing(sym, *args, &blk)
  name = sym.to_s.sub(/=$/, '')
  attribute_names = attributes.keys.map { |k| k.sub(/=$/, '') }

  if attribute_names.include? name
    attribute = attributes[name]
    ivar = instance_variable_get("@#{name}")
    if sym.to_s.include? "="
      raise DatabaseError, "Key values are immutable, don't try and change them." if attribute[:key] && ivar
      instance_variable_set("@#{name}", args[0])
      @key = instance_variable_get("@#{name}") if attribute[:key]
    else
      ivar
    end
  else
    super
  end
end

Class Attribute Details

.attributesObject (readonly)

Returns the value of attribute attributes.



36
37
38
# File 'lib/fracassandra/model.rb', line 36

def attributes
  @attributes
end

.create_hookObject (readonly)

Returns the value of attribute create_hook.



36
37
38
# File 'lib/fracassandra/model.rb', line 36

def create_hook
  @create_hook
end

.update_hookObject (readonly)

Returns the value of attribute update_hook.



36
37
38
# File 'lib/fracassandra/model.rb', line 36

def update_hook
  @update_hook
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



8
9
10
# File 'lib/fracassandra/model.rb', line 8

def key
  @key
end

#update_operationObject

Returns the value of attribute update_operation.



9
10
11
# File 'lib/fracassandra/model.rb', line 9

def update_operation
  @update_operation
end

Class Method Details

.[](key, sub_key = nil) ⇒ Object

Raises:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fracassandra/model.rb', line 83

def self.[](key, sub_key=nil)
  raise DatabaseError, "No database defined. Please set Frassandra.database= to a Cassandra connection." unless Fracassandra.database
  raise DatabaseError, "No column family defined. Please edit your model." unless column_family

  return self.multi_get(*key) if key.is_a? Array

  t = Fracassandra::database.get(column_family, key)
  return nil if t.nil?
  r = self.new
  t.each_pair do |k, v|
    unless k.is_a? Cassandra::Long
      r.send(:"#{k}=", v)
    end
  end
  if sub_key
    h = {}
    t[sub_key].each_pair do |k, v|
      h[k.to_i] = v
    end
    r.send(:"#{sub_key}=", h)
  end
  r.update_operation = true
  r
end

.all_keysObject

Raises:



65
66
67
68
69
70
# File 'lib/fracassandra/model.rb', line 65

def self.all_keys
  raise DatabaseError, "No database defined. Please set Frassandra.database= to a Cassandra connection." unless Fracassandra.database
  raise DatabaseError, "No column family defined. Please edit your model." unless column_family

  Fracassandra::database.get_range(column_family).map { |h| h.key }
end

.attribute(name, options = {}) ⇒ Object



17
18
19
20
# File 'lib/fracassandra/model.rb', line 17

def attribute(name, options={})
  @attributes ||= {}
  @attributes[name.to_s] = options
end

.column_family(column_family_name = nil) ⇒ Object



12
13
14
15
# File 'lib/fracassandra/model.rb', line 12

def column_family(column_family_name=nil)
  return @column_family_name unless column_family_name
  @column_family_name = column_family_name.to_s
end

.create(defaults = {}) ⇒ Object



51
52
53
# File 'lib/fracassandra/model.rb', line 51

def self.create(defaults={})
  self.new(defaults)
end

.from_hash(hash) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/fracassandra/model.rb', line 55

def self.from_hash(hash)
  r = self.new
  hash.each_pair do |k, v|
    unless k.is_a? Cassandra::Long
      r.send(:"#{k}=", v)
    end
  end
  r
end

.list(name, options) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
# File 'lib/fracassandra/model.rb', line 22

def list(name, options)
  raise ArgumentError, "Cannot find 'columns' key in options. Must contain an array of column names." unless options[:columns].is_a? Array
  @attributes ||= {}
  @attributes[name.to_s] = options
end

.multi_get(*keys) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/fracassandra/model.rb', line 72

def self.multi_get(*keys)
  h = Fracassandra.database.get_range(column_family)
  r = self.new
  h.each_pair do |k, v|
    unless k.is_a? Cassandra::Long
      r.send(:"#{k}=", v)
    end
  end
  r
end

.on_create(s) ⇒ Object



28
29
30
# File 'lib/fracassandra/model.rb', line 28

def on_create(s)
  @create_hook = s
end

.on_update(s) ⇒ Object



32
33
34
# File 'lib/fracassandra/model.rb', line 32

def on_update(s)
  @update_hook = s
end

.timestamps!Object



124
125
126
127
# File 'lib/fracassandra/model.rb', line 124

def self.timestamps!
  attribute('created_at')
  attribute('updated_at')
end

Instance Method Details

#attributesObject



108
109
110
# File 'lib/fracassandra/model.rb', line 108

def attributes
  self.class.attributes
end

#column_familyObject



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

def column_family
  self.class.column_family
end

#create_hookObject



116
117
118
# File 'lib/fracassandra/model.rb', line 116

def create_hook
  self.class.create_hook
end

#destroyObject



159
160
161
# File 'lib/fracassandra/model.rb', line 159

def destroy
  Fracassandra::database.remove(column_family, key)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


163
164
165
166
167
168
# File 'lib/fracassandra/model.rb', line 163

def eql?(other)
  attributes.each_key do |attribute_name|
    return false unless send(attribute_name.to_s).eql? other.send(attribute_name.to_s)
  end
  true
end

#saveObject

Raises:



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/fracassandra/model.rb', line 129

def save
  raise DatabaseError, "No database defined. Please set Frassandra.database= to a Cassandra connection." unless Fracassandra.database
  raise DatabaseError, "No column family defined. Please edit your model." unless column_family

  attribute_keys = attributes.keys
  ivars = instance_variables.map do |ivar_name|
    ivar_name[1..-1] if attribute_keys.include? ivar_name[1..-1]
  end.compact
  
  h = {}
  ivars.each do |ivar_name|
    next if attributes[ivar_name][:key]
    h[ivar_name] = instance_variable_get("@#{ivar_name}")
  end

  now = Time.now.to_i
  if ivars.include? "created_at"
    @created_at ||= now
  end
  @updated_at ||= now

  Fracassandra::database.insert(column_family, key, h)

  if @update_operation
    self.send(update_hook.to_sym)
  elsif create_hook
    self.send(create_hook.to_sym)
  end
end

#update_hookObject



120
121
122
# File 'lib/fracassandra/model.rb', line 120

def update_hook
  self.class.update_hook
end