gotime-cassandra_object 4.12.1 → 4.12.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 21e6e3bb0dbf786487b89866cc5c57a31e5caf71
4
- data.tar.gz: c4191773adbef400bd499bcb6fe142dd2b39fcc4
3
+ metadata.gz: 2bc6183624ab74d725fd7e3987ccf1e94692b18d
4
+ data.tar.gz: 3f566d83342e4ee9732a7ac48edcf37ab753f3bc
5
5
  SHA512:
6
- metadata.gz: 57223e2269db0a93bec23217e3e8bf48463c9d8366a4ba03b3055df1c6789db33ffe67d2cb9b03f96eb81fa887120c9c4f61bddaec147d73386f7032ca1619a7
7
- data.tar.gz: af2457981a4ff1102937d5d576e04d7c4dd08bc15a9ab4e9a87e889406ccf766a6c987f7375023145ee92552c0f8f13977d5ddfcdfdf63865ffd9e513c381944
6
+ metadata.gz: 685060d649e8529db79557cdc23397adf1bb50bde0b22e5dc262d95f223f133470df90ea0b0f2999e9fd0acaa68db2c916b8d55cb43d1f17629d88cab1026843
7
+ data.tar.gz: e8f7b4a16e6b51391d6cb82b56ad5dda6446de2f04201b71ff6cd8e01b532221a986355b8ecbaf01b1977e2ae6f4dfb4a3c122edb9d2fcdbc90fb818ed3cd94e
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'gotime-cassandra_object'
5
- s.version = '4.12.1'
5
+ s.version = '4.12.2'
6
6
  s.description = 'Cassandra ActiveModel'
7
7
  s.summary = 'Cassandra ActiveModel'
8
8
  s.authors = ["Michael Koziarski", "gotime"]
@@ -49,7 +49,14 @@ module CassandraObject
49
49
  end
50
50
 
51
51
  def connection
52
- @connection ||= CassandraCQL::Database.new(config.servers, {keyspace: config.keyspace}, config.thrift_options)
52
+ @connection ||= begin
53
+ thrift_options = (config[:thrift] || {})
54
+ CassandraCQL::Database.new(servers, {keyspace: config[:keyspace]}, thrift_options)
55
+ end
56
+ end
57
+
58
+ def servers
59
+ Array.wrap(config[:servers] || "127.0.0.1:9160")
53
60
  end
54
61
 
55
62
  def execute(statement)
@@ -57,7 +57,6 @@ module CassandraObject
57
57
  # conf = {:adapter=>"postgresql", :encoding=>"unicode", :database=>"axle_place_test", :pool=>5, :username=>"postgres"}
58
58
  # @connection ||= ActiveRecord::Base.postgresql_connection(conf)
59
59
  ActiveRecord::Base.connection
60
- # @connection ||= ActiveRecord::Base.postgresql_connection(config)
61
60
  end
62
61
 
63
62
  def execute(statement)
@@ -114,6 +113,15 @@ module CassandraObject
114
113
  execute stmt
115
114
  end
116
115
 
116
+ def create_table(table_name, options = {})
117
+ connection.execute 'CREATE EXTENSION IF NOT EXISTS hstore'
118
+ ActiveRecord::Migration.create_table table_name, id: false do |t|
119
+ t.string :id, null: false
120
+ t.hstore :attribute_store, null: false
121
+ end
122
+ connection.execute "ALTER TABLE #{table_name} ADD CONSTRAINT #{table_name}_pkey PRIMARY KEY (id)"
123
+ end
124
+
117
125
  def create_ids_where_clause(ids)
118
126
  ids = ids.first if ids.is_a?(Array) && ids.one?
119
127
 
@@ -7,12 +7,12 @@ module CassandraObject
7
7
  class_attribute :attribute_definitions
8
8
  self.attribute_definitions = {}
9
9
 
10
- %w(array boolean date float hash_t integer json string time).each do |type|
10
+ %w(array boolean date float integer json string time).each do |type|
11
11
  instance_eval <<-EOV, __FILE__, __LINE__ + 1
12
12
  def #{type}(*args)
13
13
  options = args.extract_options!
14
14
  args.each do |name|
15
- attribute(name, options.merge(:type => :#{type == 'hash_t' ? 'hash' : type}))
15
+ attribute(name, options.merge(:type => :#{type}))
16
16
  end
17
17
  end
18
18
  EOV
@@ -21,9 +21,17 @@ module CassandraObject
21
21
 
22
22
  module ClassMethods
23
23
  def adapter
24
- @@adapter ||= begin
25
- CassandraObject::Adapters::CassandraAdapter.new(config)
26
- # CassandraObject::Adapters::HstoreAdapter.new(config)
24
+ @@adapter ||= adapter_class.new(config)
25
+ end
26
+
27
+ def adapter_class
28
+ case config[:adapter]
29
+ when 'hstore'
30
+ CassandraObject::Adapters::HstoreAdapter
31
+ when nil, 'cassandra'
32
+ CassandraObject::Adapters::CassandraAdapter
33
+ else
34
+ raise "Unknown adapter #{config[:adapter]}"
27
35
  end
28
36
  end
29
37
  end
@@ -31,7 +31,7 @@ module CassandraObject
31
31
  end
32
32
 
33
33
  def cql
34
- @@cql ||= CassandraCQL::Database.new(config.servers, {keyspace: config.keyspace}, config.thrift_options)
34
+ @@cql ||= CassandraObject::Base.adapter.connection
35
35
  end
36
36
 
37
37
  def execute_cql(cql_string, *bind_vars)
@@ -13,7 +13,7 @@ module CassandraObject
13
13
  end
14
14
 
15
15
  def config=(config)
16
- @@config = config.is_a?(Hash) ? CassandraObject::Config.new(config) : config
16
+ @@config = config.deep_symbolize_keys
17
17
  end
18
18
 
19
19
  def config
@@ -10,12 +10,10 @@ module CassandraObject
10
10
  'strategy_options:replication_factor' => 1
11
11
  }
12
12
 
13
- def create_keyspace(keyspace, options = {})
13
+ def create_keyspace(keyspace, options = nil)
14
14
  stmt = "CREATE KEYSPACE #{keyspace}"
15
15
 
16
- if options.empty?
17
- options = DEFAULT_CREATE_KEYSPACE
18
- end
16
+ options ||= DEFAULT_CREATE_KEYSPACE
19
17
 
20
18
  system_execute statement_with_options(stmt, options)
21
19
  end
@@ -25,7 +23,11 @@ module CassandraObject
25
23
  end
26
24
 
27
25
  def create_column_family(column_family, options = {})
28
- stmt = "CREATE COLUMNFAMILY #{column_family} " +
26
+ create_table column_family, options
27
+ end
28
+
29
+ def create_table(table_name, options = {})
30
+ stmt = "CREATE COLUMNFAMILY #{table_name} " +
29
31
  "(KEY varchar PRIMARY KEY)"
30
32
 
31
33
  keyspace_execute statement_with_options(stmt, options)
@@ -64,11 +66,11 @@ module CassandraObject
64
66
  end
65
67
 
66
68
  def db
67
- @db ||= CassandraCQL::Database.new(CassandraObject::Base.config.servers, {keyspace: 'system'}, {connect_timeout: 30, timeout: 30})
69
+ @db ||= CassandraCQL::Database.new(CassandraObject::Base.adapter.servers, {keyspace: 'system'}, {connect_timeout: 30, timeout: 30})
68
70
  end
69
71
 
70
72
  def keyspace_execute(cql)
71
- db.execute "USE #{CassandraObject::Base.config.keyspace}"
73
+ db.execute "USE #{CassandraObject::Base.config[:keyspace]}"
72
74
  db.execute cql
73
75
  end
74
76
 
@@ -37,11 +37,11 @@ module CassandraObject
37
37
  end
38
38
 
39
39
  def keyspace
40
- CassandraObject::Base.config.keyspace
40
+ CassandraObject::Base.config[:keyspace]
41
41
  end
42
42
 
43
43
  def server
44
- CassandraObject::Base.config.servers.first.gsub(/:.*/, '')
44
+ CassandraObject::Base.adapter.servers.first.gsub(/:.*/, '')
45
45
  end
46
46
  end
47
47
  end
@@ -1,11 +1,11 @@
1
1
  ks_namespace = namespace :ks do
2
- desc 'Create the keyspace in cassandra_config/cassandra.yml for the current environment'
2
+ desc 'Create the keyspace in config/cassandra.yml for the current environment'
3
3
  task create: :environment do
4
4
  begin
5
- CassandraObject::Schema.create_keyspace cassandra_config.keyspace, cassandra_config.keyspace_options
5
+ CassandraObject::Schema.create_keyspace CassandraObject::Base.config[:keyspace], CassandraObject::Base.config[:keyspace_options]
6
6
  rescue Exception => e
7
7
  if e.message =~ /conflicts/
8
- p "Keyspace #{cassandra_config.keyspace} already exists"
8
+ p "Keyspace #{CassandraObject::Base.config[:keyspace]} already exists"
9
9
  else
10
10
  raise e
11
11
  end
@@ -14,10 +14,10 @@ ks_namespace = namespace :ks do
14
14
 
15
15
  task drop: :environment do
16
16
  begin
17
- CassandraObject::Schema.drop_keyspace cassandra_config.keyspace
17
+ CassandraObject::Schema.drop_keyspace CassandraObject::Base.config[:keyspace]
18
18
  rescue Exception => e
19
19
  if e.message =~ /non existing keyspace/
20
- p "Keyspace #{cassandra_config.keyspace} does not exist"
20
+ p "Keyspace #{CassandraObject::Base.config[:keyspace]} does not exist"
21
21
  else
22
22
  raise e
23
23
  end
@@ -51,12 +51,4 @@ ks_namespace = namespace :ks do
51
51
  task :_load do
52
52
  ks_namespace["structure:load"].invoke
53
53
  end
54
-
55
- private
56
- def cassandra_config
57
- @cassandra_config ||= begin
58
- cassandra_configs = YAML.load_file(Rails.root.join('config', 'cassandra.yml'))
59
- CassandraObject::Config.new cassandra_configs[Rails.env || 'development']
60
- end
61
- end
62
54
  end
@@ -2,7 +2,6 @@ CassandraObject::Type.register(:array, CassandraObject::Types::ArrayTyp
2
2
  CassandraObject::Type.register(:boolean, CassandraObject::Types::BooleanType)
3
3
  CassandraObject::Type.register(:date, CassandraObject::Types::DateType)
4
4
  CassandraObject::Type.register(:float, CassandraObject::Types::FloatType)
5
- CassandraObject::Type.register(:hash, CassandraObject::Types::HashType)
6
5
  CassandraObject::Type.register(:integer, CassandraObject::Types::IntegerType)
7
6
  CassandraObject::Type.register(:json, CassandraObject::Types::JsonType)
8
7
  CassandraObject::Type.register(:time, CassandraObject::Types::TimeType)
@@ -10,7 +10,6 @@ module CassandraObject
10
10
  autoload :Base
11
11
  autoload :BelongsTo
12
12
  autoload :Callbacks
13
- autoload :Config
14
13
  autoload :Connection
15
14
  autoload :Core
16
15
  autoload :CounterBase
@@ -61,7 +60,6 @@ module CassandraObject
61
60
  autoload :BooleanType
62
61
  autoload :DateType
63
62
  autoload :FloatType
64
- autoload :HashType
65
63
  autoload :IntegerType
66
64
  autoload :JsonType
67
65
  autoload :StringType
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: 4.12.1
4
+ version: 4.12.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Koziarski
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-22 00:00:00.000000000 Z
12
+ date: 2014-02-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -97,7 +97,6 @@ files:
97
97
  - lib/cassandra_object/belongs_to/builder.rb
98
98
  - lib/cassandra_object/belongs_to/reflection.rb
99
99
  - lib/cassandra_object/callbacks.rb
100
- - lib/cassandra_object/config.rb
101
100
  - lib/cassandra_object/connection.rb
102
101
  - lib/cassandra_object/core.rb
103
102
  - lib/cassandra_object/counter_base.rb
@@ -126,7 +125,6 @@ files:
126
125
  - lib/cassandra_object/types/boolean_type.rb
127
126
  - lib/cassandra_object/types/date_type.rb
128
127
  - lib/cassandra_object/types/float_type.rb
129
- - lib/cassandra_object/types/hash_type.rb
130
128
  - lib/cassandra_object/types/integer_type.rb
131
129
  - lib/cassandra_object/types/json_type.rb
132
130
  - lib/cassandra_object/types/string_type.rb
@@ -147,7 +145,6 @@ files:
147
145
  - test/unit/belongs_to/reflection_test.rb
148
146
  - test/unit/belongs_to_test.rb
149
147
  - test/unit/callbacks_test.rb
150
- - test/unit/config_test.rb
151
148
  - test/unit/connection_test.rb
152
149
  - test/unit/core_test.rb
153
150
  - test/unit/counter_base_test.rb
@@ -168,7 +165,6 @@ files:
168
165
  - test/unit/types/boolean_type_test.rb
169
166
  - test/unit/types/date_type_test.rb
170
167
  - test/unit/types/float_type_test.rb
171
- - test/unit/types/hash_type_test.rb
172
168
  - test/unit/types/integer_type_test.rb
173
169
  - test/unit/types/json_type_test.rb
174
170
  - test/unit/types/string_type_test.rb
@@ -1,15 +0,0 @@
1
- require 'active_support/core_ext/hash/keys'
2
-
3
- module CassandraObject
4
- class Config
5
- attr_accessor :servers, :keyspace, :thrift_options, :keyspace_options
6
-
7
- def initialize(options)
8
- options = options.symbolize_keys
9
- self.servers = Array.wrap(options[:servers] || "127.0.0.1:9160")
10
- self.keyspace = options[:keyspace]
11
- self.thrift_options = (options[:thrift] || {}).symbolize_keys
12
- self.keyspace_options = (options[:keyspace_options] || {}).symbolize_keys
13
- end
14
- end
15
- end
@@ -1,52 +0,0 @@
1
- module CassandraObject
2
- module Types
3
- class HashType < BaseType
4
- class DirtyHash < Hash
5
- attr_accessor :record, :name, :options
6
- def initialize(record, name, hash, options)
7
- @record = record
8
- @name = name.to_s
9
- @options = options
10
-
11
- self.merge!(hash)
12
- @init_hash = self.hash
13
- @init_value = hash
14
- end
15
-
16
- def []=(obj, val)
17
- modifying { super }
18
- end
19
-
20
- def delete(obj)
21
- modifying { super }
22
- end
23
-
24
- private
25
- def modifying
26
- result = yield
27
-
28
- if !record.changed_attributes.key?(name) && @init_hash != self.hash
29
- record.changed_attributes[name] = @init_value
30
- end
31
-
32
- record.send("#{name}=", self)
33
-
34
- result
35
- end
36
- end
37
-
38
- def encode(hash)
39
- ActiveSupport::JSON.encode(hash)
40
- end
41
-
42
- def decode(str)
43
- ActiveSupport::JSON.decode(str)
44
- end
45
-
46
- def wrap(record, name, value)
47
- DirtyHash.new(record, name, Hash(value), options)
48
- end
49
-
50
- end
51
- end
52
- end
@@ -1,23 +0,0 @@
1
- require 'test_helper'
2
-
3
- class CassandraObject::ConfigTest < CassandraObject::TestCase
4
- test 'config writer' do
5
- config = CassandraObject::Config.new(
6
- keyspace: 'place_directory_development',
7
- servers: '192.168.0.100:9160',
8
- thrift: {'timeout' => 10},
9
- keyspace_options: {'placement_strategy' => 'NetworkTopologyStrategy'}
10
- )
11
-
12
- assert_equal ['192.168.0.100:9160'], config.servers
13
- assert_equal 'place_directory_development', config.keyspace
14
- assert_equal 10, config.thrift_options[:timeout]
15
- assert_equal 'NetworkTopologyStrategy', config.keyspace_options[:placement_strategy]
16
- end
17
-
18
- test 'defaults' do
19
- config = CassandraObject::Config.new(keyspace: 'widget_factory')
20
-
21
- assert_equal ["127.0.0.1:9160"], config.servers
22
- end
23
- end
@@ -1,77 +0,0 @@
1
- require 'test_helper'
2
-
3
- class CassandraObject::Types::HashTypeTest < CassandraObject::Types::TestCase
4
- test 'encode' do
5
- assert_equal({a: 'b'}.to_json, coder.encode(a: 'b'))
6
- assert_equal '-3', coder.encode(-3)
7
- end
8
-
9
- test 'decode' do
10
- assert_equal({'a' => 'b'}, coder.decode({'a' => 'b'}.to_json))
11
- end
12
-
13
- test 'setting marks dirty' do
14
- record = temp_object do
15
- string :name
16
- hash_t :stuff
17
-
18
- end.new name: 'abcd', stuff: Hash[a: 1, b: 2]
19
-
20
- record.save!
21
- assert !record.stuff_changed?
22
-
23
- record.stuff[:c] = 3
24
-
25
- assert record.stuff_changed?
26
- assert_equal Hash[a: 1, b: 2, c: 3], record.stuff
27
- end
28
-
29
- test 'hash change marks dirty' do
30
- record = temp_object do
31
- string :name
32
- hash_t :stuff
33
-
34
- end.new name: 'abcd', stuff: Hash[a: 1, b: 2, v: {}]
35
-
36
- record.save!
37
- assert !record.stuff_changed?
38
-
39
- record.stuff[:v][:data] = 69
40
- record.stuff[:v] = record.stuff[:v]
41
-
42
- assert record.stuff_changed?
43
- assert_equal Hash[a: 1, b: 2, v: Hash[data: 69]], record.stuff
44
- end
45
-
46
- test 'hash no change does not dirty' do
47
- record = temp_object do
48
- string :name
49
- hash_t :stuff
50
-
51
- end.new name: 'abcd', stuff: Hash[a: 1, b: 2, v: {data: 69}]
52
-
53
- record.save!
54
- assert !record.stuff_changed?
55
-
56
- record.stuff[:v][:data] = 69
57
- record.stuff[:v] = record.stuff[:v]
58
-
59
- assert !record.stuff_changed?
60
- assert_equal Hash[a: 1, b: 2, v: Hash[data: 69]], record.stuff
61
- end
62
-
63
- test 'delete marks dirty' do
64
- record = temp_object do
65
- string :name
66
- hash_t :stuff
67
-
68
- end.new name: 'abcd', stuff: Hash[a: 1, b: 2]
69
-
70
- record.stuff.delete :b
71
-
72
- assert record.stuff_changed?
73
- assert_equal Hash[a: 1], record.stuff
74
- end
75
-
76
-
77
- end