fracassandra 0.2.0 → 0.3.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 CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.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.2.0"
8
+ s.version = "0.3.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"]
@@ -26,7 +26,6 @@ Gem::Specification.new do |s|
26
26
  "fracassandra.gemspec",
27
27
  "lib/fracassandra.rb",
28
28
  "lib/fracassandra/model.rb",
29
- "lib/fracassandra/super_model.rb",
30
29
  "spec/model_spec.rb",
31
30
  "spec/spec_helper.rb"
32
31
  ]
@@ -7,8 +7,28 @@ module Fracassandra
7
7
  class Model
8
8
  attr_reader :key
9
9
 
10
+ class << self
11
+ def column_family(column_family_name=nil)
12
+ return @column_family_name unless column_family_name
13
+ @column_family_name = column_family_name.to_s
14
+ end
15
+
16
+ def attribute(name, options={})
17
+ @attributes ||= {}
18
+ @attributes[name.to_s] = options
19
+ end
20
+
21
+ def list(name, options)
22
+ raise ArgumentError, "Cannot find 'columns' key in options. Must contain an array of column names." unless options[:columns].is_a? Array
23
+ @attributes ||= {}
24
+ @attributes[name.to_s] = options
25
+ end
26
+
27
+ attr_reader :attributes
28
+ end
29
+
10
30
  def initialize(defaults={})
11
- r = @@attributes.select { |k,v| v[:key] == true }[0]
31
+ r = attributes.select { |k,v| v[:key] == true }[0]
12
32
  raise "Invalid key error. No key was given in either the model or as an argument to new" if r.nil?
13
33
  @key = r[0]
14
34
 
@@ -21,45 +41,53 @@ module Fracassandra
21
41
  self.new(key, defaults)
22
42
  end
23
43
 
24
- def self.[](key)
25
- raise DatabaseError, "No database defined. Please set Frassandra.database= to a Cassandra connection." unless $cassandra
26
- raise DatabaseError, "No column family defined. Please edit your model." unless @@column_family_name
44
+ def self.[](key, sub_key=nil)
45
+ raise DatabaseError, "No database defined. Please set Frassandra.database= to a Cassandra connection." unless Fracassandra.database
46
+ raise DatabaseError, "No column family defined. Please edit your model." unless column_family
27
47
 
28
- t = Fracassandra::database.get(@@column_family_name, key)
48
+ t = Fracassandra::database.get(column_family, key)
29
49
  return nil if t.nil?
30
50
  r = self.new
31
51
  t.each_pair do |k, v|
32
- r.send(:"#{k}=", v)
52
+ unless k.is_a? Cassandra::Long
53
+ r.send(:"#{k}=", v)
54
+ end
55
+ end
56
+ if sub_key
57
+ h = {}
58
+ t[sub_key].each_pair do |k, v|
59
+ h[k.to_i] = v
60
+ end
61
+ r.send(:"#{sub_key}=", h)
33
62
  end
34
63
  r
35
64
  end
36
65
 
37
- def self.attribute(name, options={})
38
- @@attributes ||= {}
39
- @@attributes[name.to_s] = options
66
+ def attributes
67
+ self.class.attributes
40
68
  end
41
69
 
42
- def self.column_family(column_family_name)
43
- @@column_family_name = column_family_name.to_s
70
+ def column_family
71
+ self.class.column_family
44
72
  end
45
-
73
+
46
74
  def self.timestamps!
47
- @@attributes['created_at'] = {}
48
- @@attributes['updated_at'] = {}
75
+ attribute('created_at')
76
+ attribute('updated_at')
49
77
  end
50
78
 
51
79
  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
80
+ raise DatabaseError, "No database defined. Please set Frassandra.database= to a Cassandra connection." unless Fracassandra.database
81
+ raise DatabaseError, "No column family defined. Please edit your model." unless column_family
54
82
 
55
- attribute_keys = @@attributes.keys
83
+ attribute_keys = attributes.keys
56
84
  ivars = instance_variables.map do |ivar_name|
57
85
  ivar_name[1..-1] if attribute_keys.include? ivar_name[1..-1]
58
86
  end.compact
59
87
 
60
88
  h = {}
61
89
  ivars.each do |ivar_name|
62
- next if @@attributes[ivar_name][:key]
90
+ next if attributes[ivar_name][:key]
63
91
  h[ivar_name] = instance_variable_get("@#{ivar_name}")
64
92
  end
65
93
 
@@ -69,15 +97,15 @@ module Fracassandra
69
97
  end
70
98
  @updated_at ||= now
71
99
 
72
- Fracassandra::database.insert(@@column_family_name, key, h)
100
+ Fracassandra::database.insert(column_family, key, h)
73
101
  end
74
102
 
75
103
  def destroy
76
- Fracassandra::database.remove(@@column_family_name, key)
104
+ Fracassandra::database.remove(column_family, key)
77
105
  end
78
106
 
79
107
  def eql?(other)
80
- @@attributes.each_pair do |attribute_name, _|
108
+ attributes.each_key do |attribute_name|
81
109
  return false unless send(attribute_name.to_s).eql? other.send(attribute_name.to_s)
82
110
  end
83
111
  true
@@ -85,10 +113,10 @@ module Fracassandra
85
113
 
86
114
  def method_missing(sym, *args, &blk)
87
115
  name = sym.to_s.sub(/=$/, '')
88
- attribute_names = @@attributes.keys.map { |k| k.sub(/=$/, '') }
116
+ attribute_names = attributes.keys.map { |k| k.sub(/=$/, '') }
89
117
 
90
118
  if attribute_names.include? name
91
- attribute = @@attributes[name]
119
+ attribute = attributes[name]
92
120
  ivar = instance_variable_get("@#{name}")
93
121
  if sym.to_s.include? "="
94
122
  raise DatabaseError, "Key values are immutable, don't try and change them." if attribute[:key] && ivar
@@ -102,4 +130,4 @@ module Fracassandra
102
130
  end
103
131
  end
104
132
  end
105
- end
133
+ end
data/lib/fracassandra.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # Frac.as Cassandra Library
2
2
  # Copyright © 2011, Jeremy Tregunna, All Rights Reserved.
3
3
 
4
- FRACASSANDRA_VERSION = "0.1.1"
4
+ FRACASSANDRA_VERSION = "0.3.0"
5
5
 
6
6
  require 'fracassandra/model'
7
7
  require 'fracassandra/super_model'
data/spec/model_spec.rb CHANGED
@@ -10,6 +10,13 @@ class User < Fracassandra::Model
10
10
  attribute :email
11
11
  end
12
12
 
13
+ class Group < Fracassandra::Model
14
+ column_family :Groups
15
+
16
+ attribute :id, :key => true
17
+ list :name, :columns => [:index, :username]
18
+ end
19
+
13
20
  describe "A user record" do
14
21
  before(:all) do
15
22
  Fracassandra.database = Cassandra.new("Twitter")
@@ -64,3 +71,38 @@ describe "A user record" do
64
71
  # User["jeremytregunna"].password.should be_nil
65
72
  # end
66
73
  end
74
+
75
+ describe "Group model" do
76
+ before(:all) do
77
+ Fracassandra.database = Cassandra.new("Twitter")
78
+ end
79
+
80
+ it "creates a group record" do
81
+ g = Group.new({
82
+ 'id' => '12345',
83
+ 'name' => {
84
+ 1 => 'foobar',
85
+ 2 => 'blurgle'
86
+ }
87
+ })
88
+ g.should_not be_nil
89
+ end
90
+
91
+ it "is able to fetch a group record" do
92
+ g1 = Group.new({
93
+ 'id' => '12345',
94
+ 'name' => {
95
+ 1 => 'foobar',
96
+ 2 => 'blurgle'
97
+ }
98
+ })
99
+ g1.save
100
+ g2 = Group["12345"]
101
+ g2.should.eql? g1
102
+ end
103
+
104
+ it "is able to fetch a group record based on super column name" do
105
+ g = Group["12345", "name"]
106
+ g.should_not be_nil
107
+ end
108
+ 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: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jeremy Tregunna
@@ -141,7 +141,6 @@ files:
141
141
  - fracassandra.gemspec
142
142
  - lib/fracassandra.rb
143
143
  - lib/fracassandra/model.rb
144
- - lib/fracassandra/super_model.rb
145
144
  - spec/model_spec.rb
146
145
  - spec/spec_helper.rb
147
146
  has_rdoc: true
@@ -1,10 +0,0 @@
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