superstore 1.0.4 → 1.0.5

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: cd5cf2fdffdd1441b0a0663f90ef27127795a75e
4
- data.tar.gz: da3161e0e1f07c0fd2cb2f0f8378bc2c3e3fa679
3
+ metadata.gz: 7bc1d983f476687ecd39026cc3cd006bd371a53e
4
+ data.tar.gz: 8698caa08eaf6a434caa2b32ef18fc151559ccc6
5
5
  SHA512:
6
- metadata.gz: 9eca1be4674e7b6ed593986499bf5daaed8f574794a6488de55a9b7c141c7da3ef50a5ca3d7dad0e6a29717e9b4c93cd0337c437409b44cffa98987c8c9533fc
7
- data.tar.gz: 6eac3d6075b98f8658030c6d1d3aceeeb1e0b3e308ce88af92a9c20580a7d062d388fd849afff147cd134c64da9fb5bd70dd9048213a8f012c7f81f66225dcfb
6
+ metadata.gz: 6f6015ca26eb40b308dd763fb35c508ec6eff5b3e01198c34c0baaad7c655b4a87d7d7979ff319b7a51f5de2bcafbaf92b6721eac328532991c65bac8f00c879
7
+ data.tar.gz: c0dd0e4a5a9e9975184318a223498a77891f7998d2a627bb90e73be4b21ab49653037a9e71a8d5d39615b06c281b581a8930d1196f04c4010a38c2ca7f890526
@@ -11,7 +11,7 @@ module Superstore
11
11
  coder.default
12
12
  end
13
13
 
14
- def instantiate(record, value)
14
+ def instantiate(value)
15
15
  value = value.nil? ? coder.default : value
16
16
  return if value.nil?
17
17
 
@@ -42,9 +42,9 @@ module Superstore
42
42
  attribute_definitions[name.to_s] = AttributeMethods::Definition.new(name, coder, options)
43
43
  end
44
44
 
45
- def typecast_attribute(record, name, value)
45
+ def typecast_attribute(name, value)
46
46
  if attribute_definition = attribute_definitions[name.to_s]
47
- attribute_definition.instantiate(record, value)
47
+ attribute_definition.instantiate(value)
48
48
  else
49
49
  raise NoMethodError, "Unknown attribute #{name.inspect}"
50
50
  end
@@ -40,7 +40,7 @@ module Superstore
40
40
  end
41
41
 
42
42
  def write_attribute(name, value)
43
- @attributes[name.to_s] = self.class.typecast_attribute(self, name, value)
43
+ @attributes[name.to_s] = self.class.typecast_attribute(name, value)
44
44
  end
45
45
 
46
46
  def read_attribute(name)
@@ -67,7 +67,7 @@ module Superstore
67
67
  def typecast_persisted_attributes(object, attributes)
68
68
  attributes.each do |key, value|
69
69
  if definition = attribute_definitions[key]
70
- attributes[key] = definition.instantiate(object, value)
70
+ attributes[key] = definition.instantiate(value)
71
71
  else
72
72
  attributes.delete(key)
73
73
  end
@@ -12,6 +12,7 @@ ks_namespace = namespace :ks do
12
12
  end
13
13
  end
14
14
 
15
+ desc 'Remove the keyspace in config/superstore.yml for the current environment'
15
16
  task drop: :environment do
16
17
  begin
17
18
  Superstore::CassandraSchema.drop_keyspace Superstore::Base.config[:keyspace]
@@ -24,11 +25,14 @@ ks_namespace = namespace :ks do
24
25
  end
25
26
  end
26
27
 
28
+ desc 'Alias for ks:drop and ks:setup'
27
29
  task reset: [:drop, :setup]
28
30
 
31
+ desc 'Alias for ks:create and ks:structure:load'
29
32
  task setup: [:create, :_load]
30
33
 
31
34
  namespace :structure do
35
+ desc 'Serialize the current structure for the keyspace in config/superstore.yml to the SCHEMA environment variable (defaults to "$RAILS_ROOT/ks/structure.cql")'
32
36
  task dump: :environment do
33
37
  filename = ENV['SCHEMA'] || "#{Rails.root}/ks/structure.cql"
34
38
  File.open(filename, "w:utf-8") do |file|
@@ -36,6 +40,7 @@ ks_namespace = namespace :ks do
36
40
  end
37
41
  end
38
42
 
43
+ desc 'Load the structure for the keyspace in config/superstore.yml from the SCHEMA environment variable (defaults to "$RAILS_ROOT/ks/structure.cql")'
39
44
  task load: :environment do
40
45
  filename = ENV['SCHEMA'] || "#{Rails.root}/ks/structure.cql"
41
46
  File.open(filename) do |file|
@@ -1,13 +1,27 @@
1
1
  module Superstore
2
2
  module Types
3
3
  class JsonType < BaseType
4
- def encode(hash)
5
- ActiveSupport::JSON.encode(hash)
4
+ def encode(data)
5
+ ActiveSupport::JSON.encode(data)
6
6
  end
7
7
 
8
8
  def decode(str)
9
9
  ActiveSupport::JSON.decode(str)
10
10
  end
11
+
12
+ def typecast(data)
13
+ return data if ActiveSupport.parse_json_times
14
+
15
+ if data.acts_like?(:time) || data.acts_like?(:date)
16
+ data.as_json
17
+ elsif data.is_a?(Array)
18
+ data.map { |d| typecast(d) }
19
+ elsif data.is_a?(Hash)
20
+ data.each_with_object({}) { |(key, value), hash| hash[key] = typecast(value) }
21
+ else
22
+ data
23
+ end
24
+ end
11
25
  end
12
26
  end
13
27
  end
@@ -2,7 +2,8 @@ module Superstore
2
2
  module Types
3
3
  class TimeType < BaseType
4
4
  def encode(time)
5
- raise ArgumentError.new("#{time.inspect} is not a Time") unless time.kind_of?(Time)
5
+ raise ArgumentError.new("#{time.inspect} does not respond to #to_time") unless time.is_a?(Time) || time.respond_to?(:to_time)
6
+ time = time.to_time unless time.is_a?(Time)
6
7
  time.utc.xmlschema(6)
7
8
  end
8
9
 
data/superstore.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'superstore'
5
- s.version = '1.0.4'
5
+ s.version = '1.0.5'
6
6
  s.description = 'ActiveModel for many attributes'
7
7
  s.summary = 'Cassandra ActiveModel'
8
8
  s.authors = ["Michael Koziarski", "gotime"]
data/test/support/pg.rb CHANGED
@@ -32,6 +32,7 @@ class PGInitializer
32
32
  end
33
33
 
34
34
  PGInitializer.initialize!
35
+ ActiveRecord::Migration.verbose = false
35
36
 
36
37
  module ActiveSupport
37
38
  class TestCase
data/test/test_helper.rb CHANGED
@@ -7,8 +7,8 @@ require 'rails/test_help'
7
7
  require 'mocha/setup'
8
8
 
9
9
  require 'support/pg'
10
- # require 'support/hstore'
11
- require 'support/cassandra'
10
+ require 'support/hstore'
11
+ # require 'support/cassandra'
12
12
  require 'support/issue'
13
13
  require 'support/user'
14
14
 
@@ -51,6 +51,20 @@ class Superstore::AttributeMethods::DirtyTest < Superstore::TestCase
51
51
  assert record.changed?
52
52
  end
53
53
 
54
+ test 'typecast json with times' do
55
+ record = temp_object do
56
+ json :stuff
57
+ end.create(stuff: {'time' => Time.new(2004, 12, 24, 6, 42, 21)})
58
+
59
+ record.reload
60
+
61
+ record.stuff = {'time' => Time.new(2004, 12, 24, 6, 42, 21)}
62
+ assert !record.changed?
63
+
64
+ record.stuff = {'time' => Time.new(2004, 12, 24, 6, 42, 22)}
65
+ assert record.changed?
66
+ end
67
+
54
68
  test 'unapplied_changes' do
55
69
  record = temp_object do
56
70
  float :price
@@ -36,11 +36,11 @@ class Superstore::AttributeMethods::TypecastingTest < Superstore::TestCase
36
36
  end
37
37
 
38
38
  test 'typecast_attribute' do
39
- assert_equal 1, TestIssue.typecast_attribute(TestIssue.new, 'price', 1)
40
- assert_equal 1, TestIssue.typecast_attribute(TestIssue.new, :price, 1)
39
+ assert_equal 1, TestIssue.typecast_attribute('price', 1)
40
+ assert_equal 1, TestIssue.typecast_attribute(:price, 1)
41
41
 
42
42
  assert_raise NoMethodError do
43
- TestIssue.typecast_attribute(TestIssue.new, 'wtf', 1)
43
+ TestIssue.typecast_attribute('wtf', 1)
44
44
  end
45
45
  end
46
46
 
@@ -19,5 +19,9 @@ class Superstore::Types::JsonTypeTest < Superstore::Types::TestCase
19
19
  assert_equal(['a', 'b'], coder.decode(['a', 'b'].to_json))
20
20
  end
21
21
 
22
-
22
+ test 'typecast' do
23
+ assert_equal({'enabled' => false}, coder.typecast('enabled' => false))
24
+ assert_equal({'born_at' => "2004-12-24T00:00:00Z"}, coder.typecast('born_at' => Time.utc(2004, 12, 24)))
25
+ assert_equal(["2004-12-24T00:00:00Z"], coder.typecast([Time.utc(2004, 12, 24)]))
26
+ end
23
27
  end
@@ -3,6 +3,10 @@ require 'test_helper'
3
3
  class Superstore::Types::TimeTypeTest < Superstore::Types::TestCase
4
4
  test 'encode' do
5
5
  assert_equal '2004-12-24T01:02:03.000000Z', coder.encode(Time.utc(2004, 12, 24, 1, 2, 3))
6
+ assert_equal '2004-12-24T01:02:03.000000Z', coder.encode(DateTime.new(2004, 12, 24, 1, 2, 3))
7
+ assert_raise ArgumentError do
8
+ coder.encode 123
9
+ end
6
10
  end
7
11
 
8
12
  test 'decode' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: superstore
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
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-06-23 00:00:00.000000000 Z
12
+ date: 2014-07-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel