cassandra-model 0.1.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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Tien Le
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.
@@ -0,0 +1,56 @@
1
+ = cassandra-model
2
+
3
+ Simple model support for Cassandra (http://github.com/fauna/cassandra)
4
+
5
+ Currently, it supports:
6
+ * Serialisation (_String_, _Integer_, _Float_, _Boolean_, _DateTime_, _JSON_)
7
+ * Callbacks
8
+ * Validations
9
+
10
+ == Getting started
11
+
12
+ gem install cassandra-model
13
+
14
+ == Define
15
+ require 'cassandra-model'
16
+
17
+ class User << CassandraModel::Base
18
+ column_family :Users
19
+
20
+ key :username
21
+ column :full_name
22
+ column :created, :datetime
23
+ column :profile, :json
24
+
25
+ write_consistency_level Cassandra::Consistency::ALL
26
+ before_save :set_created_at
27
+
28
+ validation do
29
+ errors << "full name required" if full_name.blank?
30
+ end
31
+
32
+ private
33
+
34
+ def set_created_at
35
+ self.created = Time.now
36
+ end
37
+ end
38
+
39
+
40
+ == CRUD
41
+ User.create(:username => 'foo', :full_name => 'foo bar')
42
+
43
+ foo = User.new(:username => 'foo', :full_name => 'foo bar')
44
+ foo.save
45
+
46
+ foo.full_name = 'foo baz'
47
+ foo.save
48
+
49
+ foo = User['foo']
50
+ foo = User.get('foo')
51
+
52
+ foo.destroy
53
+
54
+ == Copyright
55
+
56
+ Copyright (c) 2010 Tien Le. See LICENSE for details.
@@ -0,0 +1,90 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "cassandra-model"
8
+ gem.summary = %Q{Minimal models for Cassandra.}
9
+ gem.description = %Q{Cassandra-model allows you to map ColumnFamily/SuperColumnFamily in Cassandra to Ruby objects. It was designed to be fast and simple.}
10
+ gem.email = "tienlx /at/ gmail /dot/ com"
11
+ gem.homepage = "http://github.com/tienle/cassandra-model"
12
+ gem.authors = ["Tien Le"]
13
+ gem.add_development_dependency "shoulda", ">= 0"
14
+ gem.add_development_dependency "cassandra", ">= 0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/*_test.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/*_test.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "cassandra-model #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
55
+
56
+
57
+ CASSANDRA_HOME = ENV["CASSANDRA_HOME"] || "#{ENV["HOME"]}/apache-cassandra-0.6.0"
58
+ CASSANDRA_PID = ENV["CASSANDRA_PID"] || "/tmp/cassandra.pid".freeze
59
+
60
+ cassandra_env = ""
61
+ cassandra_env << "CASSANDRA_INCLUDE=#{File.expand_path(Dir.pwd)}/test/config/cassandra.in.sh "
62
+ cassandra_env << "CASSANDRA_HOME=#{CASSANDRA_HOME} "
63
+ cassandra_env << "CASSANDRA_CONF=#{File.expand_path(Dir.pwd)}/test/config"
64
+
65
+ namespace :cassandra do
66
+ desc "Start cassandra"
67
+ task :start do
68
+ Dir.chdir(CASSANDRA_HOME) do
69
+ sh("env #{cassandra_env} bin/cassandra -f -p #{CASSANDRA_PID}")
70
+ end
71
+ end
72
+
73
+ desc "Stop cassandra"
74
+ task :stop do
75
+ system "kill -9 `cat #{CASSANDRA_PID}`"
76
+ end
77
+
78
+ desc "Restart cassandra"
79
+ task :restart => [:stop, :start]
80
+
81
+ desc "Clear test data"
82
+ task :clear_test_data do
83
+ unless defined?(CassandraModel)
84
+ $: << 'test'
85
+ require 'test_helper'
86
+ end
87
+ CassandraModel::Base.establish_connection('CassandraModel').clear_keyspace!
88
+ end
89
+ end
90
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{cassandra-model}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tien Le"]
12
+ s.date = %q{2010-11-28}
13
+ s.description = %q{Cassandra-model allows you to map ColumnFamily/SuperColumnFamily in Cassandra to Ruby objects. It was designed to be fast and simple.}
14
+ s.email = %q{tienlx /at/ gmail /dot/ com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "cassandra-model.gemspec",
27
+ "lib/cassandra-model.rb",
28
+ "lib/cassandra-model/base.rb",
29
+ "lib/cassandra-model/callbacks.rb",
30
+ "lib/cassandra-model/persistence.rb",
31
+ "lib/cassandra-model/types.rb",
32
+ "test/base_test.rb",
33
+ "test/callbacks_test.rb",
34
+ "test/cassandra_model_test.rb",
35
+ "test/config/cassandra.in.sh",
36
+ "test/config/log4j-tools.properties",
37
+ "test/config/log4j.properties",
38
+ "test/config/storage-conf.xml",
39
+ "test/test_helper.rb"
40
+ ]
41
+ s.homepage = %q{http://github.com/tienle/cassandra-model}
42
+ s.rdoc_options = ["--charset=UTF-8"]
43
+ s.require_paths = ["lib"]
44
+ s.rubygems_version = %q{1.3.7}
45
+ s.summary = %q{Minimal models for Cassandra.}
46
+ s.test_files = [
47
+ "test/base_test.rb",
48
+ "test/test_helper.rb",
49
+ "test/cassandra_model_test.rb",
50
+ "test/callbacks_test.rb"
51
+ ]
52
+
53
+ if s.respond_to? :specification_version then
54
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
55
+ s.specification_version = 3
56
+
57
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
58
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
59
+ s.add_development_dependency(%q<cassandra>, [">= 0"])
60
+ else
61
+ s.add_dependency(%q<shoulda>, [">= 0"])
62
+ s.add_dependency(%q<cassandra>, [">= 0"])
63
+ end
64
+ else
65
+ s.add_dependency(%q<shoulda>, [">= 0"])
66
+ s.add_dependency(%q<cassandra>, [">= 0"])
67
+ end
68
+ end
69
+
@@ -0,0 +1,27 @@
1
+ require 'cassandra'
2
+ require 'forwardable'
3
+ require 'date'
4
+
5
+ $:.unshift(File.dirname(__FILE__))
6
+
7
+ module CassandraModel
8
+ class CassandraModelError < StandardError; end
9
+ class UnknownRecord < CassandraModelError; end
10
+ class InvalidRecord < CassandraModelError; end
11
+ class RecordNotFound < CassandraModelError; end
12
+ end
13
+
14
+ unless Object.respond_to? :tap
15
+ class Object
16
+ def tap(value)
17
+ yield(value)
18
+ value
19
+ end
20
+ end
21
+ end
22
+
23
+ require 'cassandra-model/types'
24
+ require 'cassandra-model/callbacks'
25
+ require 'cassandra-model/persistence'
26
+ require 'cassandra-model/base'
27
+
@@ -0,0 +1,94 @@
1
+ module CassandraModel
2
+ class Base
3
+ extend Forwardable
4
+ include CassandraModel::Callbacks
5
+ include CassandraModel::Persistence
6
+
7
+ def_delegators :self.class, :connection, :connection=
8
+ define_callbacks :save, :create, :update, :destroy
9
+
10
+ class << self
11
+ def establish_connection(*args)
12
+ @connection = Cassandra.new(*args)
13
+ end
14
+
15
+ def connection
16
+ @connection || (superclass.connection if superclass)
17
+ end
18
+
19
+ def column_family(name = nil)
20
+ @column_family || (@column_family = name || self.name.split('::').last)
21
+ end
22
+
23
+ def key(name)
24
+ class_eval "def #{name}=(value); @key = value.to_s; end"
25
+ end
26
+
27
+ def column(name, type = :string)
28
+ columns[name] = type
29
+ class_eval "def #{name}; #{type.capitalize}Type.load(@attributes['#{name}']); end"
30
+
31
+ if type == :string || type == :integer || type == :float
32
+ class_eval "def #{name}=(value); @attributes['#{name}'] = value.to_s; end"
33
+ else
34
+ class_eval "def #{name}=(value); @attributes['#{name}'] = #{type.capitalize}Type.dump(value); end"
35
+ end
36
+ end
37
+
38
+ def validate(&block)
39
+ raise ArgumentError.new('provide a block that does validation') unless block_given?
40
+ @validation = block
41
+ end
42
+
43
+ def validation
44
+ @validation
45
+ end
46
+
47
+ def columns
48
+ @columns ||= {}
49
+ end
50
+
51
+ private
52
+
53
+ def inherited(child)
54
+ child.instance_variable_set('@connection', @connection)
55
+ super
56
+ end
57
+ end
58
+
59
+ attr_accessor :new_record
60
+ attr_reader :key, :attributes, :errors
61
+
62
+ def initialize(attrs = {}, convert = true)
63
+ @new_record = true
64
+ @errors = []
65
+ @attributes = {}
66
+ if convert
67
+ self.attributes = attrs
68
+ else
69
+ @attributes = attrs
70
+ end
71
+ end
72
+
73
+ def attributes=(attrs)
74
+ attrs.each {|k, v| send("#{k}=", v) if respond_to? "#{k}=".to_sym }
75
+ end
76
+
77
+ def valid?
78
+ @errors << "key required" if key.to_s !~ /\S/
79
+ self.instance_eval(&self.class.validation) unless self.class.validation.nil?
80
+ @errors.empty?
81
+ end
82
+
83
+ def new_record?
84
+ @new_record
85
+ end
86
+
87
+ def ==(other)
88
+ true
89
+ end
90
+
91
+ alias :eql? ==
92
+ end
93
+
94
+ end
@@ -0,0 +1,49 @@
1
+ module CassandraModel
2
+ module Callbacks
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ base.send :include, InstanceMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def define_callbacks(*callbacks)
10
+ callbacks.each do |callback|
11
+ [:before, :after].each do |chain|
12
+ callback_name = "#{chain}_#{callback}"
13
+ instance_eval <<-EVAL, __FILE__, __LINE__ + 1
14
+ def #{callback_name}(*args)
15
+ callbacks[:#{callback_name}] += args
16
+ end
17
+ EVAL
18
+ end
19
+ end
20
+ end
21
+
22
+ def callbacks
23
+ @callbacks ||= Hash.new {|h, k| h[k] = [] }
24
+ end
25
+ end
26
+
27
+ module InstanceMethods
28
+ def run_callbacks(name, &block)
29
+ _run_callbacks(:before, name)
30
+ result = block.call
31
+ _run_callbacks(:after, name, result)
32
+ result
33
+ end
34
+
35
+ private
36
+ def _run_callbacks(chain, name, value = nil)
37
+ callback_name = "#{chain}_#{name}".to_sym
38
+ self.class.callbacks[callback_name].each do |callback|
39
+ next unless callback.is_a? Symbol
40
+ if self.class.instance_method(callback).arity > 0
41
+ self.send(callback, value)
42
+ else
43
+ self.send(callback)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,112 @@
1
+ module CassandraModel
2
+ module Persistence
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ base.send :include, InstanceMethods
6
+ end
7
+
8
+ module InstanceMethods
9
+ def save
10
+ return self unless valid?
11
+ run_callbacks :save do
12
+ callback = new_record? ? :create : :update
13
+ run_callbacks callback do
14
+ write(attributes)
15
+ end
16
+ end
17
+ end
18
+
19
+ def destroy
20
+ run_callbacks :destroy do
21
+ self.class.remove(key)
22
+ end
23
+ end
24
+
25
+ def reload
26
+ self.class.get(key)
27
+ end
28
+
29
+ def update_attributes(attrs)
30
+ attributes = attrs
31
+ save
32
+ end
33
+
34
+ def remove_attributes(attrs)
35
+ attrs.each {|attr| self.class.remove_column(key, attr) }
36
+ end
37
+
38
+ private
39
+
40
+ def write(attrs)
41
+ self.class.write(key, attrs)
42
+ @new_record = false
43
+ end
44
+ end
45
+
46
+ module ClassMethods
47
+ attr_writer :write_consistency_level, :read_consistency_level
48
+
49
+ def write_consistency_level(level)
50
+ @write_consistency_level = level
51
+ end
52
+
53
+ def read_consistency_level(level)
54
+ @read_consistency_level = level
55
+ end
56
+
57
+ def get(key, options = {})
58
+ attrs = connection.get(column_family, key, options)
59
+ return nil if attrs.empty?
60
+ new(attrs, false).tap do |object|
61
+ object.new_record = false
62
+ end
63
+ end
64
+
65
+ alias :find :get
66
+
67
+ def [](key)
68
+ record = get(key)
69
+ raise RecordNotFound, "cannot find out key=`#{key}` in `#{column_family}`" unless record
70
+ record
71
+ end
72
+
73
+ def exists?(key)
74
+ #connection.exists?(column_family, key)
75
+ !connection.get(column_family, key).empty?
76
+ end
77
+
78
+ def all(keyrange = ''..'', options = {})
79
+ results = connection.get_range(column_family, :start => keyrange.first,
80
+ :finish => keyrange.last, :count => (options[:limit] || 100))
81
+ keys = results.map(&:key)
82
+ keys.map {|key| get(key) }
83
+ end
84
+
85
+ def first(keyrange = ''..'', options = {})
86
+ all(keyrange, options.merge(:limit => 1)).first
87
+ end
88
+
89
+ def create(attributes)
90
+ new(attributes).tap do |object|
91
+ object.save
92
+ end
93
+ end
94
+
95
+ def write(key, attributes)
96
+ connection.insert(column_family, key, attributes,
97
+ :consistency => @write_consistency_level || Cassandra::Consistency::QUORUM)
98
+ key
99
+ end
100
+
101
+ def remove(key)
102
+ connection.remove(column_family, key,
103
+ :consistency => @write_consistency_level || Cassandra::Consistency::QUORUM)
104
+ end
105
+
106
+ def remove_column(key, column)
107
+ connection.remove(column_family, key, column,
108
+ :consistency => @write_consistency_level || Cassandra::Consistency::QUORUM)
109
+ end
110
+ end
111
+ end
112
+ end