gotime-cassandra_object 2.11.9 → 2.12.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'gotime-cassandra_object'
5
- s.version = '2.11.9'
5
+ s.version = '2.12.0'
6
6
  s.description = 'Cassandra ActiveModel'
7
7
  s.summary = 'Cassandra ActiveModel'
8
8
 
@@ -10,12 +10,11 @@ module CassandraObject
10
10
  end
11
11
 
12
12
  def id
13
- key.to_s
13
+ @id ||= self.class._generate_key(self)
14
14
  end
15
15
 
16
- def id=(key)
17
- @key = self.class.parse_key(key)
18
- id
16
+ def id=(id)
17
+ @id = id
19
18
  end
20
19
 
21
20
  def attributes
@@ -3,20 +3,18 @@ module CassandraObject
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  module ClassMethods
6
- def find(key)
7
- key_string = key.try(:to_s)
8
-
9
- if key_string.blank?
10
- raise CassandraObject::RecordNotFound, "Couldn't find #{self.name} with key #{key.inspect}"
11
- elsif attributes = connection.get(column_family, key_string, {:count => 500}).presence
12
- instantiate(key_string, attributes)
6
+ def find(id)
7
+ if id.blank?
8
+ raise CassandraObject::RecordNotFound, "Couldn't find #{self.name} with key #{id.inspect}"
9
+ elsif attributes = connection.get(column_family, id, {:count => 500}).presence
10
+ instantiate(id, attributes)
13
11
  else
14
12
  raise CassandraObject::RecordNotFound
15
13
  end
16
14
  end
17
15
 
18
- def find_by_id(key)
19
- find(key)
16
+ def find_by_id(id)
17
+ find(id)
20
18
  rescue CassandraObject::RecordNotFound
21
19
  nil
22
20
  end
@@ -55,7 +53,7 @@ module CassandraObject
55
53
  end
56
54
 
57
55
  Hash[attribute_results.map do |key, attributes|
58
- [parse_key(key), attributes.present? ? instantiate(key, attributes) : nil]
56
+ [key, attributes.present? ? instantiate(key, attributes) : nil]
59
57
  end]
60
58
  end
61
59
  end
@@ -1,4 +1,4 @@
1
- class <%= name.classify %> < CassandraObject::Schema::Migration
1
+ class <%= name.camelcase %> < CassandraObject::Schema::Migration
2
2
  def self.up
3
3
 
4
4
  end
@@ -1,52 +1,24 @@
1
1
  module CassandraObject
2
2
  module Identity
3
3
  extend ActiveSupport::Concern
4
- extend ActiveSupport::Autoload
5
-
6
- autoload :Key
7
- autoload :AbstractKeyFactory
8
- autoload :UUIDKeyFactory
9
- autoload :NaturalKeyFactory
10
- autoload :HashedNaturalKeyFactory
11
- autoload :CustomKeyFactory
12
4
 
13
5
  included do
14
- class_attribute :key_factory
15
- key :uuid
16
- end
6
+ class_attribute :key_generator
17
7
 
18
- module ClassMethods
19
- # Indicate what kind of key the model will have: uuid or natural
20
- #
21
- # @param [:uuid, :natural] the type of key
22
- # @param the options you want to pass along to the key factory (like :attributes => :name, for a natural key).
23
- #
24
- def key(name_or_factory = :uuid, *options)
25
- self.key_factory = case name_or_factory
26
- when :uuid
27
- UUIDKeyFactory.new
28
- when :natural
29
- NaturalKeyFactory.new(*options)
30
- when :custom
31
- CustomKeyFactory.new(*options)
32
- else
33
- name_or_factory
34
- end
8
+ key do
9
+ SimpleUUID::UUID.new.to_guid
35
10
  end
11
+ end
36
12
 
37
- def next_key(object = nil)
38
- key_factory.next_key(object).tap do |key|
39
- raise "Keys may not be nil" if key.nil?
40
- end
13
+ module ClassMethods
14
+ # Define a key generator. Default is UUID.
15
+ def key(&block)
16
+ self.key_generator = block
41
17
  end
42
18
 
43
- def parse_key(string)
44
- key_factory.parse(string)
19
+ def _generate_key(object)
20
+ object.instance_eval(&key_generator)
45
21
  end
46
22
  end
47
-
48
- def key
49
- @key ||= self.class.next_key(self)
50
- end
51
23
  end
52
24
  end
@@ -33,7 +33,7 @@ module CassandraObject
33
33
 
34
34
  def instantiate(key, attributes)
35
35
  allocate.tap do |object|
36
- object.instance_variable_set("@key", parse_key(key)) if key
36
+ object.instance_variable_set("@id", key) if key
37
37
  object.instance_variable_set("@new_record", false)
38
38
  object.instance_variable_set("@destroyed", false)
39
39
  object.instance_variable_set("@attributes", typecast_attributes(object, attributes))
@@ -82,7 +82,7 @@ module CassandraObject
82
82
  end
83
83
 
84
84
  def destroy
85
- self.class.remove(key)
85
+ self.class.remove(id)
86
86
  @destroyed = true
87
87
  freeze
88
88
  end
@@ -104,7 +104,7 @@ module CassandraObject
104
104
  end
105
105
 
106
106
  def reload
107
- @attributes.update(self.class.find(self.id).instance_variable_get('@attributes'))
107
+ @attributes.update(self.class.find(id).instance_variable_get('@attributes'))
108
108
  end
109
109
 
110
110
  private
@@ -116,7 +116,7 @@ module CassandraObject
116
116
  def create
117
117
  write
118
118
  @new_record = false
119
- key
119
+ id
120
120
  end
121
121
 
122
122
  def update
@@ -125,7 +125,7 @@ module CassandraObject
125
125
 
126
126
  def write
127
127
  changed_attributes = changed.inject({}) { |h, n| h[n] = read_attribute(n); h }
128
- self.class.write(key, changed_attributes)
128
+ self.class.write(id, changed_attributes)
129
129
  end
130
130
  end
131
131
  end
@@ -1,17 +1,21 @@
1
1
  require 'test_helper'
2
2
 
3
- class CassandraObject::AttributeMethods::PrimaryTest < CassandraObject::TestCase
3
+ class CassandraObject::AttributeMethods::PrimaryKeyTest < CassandraObject::TestCase
4
4
  test 'get id' do
5
- issue = Issue.new
5
+ model = temp_object do
6
+ key do
7
+ "foo"
8
+ end
9
+ end
10
+ record = model.new
6
11
 
7
- assert_equal issue.key.to_s, issue.id
12
+ assert_equal 'foo', record.id
8
13
  end
9
14
 
10
15
  test 'set id' do
11
- uuid = SimpleUUID::UUID.new.to_guid
12
- issue = Issue.new id: uuid
16
+ issue = Issue.new id: 'foo'
13
17
 
14
- assert_equal issue.key.to_s, uuid
18
+ assert_equal 'foo', issue.id
15
19
  end
16
20
 
17
21
  test 'attributes' do
@@ -4,7 +4,6 @@ class CassandraObject::FinderMethodsTest < CassandraObject::TestCase
4
4
  test 'find' do
5
5
  Issue.create.tap do |issue|
6
6
  assert_equal issue, Issue.find(issue.id)
7
- assert_equal issue, Issue.find(issue.key)
8
7
  end
9
8
 
10
9
  begin
@@ -47,7 +46,7 @@ class CassandraObject::FinderMethodsTest < CassandraObject::TestCase
47
46
  third_issue = Issue.create
48
47
 
49
48
  assert_equal [], Issue.find_with_ids([])
50
- assert_equal [first_issue, second_issue].to_set, Issue.find_with_ids(first_issue.key, second_issue.key).to_set
51
- assert_equal [first_issue, second_issue].to_set, Issue.find_with_ids([first_issue.key, second_issue.key]).to_set
49
+ assert_equal [first_issue, second_issue].to_set, Issue.find_with_ids(first_issue.id, second_issue.id).to_set
50
+ assert_equal [first_issue, second_issue].to_set, Issue.find_with_ids([first_issue.id, second_issue.id]).to_set
52
51
  end
53
52
  end
@@ -5,18 +5,22 @@ class CassandraObject::IdentityTest < CassandraObject::TestCase
5
5
  assert_equal 'id', Issue.primary_key
6
6
  end
7
7
 
8
- test 'get key' do
8
+ test 'default _generate_key' do
9
9
  issue = Issue.new
10
10
 
11
- assert_not_nil issue.key
11
+ assert_not_nil Issue._generate_key(issue)
12
12
  end
13
13
 
14
- test 'parse_key' do
15
- assert_kind_of(
16
- CassandraObject::Identity::UUIDKeyFactory::UUID,
17
- Issue.parse_key('bb4cbbbc-b7c7-11e0-9ca2-732604ff41fe')
18
- )
14
+ test 'custom key' do
15
+ model = temp_object do
16
+ key do
17
+ "name:#{name}"
18
+ end
19
+ attr_accessor :name
20
+ end
21
+ record = model.new
22
+ record.name = 'bar'
19
23
 
20
- assert_nil Issue.parse_key('fail')
24
+ assert_equal 'name:bar', model._generate_key(record)
21
25
  end
22
26
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gotime-cassandra_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.11.9
4
+ version: 2.12.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-05-16 00:00:00.000000000 Z
13
+ date: 2012-05-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activemodel
@@ -108,11 +108,6 @@ files:
108
108
  - lib/cassandra_object/generators/migration_generator.rb
109
109
  - lib/cassandra_object/generators/templates/migration.rb.erb
110
110
  - lib/cassandra_object/identity.rb
111
- - lib/cassandra_object/identity/abstract_key_factory.rb
112
- - lib/cassandra_object/identity/custom_key_factory.rb
113
- - lib/cassandra_object/identity/hashed_natural_key_factory.rb
114
- - lib/cassandra_object/identity/natural_key_factory.rb
115
- - lib/cassandra_object/identity/uuid_key_factory.rb
116
111
  - lib/cassandra_object/inspect.rb
117
112
  - lib/cassandra_object/log_subscriber.rb
118
113
  - lib/cassandra_object/migrations.rb
@@ -160,7 +155,6 @@ files:
160
155
  - test/unit/connection_test.rb
161
156
  - test/unit/consistency_test.rb
162
157
  - test/unit/finder_methods_test.rb
163
- - test/unit/identity/uuid_key_factory_test.rb
164
158
  - test/unit/identity_test.rb
165
159
  - test/unit/inspect_test.rb
166
160
  - test/unit/persistence_test.rb
@@ -1,26 +0,0 @@
1
- module CassandraObject
2
- module Identity
3
- # Key factories need to support 3 operations
4
- class AbstractKeyFactory
5
- # Next key takes an object and returns the key object it should use.
6
- # object will be ignored with synthetic keys but could be useful with natural ones
7
- #
8
- # @param [CassandraObject::Base] the object that needs a new key
9
- # @return [CassandraObject::Identity::Key] the key
10
- #
11
- def next_key(object)
12
- raise NotImplementedError, "#{self.class.name}#next_key isn't implemented."
13
- end
14
-
15
- # Parse should create a new key object from the 'to_param' format
16
- #
17
- # @param [String] the result of calling key.to_param
18
- # @return [CassandraObject::Identity::Key] the parsed key
19
- #
20
- def parse(string)
21
- raise NotImplementedError, "#{self.class.name}#parse isn't implemented."
22
- end
23
- end
24
- end
25
- end
26
-
@@ -1,36 +0,0 @@
1
- module CassandraObject
2
- module Identity
3
- class CustomKeyFactory < AbstractKeyFactory
4
- class CustomKey
5
- attr_reader :value
6
-
7
- def initialize(value)
8
- @value = value
9
- end
10
-
11
- def to_s
12
- value
13
- end
14
-
15
- def ==(other)
16
- other.to_s == value
17
- end
18
- end
19
-
20
- attr_reader :method
21
-
22
- def initialize(options)
23
- @method = options[:method]
24
- end
25
-
26
- def next_key(object)
27
- CustomKey.new(object.send(@method))
28
- end
29
-
30
- def parse(value)
31
- value
32
- end
33
- end
34
- end
35
- end
36
-
@@ -1,10 +0,0 @@
1
- require 'digest/sha1'
2
- module CassandraObject
3
- module Identity
4
- class HashedNaturalKeyFactory < NaturalKeyFactory
5
- def next_key(object)
6
- NaturalKey.new(Digest::SHA1.hexdigest(attributes.map { |attribute| object[attribute] }.join(separator)))
7
- end
8
- end
9
- end
10
- end
@@ -1,37 +0,0 @@
1
- module CassandraObject
2
- module Identity
3
- class NaturalKeyFactory < AbstractKeyFactory
4
- class NaturalKey
5
- attr_reader :value
6
-
7
- def initialize(value)
8
- @value = value
9
- end
10
-
11
- def to_s
12
- value
13
- end
14
-
15
- def ==(other)
16
- other.to_s == to_s
17
- end
18
- end
19
-
20
- attr_reader :attributes, :separator
21
-
22
- def initialize(options)
23
- @attributes = [*options[:attributes]]
24
- @separator = options[:separator] || "-"
25
- end
26
-
27
- def next_key(object)
28
- NaturalKey.new(attributes.map { |a| object.attributes[a.to_s] }.join(separator))
29
- end
30
-
31
- def parse(paramized_key)
32
- NaturalKey.new(paramized_key)
33
- end
34
- end
35
- end
36
- end
37
-
@@ -1,23 +0,0 @@
1
- module CassandraObject
2
- module Identity
3
- # Key factories need to support 3 operations
4
- class UUIDKeyFactory < AbstractKeyFactory
5
- class UUID < SimpleUUID::UUID
6
- def to_s
7
- to_guid
8
- end
9
- end
10
-
11
- def next_key(object)
12
- UUID.new
13
- end
14
-
15
- def parse(string)
16
- UUID.new(string) if string
17
- rescue
18
- nil
19
- end
20
- end
21
- end
22
- end
23
-
@@ -1,14 +0,0 @@
1
- require 'test_helper'
2
-
3
- class CassandraObject::Identity::UuidKeyFactoryTest < CassandraObject::TestCase
4
- test 'parse' do
5
- assert_not_nil key_factory.parse('95b2cf70-c318-11e0-962b-0800200c9a66')
6
- assert_nil key_factory.parse('xyz')
7
- assert_nil key_factory.parse(nil)
8
- end
9
-
10
- private
11
- def key_factory
12
- @key_factory ||= CassandraObject::Identity::UUIDKeyFactory.new
13
- end
14
- end