fracassandra 0.1.0 → 0.1.1

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 CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -0,0 +1,73 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{fracassandra}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jeremy Tregunna"]
12
+ s.date = %q{2011-03-16}
13
+ s.description = %q{Hook your cassandra column families into ruby classes, mapping your rows to objects.}
14
+ s.email = %q{jeremy.tregunna@me.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "LICENSE.txt",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "fracassandra.gemspec",
27
+ "lib/fracassandra.rb",
28
+ "lib/fracassandra/model.rb",
29
+ "lib/fracassandra/super_model.rb",
30
+ "spec/model_spec.rb",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/jeremytregunna/fracassandra}
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.6.2}
37
+ s.summary = %q{Simple object mapper for Cassandra}
38
+ s.test_files = [
39
+ "spec/model_spec.rb",
40
+ "spec/spec_helper.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_runtime_dependency(%q<cassandra>, [">= 0"])
48
+ s.add_development_dependency(%q<rspec>, [">= 0"])
49
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
50
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
51
+ s.add_development_dependency(%q<rcov>, [">= 0"])
52
+ s.add_runtime_dependency(%q<cassandra>, [">= 0"])
53
+ s.add_development_dependency(%q<rspec>, ["> 1.2.3"])
54
+ else
55
+ s.add_dependency(%q<cassandra>, [">= 0"])
56
+ s.add_dependency(%q<rspec>, [">= 0"])
57
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
58
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
59
+ s.add_dependency(%q<rcov>, [">= 0"])
60
+ s.add_dependency(%q<cassandra>, [">= 0"])
61
+ s.add_dependency(%q<rspec>, ["> 1.2.3"])
62
+ end
63
+ else
64
+ s.add_dependency(%q<cassandra>, [">= 0"])
65
+ s.add_dependency(%q<rspec>, [">= 0"])
66
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
67
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
68
+ s.add_dependency(%q<rcov>, [">= 0"])
69
+ s.add_dependency(%q<cassandra>, [">= 0"])
70
+ s.add_dependency(%q<rspec>, ["> 1.2.3"])
71
+ end
72
+ end
73
+
@@ -2,56 +2,60 @@
2
2
  # Copyright © 2011, Jeremy Tregunna, All Rights Reserved.
3
3
 
4
4
  module Fracassandra
5
-
6
5
  # This class covers a regular column family. If you need super column family support, you will
7
6
  # want to look for the SuperModel class.
8
7
  class Model
9
8
  attr_reader :key
10
-
11
- def initialize(key=nil, defaults={})
12
- if key
13
- @key = key.to_s
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
14
18
  else
15
19
  r = @@attributes.select { |k,v| v[:key] == true }[0]
16
20
  raise "Invalid key error. No key was given in either the model or as an argument to new" if r.nil?
17
21
  @key = r[0]
18
22
  end
19
-
23
+
20
24
  defaults.each_pair do |attribute, value|
21
25
  self.send(:"#{attribute}=", value)
22
26
  end
23
27
  end
24
-
28
+
25
29
  def self.create(key=nil, defaults={})
26
30
  self.new(key, defaults)
27
31
  end
28
-
32
+
29
33
  def self.[](key)
30
34
  raise DatabaseError, "No database defined. Please set Frassandra.database= to a Cassandra connection." unless $cassandra
31
35
  raise DatabaseError, "No column family defined. Please edit your model." unless @@column_family_name
32
-
36
+
33
37
  t = Fracassandra::database.get(@@column_family_name, key)
34
38
  return nil if t.nil?
35
- r = self.new
39
+ r = self.new(key)
36
40
  t.each_pair do |k, v|
37
41
  r.send(:"#{k}=", v)
38
42
  end
39
43
  r
40
44
  end
41
-
45
+
42
46
  def self.attribute(name, options={})
43
47
  @@attributes ||= {}
44
48
  @@attributes[name.to_s] = options
45
49
  end
46
-
50
+
47
51
  def self.column_family(column_family_name)
48
52
  @@column_family_name = column_family_name.to_s
49
53
  end
50
-
54
+
51
55
  def save
52
56
  raise DatabaseError, "No database defined. Please set Frassandra.database= to a Cassandra connection." unless $cassandra
53
57
  raise DatabaseError, "No column family defined. Please edit your model." unless @@column_family_name
54
-
58
+
55
59
  h = {}
56
60
  @@attributes.each_pair do |attribute_name, hash|
57
61
  next if hash[:key]
@@ -59,24 +63,24 @@ module Fracassandra
59
63
  h[attribute_name] = (key == :value) ? value : nil
60
64
  end
61
65
  end
62
-
66
+
63
67
  Fracassandra::database.insert(@@column_family_name, key, h)
64
68
  end
65
-
69
+
66
70
  def destroy
67
71
  Fracassandra::database.remove(@@column_family_name, key)
68
72
  end
69
-
73
+
70
74
  def eql?(other)
71
75
  @@attributes.each_pair do |attribute_name, hash|
72
76
  return false unless send(attribute_name.to_s).eql? other.send(attribute_name.to_s)
73
77
  end
74
78
  true
75
79
  end
76
-
80
+
77
81
  def method_missing(sym, *args, &blk)
78
82
  name = sym.to_s
79
-
83
+
80
84
  if @@attributes.keys.include?(name.sub(/=$/, '').to_s)
81
85
  attribute = @@attributes[name.sub(/=$/, '').to_s]
82
86
  if name.include?("=")
data/lib/fracassandra.rb CHANGED
@@ -1,22 +1,26 @@
1
1
  # Frac.as Cassandra Library
2
2
  # Copyright © 2011, Jeremy Tregunna, All Rights Reserved.
3
3
 
4
- FRACASSANDRA_VERSION = "0.1.0"
4
+ FRACASSANDRA_VERSION = "0.1.1"
5
5
 
6
6
  require 'fracassandra/model'
7
7
  require 'fracassandra/super_model'
8
8
 
9
9
  module Fracassandra
10
10
  $cassandra = nil
11
-
11
+
12
12
  def self.database=(db)
13
13
  $cassandra = db
14
14
  end
15
-
15
+
16
16
  def self.database
17
17
  $cassandra
18
18
  end
19
-
19
+
20
+ def self.credentials=(options)
21
+ $cassandra.login!(options[:username], options[:password])
22
+ end
23
+
20
24
  class DatabaseError < Exception
21
25
  end
22
26
  end
data/spec/model_spec.rb CHANGED
@@ -3,7 +3,7 @@ require 'cassandra/0.7'
3
3
 
4
4
  class User < Fracassandra::Model
5
5
  column_family :Users
6
-
6
+
7
7
  attribute :username, :key => true
8
8
  attribute :password
9
9
  attribute :salt
@@ -14,24 +14,57 @@ describe "A user record" do
14
14
  before(:all) do
15
15
  Fracassandra.database = Cassandra.new("Twitter")
16
16
  end
17
-
18
- it "creates a valid user record with minimal data" do
19
- u = User.create("jeremytregunna", {
17
+
18
+ before(:each) do
19
+ @record = {
20
20
  'password' => "foobar",
21
21
  'salt' => "2F37B7B2-18F9-47BB-8D60-AEFF7275EF87",
22
22
  'email' => "jeremy.tregunna@me.com"
23
- })
24
- u.should_not be_nil
23
+ }
25
24
  end
26
-
27
- it "saves the record and can find it afterwards" do
28
- u1 = User.create("jeremytregunna", {
25
+
26
+ it "responds to the password getter" do
27
+ u = User.new("jeremytregunna", @record)
28
+ u.password.should == "foobar"
29
+ end
30
+
31
+ it "changes the value of password" do
32
+ u = User.new("jeremytregunna", @record)
33
+ u.password = "barfoo"
34
+ u.password.should == "barfoo"
35
+ end
36
+
37
+ it "can retrieve the key by attribute name" do
38
+ u = User.new({
39
+ 'username' => 'jeremytregunna',
29
40
  'password' => "foobar",
30
41
  'salt' => "2F37B7B2-18F9-47BB-8D60-AEFF7275EF87",
31
42
  'email' => "jeremy.tregunna@me.com"
32
43
  })
44
+ u.username.should == "jeremytregunna"
45
+ end
46
+
47
+ it "raises an exception when trying to change the key" do
48
+ u = User.new("jeremytregunna", @record)
49
+ lambda { u.username = "godzilla" }.should raise_error(Fracassandra::DatabaseError)
50
+ end
51
+
52
+ it "creates a valid user record with minimal data" do
53
+ u = User.create("jeremytregunna", @record)
54
+ u.should_not be_nil
55
+ end
56
+
57
+ it "saves the record and can find it afterwards" do
58
+ u1 = User.create("jeremytregunna", @record)
33
59
  u1.save
34
60
  u2 = User["jtregunna"]
35
61
  u2.should.eql? u1
36
62
  end
37
- end
63
+
64
+ # Disabled because of some weird behaviour in Cassandra. Can still find it after it's deleted through the Thrift API,
65
+ # but Cassandra's CLI shows no columns.
66
+ # it "deletes the record" do
67
+ # User["jeremytregunna"].destroy
68
+ # User["jeremytregunna"].password.should be_nil
69
+ # end
70
+ end
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: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jeremy Tregunna
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-15 00:00:00 -04:00
18
+ date: 2011-03-16 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -138,6 +138,7 @@ files:
138
138
  - README.rdoc
139
139
  - Rakefile
140
140
  - VERSION
141
+ - fracassandra.gemspec
141
142
  - lib/fracassandra.rb
142
143
  - lib/fracassandra/model.rb
143
144
  - lib/fracassandra/super_model.rb