fracassandra 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
data/fracassandra.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{fracassandra}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jeremy Tregunna"]
@@ -7,19 +7,10 @@ module Fracassandra
7
7
  class Model
8
8
  attr_reader :key
9
9
 
10
- def initialize(key_name=nil, defaults={})
11
- if key_name.is_a?(Hash)
12
- defaults = key_name
13
- key_name = nil
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(key)
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
- @@attributes.each_pair do |attribute_name, hash|
61
- next if hash[:key]
62
- hash.each_pair do |key, value|
63
- h[attribute_name] = (key == :value) ? value : nil
64
- end
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, hash|
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
- if @@attributes.keys.include?(name.sub(/=$/, '').to_s)
85
- attribute = @@attributes[name.sub(/=$/, '').to_s]
86
- if name.include?("=")
87
- raise DatabaseError, "Key values are immutable, don't try and change them." if attribute[:key] && attribute[:value]
88
- opts = attribute
89
- opts[:value] = args[0]
90
- @key = opts[:value] if opts[:key]
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
- @@attributes[name.to_s][:value]
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("jeremytregunna", @record)
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("jeremytregunna", @record)
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("jeremytregunna", @record)
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.create("jeremytregunna", @record)
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.create("jeremytregunna", @record)
54
+ u1 = User.new(@record)
59
55
  u1.save
60
- u2 = User["jtregunna"]
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: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jeremy Tregunna