fracassandra 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/fracassandra.gemspec +1 -1
- data/lib/fracassandra/model.rb +36 -30
- data/spec/model_spec.rb +8 -12
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/fracassandra.gemspec
CHANGED
data/lib/fracassandra/model.rb
CHANGED
@@ -7,19 +7,10 @@ module Fracassandra
|
|
7
7
|
class Model
|
8
8
|
attr_reader :key
|
9
9
|
|
10
|
-
def initialize(
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
end
|
15
|
-
|
16
|
-
if key_name
|
17
|
-
@key = key_name.to_s
|
18
|
-
else
|
19
|
-
r = @@attributes.select { |k,v| v[:key] == true }[0]
|
20
|
-
raise "Invalid key error. No key was given in either the model or as an argument to new" if r.nil?
|
21
|
-
@key = r[0]
|
22
|
-
end
|
10
|
+
def initialize(defaults={})
|
11
|
+
r = @@attributes.select { |k,v| v[:key] == true }[0]
|
12
|
+
raise "Invalid key error. No key was given in either the model or as an argument to new" if r.nil?
|
13
|
+
@key = r[0]
|
23
14
|
|
24
15
|
defaults.each_pair do |attribute, value|
|
25
16
|
self.send(:"#{attribute}=", value)
|
@@ -36,7 +27,7 @@ module Fracassandra
|
|
36
27
|
|
37
28
|
t = Fracassandra::database.get(@@column_family_name, key)
|
38
29
|
return nil if t.nil?
|
39
|
-
r = self.new
|
30
|
+
r = self.new
|
40
31
|
t.each_pair do |k, v|
|
41
32
|
r.send(:"#{k}=", v)
|
42
33
|
end
|
@@ -51,18 +42,32 @@ module Fracassandra
|
|
51
42
|
def self.column_family(column_family_name)
|
52
43
|
@@column_family_name = column_family_name.to_s
|
53
44
|
end
|
45
|
+
|
46
|
+
def self.timestamps!
|
47
|
+
@@attributes['created_at'] = {}
|
48
|
+
@@attributes['updated_at'] = {}
|
49
|
+
end
|
54
50
|
|
55
51
|
def save
|
56
52
|
raise DatabaseError, "No database defined. Please set Frassandra.database= to a Cassandra connection." unless $cassandra
|
57
53
|
raise DatabaseError, "No column family defined. Please edit your model." unless @@column_family_name
|
58
54
|
|
55
|
+
attribute_keys = @@attributes.keys
|
56
|
+
ivars = instance_variables.map do |ivar_name|
|
57
|
+
ivar_name[1..-1] if attribute_keys.include? ivar_name[1..-1]
|
58
|
+
end.compact
|
59
|
+
|
59
60
|
h = {}
|
60
|
-
|
61
|
-
next if
|
62
|
-
|
63
|
-
|
64
|
-
|
61
|
+
ivars.each do |ivar_name|
|
62
|
+
next if @@attributes[ivar_name][:key]
|
63
|
+
h[ivar_name] = instance_variable_get("@#{ivar_name}")
|
64
|
+
end
|
65
|
+
|
66
|
+
now = Time.now.to_i
|
67
|
+
if ivars.include? "created_at"
|
68
|
+
@created_at ||= now
|
65
69
|
end
|
70
|
+
@updated_at ||= now
|
66
71
|
|
67
72
|
Fracassandra::database.insert(@@column_family_name, key, h)
|
68
73
|
end
|
@@ -72,24 +77,25 @@ module Fracassandra
|
|
72
77
|
end
|
73
78
|
|
74
79
|
def eql?(other)
|
75
|
-
@@attributes.each_pair do |attribute_name,
|
80
|
+
@@attributes.each_pair do |attribute_name, _|
|
76
81
|
return false unless send(attribute_name.to_s).eql? other.send(attribute_name.to_s)
|
77
82
|
end
|
78
83
|
true
|
79
84
|
end
|
80
85
|
|
81
86
|
def method_missing(sym, *args, &blk)
|
82
|
-
name = sym.to_s
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
87
|
+
name = sym.to_s.sub(/=$/, '')
|
88
|
+
attribute_names = @@attributes.keys.map { |k| k.sub(/=$/, '') }
|
89
|
+
|
90
|
+
if attribute_names.include? name
|
91
|
+
attribute = @@attributes[name]
|
92
|
+
ivar = instance_variable_get("@#{name}")
|
93
|
+
if sym.to_s.include? "="
|
94
|
+
raise DatabaseError, "Key values are immutable, don't try and change them." if attribute[:key] && ivar
|
95
|
+
instance_variable_set("@#{name}", args[0])
|
96
|
+
@key = instance_variable_get("@#{name}") if attribute[:key]
|
91
97
|
else
|
92
|
-
|
98
|
+
ivar
|
93
99
|
end
|
94
100
|
else
|
95
101
|
super
|
data/spec/model_spec.rb
CHANGED
@@ -17,6 +17,7 @@ describe "A user record" do
|
|
17
17
|
|
18
18
|
before(:each) do
|
19
19
|
@record = {
|
20
|
+
'username' => 'jeremytregunna',
|
20
21
|
'password' => "foobar",
|
21
22
|
'salt' => "2F37B7B2-18F9-47BB-8D60-AEFF7275EF87",
|
22
23
|
'email' => "jeremy.tregunna@me.com"
|
@@ -24,40 +25,35 @@ describe "A user record" do
|
|
24
25
|
end
|
25
26
|
|
26
27
|
it "responds to the password getter" do
|
27
|
-
u = User.new(
|
28
|
+
u = User.new(@record)
|
28
29
|
u.password.should == "foobar"
|
29
30
|
end
|
30
31
|
|
31
32
|
it "changes the value of password" do
|
32
|
-
u = User.new(
|
33
|
+
u = User.new(@record)
|
33
34
|
u.password = "barfoo"
|
34
35
|
u.password.should == "barfoo"
|
35
36
|
end
|
36
37
|
|
37
38
|
it "can retrieve the key by attribute name" do
|
38
|
-
u = User.new(
|
39
|
-
'username' => 'jeremytregunna',
|
40
|
-
'password' => "foobar",
|
41
|
-
'salt' => "2F37B7B2-18F9-47BB-8D60-AEFF7275EF87",
|
42
|
-
'email' => "jeremy.tregunna@me.com"
|
43
|
-
})
|
39
|
+
u = User.new(@record)
|
44
40
|
u.username.should == "jeremytregunna"
|
45
41
|
end
|
46
42
|
|
47
43
|
it "raises an exception when trying to change the key" do
|
48
|
-
u = User.new(
|
44
|
+
u = User.new(@record)
|
49
45
|
lambda { u.username = "godzilla" }.should raise_error(Fracassandra::DatabaseError)
|
50
46
|
end
|
51
47
|
|
52
48
|
it "creates a valid user record with minimal data" do
|
53
|
-
u = User.
|
49
|
+
u = User.new(@record)
|
54
50
|
u.should_not be_nil
|
55
51
|
end
|
56
52
|
|
57
53
|
it "saves the record and can find it afterwards" do
|
58
|
-
u1 = User.
|
54
|
+
u1 = User.new(@record)
|
59
55
|
u1.save
|
60
|
-
u2 = User["
|
56
|
+
u2 = User["jeremytregunna"]
|
61
57
|
u2.should.eql? u1
|
62
58
|
end
|
63
59
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fracassandra
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jeremy Tregunna
|