fracassandra 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "cassandra"
4
+
5
+ # Add dependencies to develop your gem here.
6
+ # Include everything needed to run rake, tests, features, etc.
7
+ group :development do
8
+ gem "rspec"
9
+ gem "bundler", "~> 1.0.0"
10
+ gem "jeweler", "~> 1.5.2"
11
+ gem "rcov", ">= 0"
12
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Jeremy Tregunna
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,40 @@
1
+ = Fracassandra
2
+
3
+ Fracassandra is an object mapper for the Cassandra database.
4
+
5
+ It's currently not ready for prime time, as super column family support is not available yet. It also
6
+ does not support adjusting the row/key cache sizes, how columns are sorted, etc.
7
+
8
+ == Example usage
9
+
10
+ It can be used in familiar ways, for instance:
11
+
12
+ class User < Fracassandra::Model
13
+ column_family :Users
14
+
15
+ attribute :username, :key => true
16
+ attribute :password
17
+ attribute :salt
18
+ attribute :email
19
+ end
20
+
21
+ This will create a class suitable for use with the Cassandra mapping, using the username as the key to the
22
+ column family, storing a bunch of columns (password, salt and email respectively). Each attribute will respond
23
+ to a getter with the same name, and a setter with the same name. One exception is the :key attribute, which
24
+ is immutable, and cannot be modified. As such, an exception is thrown if you try and use the setter.
25
+
26
+ == Contributing to Fracassandra
27
+
28
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
29
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
30
+ * Fork the project
31
+ * Start a feature/bugfix branch
32
+ * Commit and push until you are happy with your contribution
33
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
34
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
35
+
36
+ == Copyright
37
+
38
+ Copyright (c) 2011 Jeremy Tregunna. See LICENSE.txt for
39
+ further details.
40
+
data/Rakefile ADDED
@@ -0,0 +1,40 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "fracassandra"
16
+ gem.homepage = "http://github.com/jeremytregunna/fracassandra"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Simple object mapper for Cassandra}
19
+ gem.description = %Q{Hook your cassandra column families into ruby classes, mapping your rows to objects.}
20
+ gem.email = "jeremy.tregunna@me.com"
21
+ gem.authors = ["Jeremy Tregunna"]
22
+ gem.add_runtime_dependency 'cassandra', '>= 0'
23
+ gem.add_development_dependency 'rspec', '> 1.2.3'
24
+ end
25
+ Jeweler::RubygemsDotOrgTasks.new
26
+
27
+ #require 'spec/rake/spectask'
28
+ require 'rspec/core/rake_task'
29
+ RSpec::Core::RakeTask.new(:spec)
30
+ task :default => :spec
31
+
32
+ require 'rake/rdoctask'
33
+ Rake::RDocTask.new do |rdoc|
34
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
35
+
36
+ rdoc.rdoc_dir = 'rdoc'
37
+ rdoc.title = "fracassandra #{version}"
38
+ rdoc.rdoc_files.include('README*')
39
+ rdoc.rdoc_files.include('lib/**/*.rb')
40
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,95 @@
1
+ # Frac.as Cassandra Library
2
+ # Copyright © 2011, Jeremy Tregunna, All Rights Reserved.
3
+
4
+ module Fracassandra
5
+
6
+ # This class covers a regular column family. If you need super column family support, you will
7
+ # want to look for the SuperModel class.
8
+ class Model
9
+ attr_reader :key
10
+
11
+ def initialize(key=nil, defaults={})
12
+ if key
13
+ @key = key.to_s
14
+ else
15
+ r = @@attributes.select { |k,v| v[:key] == true }[0]
16
+ raise "Invalid key error. No key was given in either the model or as an argument to new" if r.nil?
17
+ @key = r[0]
18
+ end
19
+
20
+ defaults.each_pair do |attribute, value|
21
+ self.send(:"#{attribute}=", value)
22
+ end
23
+ end
24
+
25
+ def self.create(key=nil, defaults={})
26
+ self.new(key, defaults)
27
+ end
28
+
29
+ def self.[](key)
30
+ raise DatabaseError, "No database defined. Please set Frassandra.database= to a Cassandra connection." unless $cassandra
31
+ raise DatabaseError, "No column family defined. Please edit your model." unless @@column_family_name
32
+
33
+ t = Fracassandra::database.get(@@column_family_name, key)
34
+ return nil if t.nil?
35
+ r = self.new
36
+ t.each_pair do |k, v|
37
+ r.send(:"#{k}=", v)
38
+ end
39
+ r
40
+ end
41
+
42
+ def self.attribute(name, options={})
43
+ @@attributes ||= {}
44
+ @@attributes[name.to_s] = options
45
+ end
46
+
47
+ def self.column_family(column_family_name)
48
+ @@column_family_name = column_family_name.to_s
49
+ end
50
+
51
+ def save
52
+ raise DatabaseError, "No database defined. Please set Frassandra.database= to a Cassandra connection." unless $cassandra
53
+ raise DatabaseError, "No column family defined. Please edit your model." unless @@column_family_name
54
+
55
+ h = {}
56
+ @@attributes.each_pair do |attribute_name, hash|
57
+ next if hash[:key]
58
+ hash.each_pair do |key, value|
59
+ h[attribute_name] = (key == :value) ? value : nil
60
+ end
61
+ end
62
+
63
+ Fracassandra::database.insert(@@column_family_name, key, h)
64
+ end
65
+
66
+ def destroy
67
+ Fracassandra::database.remove(@@column_family_name, key)
68
+ end
69
+
70
+ def eql?(other)
71
+ @@attributes.each_pair do |attribute_name, hash|
72
+ return false unless send(attribute_name.to_s).eql? other.send(attribute_name.to_s)
73
+ end
74
+ true
75
+ end
76
+
77
+ def method_missing(sym, *args, &blk)
78
+ name = sym.to_s
79
+
80
+ if @@attributes.keys.include?(name.sub(/=$/, '').to_s)
81
+ attribute = @@attributes[name.sub(/=$/, '').to_s]
82
+ if name.include?("=")
83
+ raise DatabaseError, "Key values are immutable, don't try and change them." if attribute[:key] && attribute[:value]
84
+ opts = attribute
85
+ opts[:value] = args[0]
86
+ @key = opts[:value] if opts[:key]
87
+ else
88
+ @@attributes[name.to_s][:value]
89
+ end
90
+ else
91
+ super
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,10 @@
1
+ # Frac.as Cassandra Library
2
+ # Copyright © 2011, Jeremy Tregunna, All Rights Reserved.
3
+
4
+ module Fracassandra
5
+
6
+ # Super column family... Not done yet.
7
+ class SuperModel
8
+ attr_reader :key
9
+ end
10
+ end
@@ -0,0 +1,22 @@
1
+ # Frac.as Cassandra Library
2
+ # Copyright © 2011, Jeremy Tregunna, All Rights Reserved.
3
+
4
+ FRACASSANDRA_VERSION = "0.1.0"
5
+
6
+ require 'fracassandra/model'
7
+ require 'fracassandra/super_model'
8
+
9
+ module Fracassandra
10
+ $cassandra = nil
11
+
12
+ def self.database=(db)
13
+ $cassandra = db
14
+ end
15
+
16
+ def self.database
17
+ $cassandra
18
+ end
19
+
20
+ class DatabaseError < Exception
21
+ end
22
+ end
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'cassandra/0.7'
3
+
4
+ class User < Fracassandra::Model
5
+ column_family :Users
6
+
7
+ attribute :username, :key => true
8
+ attribute :password
9
+ attribute :salt
10
+ attribute :email
11
+ end
12
+
13
+ describe "A user record" do
14
+ before(:all) do
15
+ Fracassandra.database = Cassandra.new("Twitter")
16
+ end
17
+
18
+ it "creates a valid user record with minimal data" do
19
+ u = User.create("jeremytregunna", {
20
+ 'password' => "foobar",
21
+ 'salt' => "2F37B7B2-18F9-47BB-8D60-AEFF7275EF87",
22
+ 'email' => "jeremy.tregunna@me.com"
23
+ })
24
+ u.should_not be_nil
25
+ end
26
+
27
+ it "saves the record and can find it afterwards" do
28
+ u1 = User.create("jeremytregunna", {
29
+ 'password' => "foobar",
30
+ 'salt' => "2F37B7B2-18F9-47BB-8D60-AEFF7275EF87",
31
+ 'email' => "jeremy.tregunna@me.com"
32
+ })
33
+ u1.save
34
+ u2 = User["jtregunna"]
35
+ u2.should.eql? u1
36
+ end
37
+ end
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+
11
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
12
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
+ require 'fracassandra'
metadata ADDED
@@ -0,0 +1,182 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fracassandra
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Jeremy Tregunna
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-15 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: cassandra
23
+ type: :runtime
24
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ prerelease: false
34
+ requirement: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ type: :development
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ prerelease: false
48
+ requirement: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: bundler
51
+ type: :development
52
+ version_requirements: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ hash: 23
58
+ segments:
59
+ - 1
60
+ - 0
61
+ - 0
62
+ version: 1.0.0
63
+ prerelease: false
64
+ requirement: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: jeweler
67
+ type: :development
68
+ version_requirements: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ~>
72
+ - !ruby/object:Gem::Version
73
+ hash: 7
74
+ segments:
75
+ - 1
76
+ - 5
77
+ - 2
78
+ version: 1.5.2
79
+ prerelease: false
80
+ requirement: *id004
81
+ - !ruby/object:Gem::Dependency
82
+ name: rcov
83
+ type: :development
84
+ version_requirements: &id005 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ prerelease: false
94
+ requirement: *id005
95
+ - !ruby/object:Gem::Dependency
96
+ name: cassandra
97
+ type: :runtime
98
+ version_requirements: &id006 !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ prerelease: false
108
+ requirement: *id006
109
+ - !ruby/object:Gem::Dependency
110
+ name: rspec
111
+ type: :development
112
+ version_requirements: &id007 !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">"
116
+ - !ruby/object:Gem::Version
117
+ hash: 25
118
+ segments:
119
+ - 1
120
+ - 2
121
+ - 3
122
+ version: 1.2.3
123
+ prerelease: false
124
+ requirement: *id007
125
+ description: Hook your cassandra column families into ruby classes, mapping your rows to objects.
126
+ email: jeremy.tregunna@me.com
127
+ executables: []
128
+
129
+ extensions: []
130
+
131
+ extra_rdoc_files:
132
+ - LICENSE.txt
133
+ - README.rdoc
134
+ files:
135
+ - .document
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.rdoc
139
+ - Rakefile
140
+ - VERSION
141
+ - lib/fracassandra.rb
142
+ - lib/fracassandra/model.rb
143
+ - lib/fracassandra/super_model.rb
144
+ - spec/model_spec.rb
145
+ - spec/spec_helper.rb
146
+ has_rdoc: true
147
+ homepage: http://github.com/jeremytregunna/fracassandra
148
+ licenses:
149
+ - MIT
150
+ post_install_message:
151
+ rdoc_options: []
152
+
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ none: false
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ hash: 3
161
+ segments:
162
+ - 0
163
+ version: "0"
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ none: false
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ hash: 3
170
+ segments:
171
+ - 0
172
+ version: "0"
173
+ requirements: []
174
+
175
+ rubyforge_project:
176
+ rubygems_version: 1.6.2
177
+ signing_key:
178
+ specification_version: 3
179
+ summary: Simple object mapper for Cassandra
180
+ test_files:
181
+ - spec/model_spec.rb
182
+ - spec/spec_helper.rb