superstore 2.4.3 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/ruby.yml +40 -0
- data/.gitignore +1 -0
- data/.simplecov +3 -0
- data/Gemfile +2 -3
- data/LICENSE +1 -1
- data/MIT-LICENSE +1 -1
- data/README.md +4 -34
- data/lib/superstore/adapters/abstract_adapter.rb +1 -27
- data/lib/superstore/adapters/jsonb_adapter.rb +4 -132
- data/lib/superstore/associations/association.rb +6 -0
- data/lib/superstore/associations/association_scope.rb +20 -0
- data/lib/superstore/associations/belongs_to.rb +3 -1
- data/lib/superstore/associations/builder/association.rb +1 -1
- data/lib/superstore/associations/has_many.rb +15 -2
- data/lib/superstore/associations/reflection.rb +8 -2
- data/lib/superstore/associations.rb +9 -4
- data/lib/superstore/attribute_assignment.rb +7 -0
- data/lib/superstore/attribute_methods/primary_key.rb +20 -11
- data/lib/superstore/attribute_methods.rb +1 -109
- data/lib/superstore/attributes.rb +13 -0
- data/lib/superstore/base.rb +6 -34
- data/lib/superstore/core.rb +7 -65
- data/lib/superstore/model_schema.rb +35 -0
- data/lib/superstore/persistence.rb +26 -115
- data/lib/superstore/railtie.rb +3 -11
- data/lib/superstore/relation/scrolling.rb +48 -0
- data/lib/superstore/types/array_type.rb +3 -7
- data/lib/superstore/types/base.rb +9 -0
- data/lib/superstore/types/boolean_type.rb +7 -12
- data/lib/superstore/types/date_range_type.rb +7 -0
- data/lib/superstore/types/date_type.rb +7 -10
- data/lib/superstore/types/float_type.rb +3 -11
- data/lib/superstore/types/geo_point_type.rb +30 -0
- data/lib/superstore/types/integer_range_type.rb +15 -0
- data/lib/superstore/types/integer_type.rb +8 -14
- data/lib/superstore/types/json_type.rb +1 -1
- data/lib/superstore/types/range_type.rb +58 -0
- data/lib/superstore/types/string_type.rb +4 -4
- data/lib/superstore/types/time_type.rb +10 -8
- data/lib/superstore/types.rb +11 -9
- data/lib/superstore.rb +17 -19
- data/superstore.gemspec +8 -10
- data/test/support/jsonb.rb +3 -1
- data/test/support/models.rb +10 -4
- data/test/test_helper.rb +7 -3
- data/test/unit/adapters/adapter_test.rb +1 -3
- data/test/unit/associations/belongs_to_test.rb +1 -1
- data/test/unit/associations/has_many_test.rb +10 -2
- data/test/unit/attribute_methods/dirty_test.rb +8 -19
- data/test/unit/attribute_methods/primary_key_test.rb +1 -1
- data/test/unit/attribute_methods_test.rb +10 -22
- data/test/unit/{attribute_methods/typecasting_test.rb → attributes_test.rb} +13 -39
- data/test/unit/base_test.rb +4 -0
- data/test/unit/caching_test.rb +1 -1
- data/test/unit/callbacks_test.rb +4 -4
- data/test/unit/core_test.rb +15 -20
- data/test/unit/persistence_test.rb +36 -44
- data/test/unit/{scope/batches_test.rb → relation/scrolling_test.rb} +9 -5
- data/test/unit/serialization_test.rb +10 -2
- data/test/unit/{timestamps_test.rb → timestamp_test.rb} +5 -5
- data/test/unit/types/array_type_test.rb +3 -18
- data/test/unit/types/boolean_type_test.rb +7 -21
- data/test/unit/types/date_range_type_test.rb +29 -0
- data/test/unit/types/date_type_test.rb +15 -6
- data/test/unit/types/float_type_test.rb +4 -19
- data/test/unit/types/geo_point_type_test.rb +28 -0
- data/test/unit/types/integer_range_type_test.rb +28 -0
- data/test/unit/types/integer_type_test.rb +7 -16
- data/test/unit/types/string_type_test.rb +9 -13
- data/test/unit/types/time_type_test.rb +17 -11
- data/test/unit/validations_test.rb +2 -2
- metadata +39 -53
- data/.travis.yml +0 -13
- data/Gemfile-rails4.2 +0 -11
- data/lib/superstore/attribute_methods/definition.rb +0 -17
- data/lib/superstore/attribute_methods/dirty.rb +0 -52
- data/lib/superstore/attribute_methods/typecasting.rb +0 -53
- data/lib/superstore/caching.rb +0 -13
- data/lib/superstore/callbacks.rb +0 -29
- data/lib/superstore/connection.rb +0 -24
- data/lib/superstore/errors.rb +0 -10
- data/lib/superstore/inspect.rb +0 -25
- data/lib/superstore/model.rb +0 -38
- data/lib/superstore/schema.rb +0 -20
- data/lib/superstore/scope/batches.rb +0 -27
- data/lib/superstore/scope/finder_methods.rb +0 -51
- data/lib/superstore/scope/query_methods.rb +0 -52
- data/lib/superstore/scope.rb +0 -73
- data/lib/superstore/scoping.rb +0 -30
- data/lib/superstore/timestamps.rb +0 -19
- data/lib/superstore/type.rb +0 -16
- data/lib/superstore/types/base_type.rb +0 -23
- data/lib/superstore/validations.rb +0 -44
- data/test/unit/attribute_methods/definition_test.rb +0 -16
- data/test/unit/inspect_test.rb +0 -26
- data/test/unit/schema_test.rb +0 -15
- data/test/unit/scope/finder_methods_test.rb +0 -62
- data/test/unit/scope/query_methods_test.rb +0 -36
- data/test/unit/scoping_test.rb +0 -7
- data/test/unit/types/base_type_test.rb +0 -11
data/lib/superstore/scope.rb
DELETED
@@ -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
|
data/lib/superstore/scoping.rb
DELETED
@@ -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
|
data/lib/superstore/type.rb
DELETED
@@ -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
|
data/test/unit/inspect_test.rb
DELETED
@@ -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
|
data/test/unit/schema_test.rb
DELETED
@@ -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
|
data/test/unit/scoping_test.rb
DELETED