superstore 2.4.3 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ruby.yml +40 -0
  3. data/.gitignore +1 -0
  4. data/.simplecov +3 -0
  5. data/Gemfile +2 -3
  6. data/LICENSE +1 -1
  7. data/MIT-LICENSE +1 -1
  8. data/README.md +4 -34
  9. data/lib/superstore/adapters/abstract_adapter.rb +1 -27
  10. data/lib/superstore/adapters/jsonb_adapter.rb +4 -132
  11. data/lib/superstore/associations/association.rb +6 -0
  12. data/lib/superstore/associations/association_scope.rb +20 -0
  13. data/lib/superstore/associations/belongs_to.rb +3 -1
  14. data/lib/superstore/associations/builder/association.rb +1 -1
  15. data/lib/superstore/associations/has_many.rb +15 -2
  16. data/lib/superstore/associations/reflection.rb +8 -2
  17. data/lib/superstore/associations.rb +9 -4
  18. data/lib/superstore/attribute_assignment.rb +7 -0
  19. data/lib/superstore/attribute_methods/primary_key.rb +20 -11
  20. data/lib/superstore/attribute_methods.rb +1 -109
  21. data/lib/superstore/attributes.rb +13 -0
  22. data/lib/superstore/base.rb +6 -34
  23. data/lib/superstore/core.rb +7 -65
  24. data/lib/superstore/model_schema.rb +35 -0
  25. data/lib/superstore/persistence.rb +26 -115
  26. data/lib/superstore/railtie.rb +3 -11
  27. data/lib/superstore/relation/scrolling.rb +48 -0
  28. data/lib/superstore/types/array_type.rb +3 -7
  29. data/lib/superstore/types/base.rb +9 -0
  30. data/lib/superstore/types/boolean_type.rb +7 -12
  31. data/lib/superstore/types/date_range_type.rb +7 -0
  32. data/lib/superstore/types/date_type.rb +7 -10
  33. data/lib/superstore/types/float_type.rb +3 -11
  34. data/lib/superstore/types/geo_point_type.rb +30 -0
  35. data/lib/superstore/types/integer_range_type.rb +15 -0
  36. data/lib/superstore/types/integer_type.rb +8 -14
  37. data/lib/superstore/types/json_type.rb +1 -1
  38. data/lib/superstore/types/range_type.rb +58 -0
  39. data/lib/superstore/types/string_type.rb +4 -4
  40. data/lib/superstore/types/time_type.rb +10 -8
  41. data/lib/superstore/types.rb +11 -9
  42. data/lib/superstore.rb +17 -19
  43. data/superstore.gemspec +8 -10
  44. data/test/support/jsonb.rb +3 -1
  45. data/test/support/models.rb +10 -4
  46. data/test/test_helper.rb +7 -3
  47. data/test/unit/adapters/adapter_test.rb +1 -3
  48. data/test/unit/associations/belongs_to_test.rb +1 -1
  49. data/test/unit/associations/has_many_test.rb +10 -2
  50. data/test/unit/attribute_methods/dirty_test.rb +8 -19
  51. data/test/unit/attribute_methods/primary_key_test.rb +1 -1
  52. data/test/unit/attribute_methods_test.rb +10 -22
  53. data/test/unit/{attribute_methods/typecasting_test.rb → attributes_test.rb} +13 -39
  54. data/test/unit/base_test.rb +4 -0
  55. data/test/unit/caching_test.rb +1 -1
  56. data/test/unit/callbacks_test.rb +4 -4
  57. data/test/unit/core_test.rb +15 -20
  58. data/test/unit/persistence_test.rb +36 -44
  59. data/test/unit/{scope/batches_test.rb → relation/scrolling_test.rb} +9 -5
  60. data/test/unit/serialization_test.rb +10 -2
  61. data/test/unit/{timestamps_test.rb → timestamp_test.rb} +5 -5
  62. data/test/unit/types/array_type_test.rb +3 -18
  63. data/test/unit/types/boolean_type_test.rb +7 -21
  64. data/test/unit/types/date_range_type_test.rb +29 -0
  65. data/test/unit/types/date_type_test.rb +15 -6
  66. data/test/unit/types/float_type_test.rb +4 -19
  67. data/test/unit/types/geo_point_type_test.rb +28 -0
  68. data/test/unit/types/integer_range_type_test.rb +28 -0
  69. data/test/unit/types/integer_type_test.rb +7 -16
  70. data/test/unit/types/string_type_test.rb +9 -13
  71. data/test/unit/types/time_type_test.rb +17 -11
  72. data/test/unit/validations_test.rb +2 -2
  73. metadata +39 -53
  74. data/.travis.yml +0 -13
  75. data/Gemfile-rails4.2 +0 -11
  76. data/lib/superstore/attribute_methods/definition.rb +0 -17
  77. data/lib/superstore/attribute_methods/dirty.rb +0 -52
  78. data/lib/superstore/attribute_methods/typecasting.rb +0 -53
  79. data/lib/superstore/caching.rb +0 -13
  80. data/lib/superstore/callbacks.rb +0 -29
  81. data/lib/superstore/connection.rb +0 -24
  82. data/lib/superstore/errors.rb +0 -10
  83. data/lib/superstore/inspect.rb +0 -25
  84. data/lib/superstore/model.rb +0 -38
  85. data/lib/superstore/schema.rb +0 -20
  86. data/lib/superstore/scope/batches.rb +0 -27
  87. data/lib/superstore/scope/finder_methods.rb +0 -51
  88. data/lib/superstore/scope/query_methods.rb +0 -52
  89. data/lib/superstore/scope.rb +0 -73
  90. data/lib/superstore/scoping.rb +0 -30
  91. data/lib/superstore/timestamps.rb +0 -19
  92. data/lib/superstore/type.rb +0 -16
  93. data/lib/superstore/types/base_type.rb +0 -23
  94. data/lib/superstore/validations.rb +0 -44
  95. data/test/unit/attribute_methods/definition_test.rb +0 -16
  96. data/test/unit/inspect_test.rb +0 -26
  97. data/test/unit/schema_test.rb +0 -15
  98. data/test/unit/scope/finder_methods_test.rb +0 -62
  99. data/test/unit/scope/query_methods_test.rb +0 -36
  100. data/test/unit/scoping_test.rb +0 -7
  101. data/test/unit/types/base_type_test.rb +0 -11
@@ -1,73 +0,0 @@
1
- require 'superstore/scope/batches'
2
- require 'superstore/scope/finder_methods'
3
- require 'superstore/scope/query_methods'
4
-
5
- module Superstore
6
- class Scope
7
- include Batches, FinderMethods, QueryMethods
8
-
9
- attr_accessor :klass
10
- attr_accessor :limit_value, :select_values, :where_values, :id_values, :order_values
11
-
12
- def initialize(klass)
13
- @klass = klass
14
-
15
- @limit_value = nil
16
- @select_values = []
17
- @where_values = []
18
- @order_values = []
19
- @id_values = []
20
-
21
- reset
22
- end
23
-
24
- def initialize_copy(other)
25
- @limit_value = @limit_value.dup if @limit_value
26
- @select_values = @select_values.dup
27
- @where_values = @where_values.dup
28
- @order_values = @order_values.dup
29
- @id_values = @id_values.dup
30
- reset
31
- end
32
-
33
- def reset
34
- @records = nil
35
- @loaded = false
36
- end
37
-
38
- private
39
-
40
- def scoping
41
- previous, klass.current_scope = klass.current_scope, self
42
- yield
43
- ensure
44
- klass.current_scope = previous
45
- end
46
-
47
- def method_missing(method_name, *args, &block)
48
- if klass.respond_to?(method_name)
49
- scoping { klass.send(method_name, *args, &block) }
50
- elsif Array.method_defined?(method_name)
51
- to_a.send(method_name, *args, &block)
52
- else
53
- super
54
- end
55
- end
56
-
57
- def to_a
58
- unless @loaded
59
- @records = select_records
60
- @loaded = true
61
- end
62
- @records
63
- end
64
-
65
- def select_records
66
- results = []
67
- klass.adapter.select(self) do |key, attributes|
68
- results << klass.instantiate(key, attributes)
69
- end
70
- results
71
- end
72
- end
73
- end
@@ -1,30 +0,0 @@
1
- module Superstore
2
- module Scoping
3
- extend ActiveSupport::Concern
4
-
5
- included do
6
- singleton_class.class_eval do
7
- delegate :find, :find_by_id, :first, :all, :to_ids, to: :scope
8
- delegate :find_each, :find_in_batches, to: :scope
9
- delegate :select, :where, :where_ids, to: :scope
10
- end
11
-
12
- class_attribute :default_scopes, instance_writer: false, instance_predicate: false
13
- self.default_scopes = []
14
- end
15
-
16
- module ClassMethods
17
- def scope
18
- self.current_scope ||= Scope.new(self)
19
- end
20
-
21
- def current_scope
22
- Thread.current["#{self}_current_scope"]
23
- end
24
-
25
- def current_scope=(new_scope)
26
- Thread.current["#{self}_current_scope"] = new_scope
27
- end
28
- end
29
- end
30
- end
@@ -1,19 +0,0 @@
1
- module Superstore
2
- module Timestamps
3
- extend ActiveSupport::Concern
4
-
5
- included do
6
- attribute :created_at, type: :time
7
- attribute :updated_at, type: :time
8
-
9
- before_create do
10
- self.created_at ||= Time.current
11
- self.updated_at ||= Time.current
12
- end
13
-
14
- before_update if: :changed? do
15
- self.updated_at = Time.current unless self.changed_attributes.key?("updated_at")
16
- end
17
- end
18
- end
19
- end
@@ -1,16 +0,0 @@
1
- module Superstore
2
- class Type
3
- cattr_accessor :attribute_types
4
- self.attribute_types = {}.with_indifferent_access
5
-
6
- class << self
7
- def register(name, type)
8
- attribute_types[name] = type
9
- end
10
-
11
- def get_type_class(name)
12
- attribute_types[name]
13
- end
14
- end
15
- end
16
- end
@@ -1,23 +0,0 @@
1
- module Superstore
2
- module Types
3
- class BaseType
4
- attr_accessor :model, :options
5
- def initialize(model, options = {})
6
- @model = model
7
- @options = options
8
- end
9
-
10
- def encode(value)
11
- value
12
- end
13
-
14
- def decode(str)
15
- str
16
- end
17
-
18
- def typecast(value)
19
- value
20
- end
21
- end
22
- end
23
- end
@@ -1,44 +0,0 @@
1
- module Superstore
2
- class RecordInvalid < StandardError
3
- attr_reader :record
4
- def initialize(record)
5
- @record = record
6
- super("Invalid record: #{@record.errors.full_messages.to_sentence}")
7
- end
8
- end
9
-
10
- module Validations
11
- extend ActiveSupport::Concern
12
- include ActiveModel::Validations
13
-
14
- included do
15
- define_callbacks :validate, scope: :name
16
- end
17
-
18
- module ClassMethods
19
- def create!(attributes = {})
20
- new(attributes).tap do |object|
21
- object.save!
22
- end
23
- end
24
- end
25
-
26
- def save(options={})
27
- if perform_validations(options)
28
- super
29
- true
30
- else
31
- false
32
- end
33
- end
34
-
35
- def save!
36
- save || raise(RecordInvalid.new(self))
37
- end
38
-
39
- protected
40
- def perform_validations(options={})
41
- (options[:validate] != false) ? valid? : true
42
- end
43
- end
44
- end
@@ -1,16 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Superstore::AttributeMethods::DefinitionTest < Superstore::TestCase
4
- class TestType < Superstore::Types::BaseType
5
- def typecast(v)
6
- "#{v}-foo"
7
- end
8
- end
9
-
10
- test 'initialize' do
11
- definition = Superstore::AttributeMethods::Definition.new(Issue, :foo, TestType, {})
12
-
13
- assert_equal 'foo', definition.name
14
- assert_kind_of TestType, definition.type
15
- end
16
- end
@@ -1,26 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Superstore::InspectTest < Superstore::TestCase
4
- test 'attribute_for_inspect' do
5
- object = temp_object do
6
- string :long_string
7
- time :the_time
8
- integer :other
9
- end
10
-
11
- assert_equal "#{'x' * 51}...".inspect, object.new.attribute_for_inspect('x' * 100)
12
- assert_equal "2012-02-14 12:01:02".inspect, object.new.attribute_for_inspect(Time.new(2012, 02, 14, 12, 01, 02))
13
- assert_equal "\"foo\"", object.new.attribute_for_inspect('foo')
14
- end
15
-
16
- test 'inspect' do
17
- object = temp_object do
18
- string :description
19
- integer :price
20
- end.new(description: "yeah buddy", price: nil)
21
-
22
- assert_match(/id/, object.inspect)
23
- assert_match(/description/, object.inspect)
24
- assert_no_match(/price/, object.inspect)
25
- end
26
- end
@@ -1,15 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Superstore::SchemaTest < Superstore::TestCase
4
- # SELECT columnfamily_name FROM System.schema_columnfamilies WHERE keyspace_name='myKeyspaceName';
5
- # test "create_table" do
6
- # Superstore::Schema.create_table 'TestRecords'
7
- #
8
- # begin
9
- # Superstore::Schema.create_table 'TestRecords'
10
- # assert false, 'TestRecords should already exist'
11
- # rescue Exception => e
12
- # end
13
- # end
14
-
15
- end
@@ -1,62 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Superstore::FinderMethodsTest < Superstore::TestCase
4
- test 'find' do
5
- Issue.create.tap do |issue|
6
- assert_equal issue, Issue.find(issue.id)
7
- end
8
-
9
- begin
10
- Issue.find(nil)
11
- assert false
12
- rescue => e
13
- assert_equal "Couldn't find Issue with key nil", e.message
14
- end
15
-
16
- assert_raise Superstore::RecordNotFound do
17
- Issue.find('what')
18
- end
19
- end
20
-
21
- test 'find with ids' do
22
- first_issue = Issue.create
23
- second_issue = Issue.create
24
- Issue.create
25
-
26
- assert_equal [], Issue.find([])
27
- assert_equal [first_issue, second_issue], Issue.find([first_issue.id, second_issue.id])
28
- end
29
-
30
- test 'find with nil ids' do
31
- Issue.create
32
- assert_equal [], Issue.find([nil])
33
- end
34
-
35
- test 'find_by_id' do
36
- Issue.create.tap do |issue|
37
- assert_equal issue, Issue.find_by_id(issue.id)
38
- end
39
-
40
- assert_nil Issue.find_by_id('what')
41
- end
42
-
43
- test 'all' do
44
- first_issue = Issue.create
45
- second_issue = Issue.create
46
-
47
- assert_equal [first_issue, second_issue].to_set, Issue.all.to_set
48
- end
49
-
50
- test 'first' do
51
- first_issue = Issue.create
52
- second_issue = Issue.create
53
-
54
- assert [first_issue, second_issue].include?(Issue.first)
55
- end
56
-
57
- test 'to_ids' do
58
- first_issue = Issue.create title: 'Sup!'
59
-
60
- assert_equal [first_issue.id], Issue.to_ids
61
- end
62
- end
@@ -1,36 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Superstore::Scope::QueryMethodsTest < Superstore::TestCase
4
- test "select" do
5
- original_issue = Issue.create title: 'foo', description: 'bar'
6
- found_issue = Issue.select(:title).find(original_issue.id)
7
-
8
- assert_equal 'foo', found_issue.title
9
- assert_equal original_issue.id, found_issue.id
10
- assert_nil found_issue.description
11
- end
12
-
13
- test "where" do
14
- foo_issue = Issue.create title: 'foo'
15
- bar_issue = Issue.create title: 'bar'
16
- nil_issue = Issue.create title: nil
17
-
18
- assert_equal [foo_issue], Issue.where(title: 'foo').to_a
19
- assert_equal [foo_issue, bar_issue], Issue.where(title: ['foo', 'bar']).to_a
20
- assert_equal [nil_issue], Issue.where(title: nil).to_a
21
- end
22
-
23
- test "select with block" do
24
- foo_issue = Issue.create title: 'foo'
25
- Issue.create title: 'bar'
26
-
27
- assert_equal [foo_issue], Issue.select { |issue| issue.title == 'foo' }
28
- end
29
-
30
- test "chaining where with scope" do
31
- issue = Issue.create title: 'abc', description: 'def'
32
- query = Issue.select(:title).for_key(issue.id)
33
-
34
- assert_equal [:title], query.select_values
35
- end
36
- end
@@ -1,7 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Superstore::ScopingTest < Superstore::TestCase
4
- test "scope" do
5
- assert_kind_of Superstore::Scope, Issue.scope
6
- end
7
- end
@@ -1,11 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Superstore::Types::BaseTypeTest < Superstore::Types::TestCase
4
- test 'encode' do
5
- assert_equal 'x', type.encode('x')
6
- end
7
-
8
- test 'decode' do
9
- assert_equal 'abc', type.decode('abc')
10
- end
11
- end