Class: CassandraORM::Model
- Inherits:
-
Base
- Object
- Base
- CassandraORM::Model
show all
- Extended by:
- Finder
- Includes:
- Logger, Persist
- Defined in:
- lib/cassandra-orm/model.rb,
lib/cassandra-orm/model/errors.rb,
lib/cassandra-orm/model/finder.rb,
lib/cassandra-orm/model/persist.rb
Defined Under Namespace
Modules: Finder, Persist
Classes: Errors
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Finder
all, count, find, find!, find_all, first
Methods included from Logger
#execute, #execute_async, included
Methods included from Persist
#destroy, #destroy!, #save, #save!
Methods inherited from Base
configure, connect, reconnect
Constructor Details
#initialize(attrs = {}) ⇒ Model
Returns a new instance of Model.
18
19
20
21
22
|
# File 'lib/cassandra-orm/model.rb', line 18
def initialize attrs = {}
fail 'Cannot instantiate CassandraORM::Model' if self == Model
set attrs
@errors = Errors.new self
end
|
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
16
17
18
|
# File 'lib/cassandra-orm/model.rb', line 16
def errors
@errors
end
|
Class Method Details
.inherited(base) ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/cassandra-orm/model.rb', line 91
def inherited base
base.singleton_class.class_exec do
def attributes *names
@attributes ||= []
if names.empty?
@attributes
else
attr_accessor(*names)
@attributes.concat names.map(&:to_sym)
@attributes.uniq!
end
end
attr_reader :primary_key
def set_primary_key *keys
keys.uniq!
attributes(*keys)
keys.each { |key|
define_method("#{key}_with_primary_key_check=") { |val|
if new?
send("#{key}_without_primary_key_check=", val)
elsif send(key) != val
fail(CannotUpdatePrimaryKey)
end
}
alias_method_chain "#{key}=", :primary_key_check
}
@primary_key = keys.map(&:to_sym)
end
private :set_primary_key
def table_name
name.tableize
end
def truncate
execute 'truncate', "TRUNCATE #{table_name}"
end
end
end
|
Instance Method Details
#==(right) ⇒ Object
59
60
61
62
|
# File 'lib/cassandra-orm/model.rb', line 59
def == right
self.class == right.class &&
primary_key_hash == right.primary_key_hash
end
|
#append_error(key, value, options = {}) ⇒ Object
64
65
66
67
|
# File 'lib/cassandra-orm/model.rb', line 64
def append_error key, value, options = {}
@errors.append key, value, options
false
end
|
#append_error!(key, value, options = {}) ⇒ Object
69
70
71
72
|
# File 'lib/cassandra-orm/model.rb', line 69
def append_error! key, value, options = {}
append_error key, value, options
fail ValidationError
end
|
#attributes ⇒ Object
31
32
33
34
35
|
# File 'lib/cassandra-orm/model.rb', line 31
def attributes
self.class.attributes.each_with_object({}) do |key, hash|
hash[key] = send key
end
end
|
#attributes=(attrs) ⇒ Object
37
38
39
40
41
42
|
# File 'lib/cassandra-orm/model.rb', line 37
def attributes= attrs
attrs = attrs.symbolize_keys
self.class.attributes.each do |attr|
send "#{attr}=", attrs[attr]
end
end
|
#inspect ⇒ Object
50
51
52
53
|
# File 'lib/cassandra-orm/model.rb', line 50
def inspect
attrs = attributes.map { |k, v| "#{k}=#{v.inspect}" }.join(' ')
"#<#{self.class.name} #{attrs}>"
end
|
#new? ⇒ Boolean
55
56
57
|
# File 'lib/cassandra-orm/model.rb', line 55
def new?
!@persisted
end
|
#primary_key_hash ⇒ Object
44
45
46
47
48
|
# File 'lib/cassandra-orm/model.rb', line 44
def primary_key_hash
self.class.primary_key.each_with_object({}) do |key, hash|
hash[key] = send key
end
end
|
#reload ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/cassandra-orm/model.rb', line 74
def reload
hash = primary_key_hash
keys, values = hash.keys, hash.values
row = self.class.send(:_find_all, keys, values, limit: 1).first
if row
self.attributes = row
@errors.clear
@persisted = true
self
else
@errors.clear
@persisted = nil
false
end
end
|
#set(attrs) ⇒ Object
24
25
26
27
28
29
|
# File 'lib/cassandra-orm/model.rb', line 24
def set attrs
attrs.each do |k, v|
method = :"#{k}="
respond_to?(method) ? send(method, v) : instance_variable_set(:"@#{k}", v)
end
end
|