mr 0.35.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/Gemfile +13 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/bench/all.rb +4 -0
- data/bench/factory.rb +68 -0
- data/bench/fake_record.rb +174 -0
- data/bench/model.rb +201 -0
- data/bench/read_model.rb +191 -0
- data/bench/results/factory.txt +21 -0
- data/bench/results/fake_record.txt +37 -0
- data/bench/results/model.txt +44 -0
- data/bench/results/read_model.txt +46 -0
- data/bench/setup.rb +132 -0
- data/lib/mr.rb +11 -0
- data/lib/mr/after_commit.rb +49 -0
- data/lib/mr/after_commit/fake_record.rb +39 -0
- data/lib/mr/after_commit/record.rb +48 -0
- data/lib/mr/after_commit/record_procs_methods.rb +82 -0
- data/lib/mr/factory.rb +82 -0
- data/lib/mr/factory/config.rb +240 -0
- data/lib/mr/factory/model_factory.rb +103 -0
- data/lib/mr/factory/model_stack.rb +28 -0
- data/lib/mr/factory/read_model_factory.rb +104 -0
- data/lib/mr/factory/record_factory.rb +130 -0
- data/lib/mr/factory/record_stack.rb +219 -0
- data/lib/mr/fake_query.rb +53 -0
- data/lib/mr/fake_record.rb +58 -0
- data/lib/mr/fake_record/associations.rb +257 -0
- data/lib/mr/fake_record/attributes.rb +168 -0
- data/lib/mr/fake_record/persistence.rb +116 -0
- data/lib/mr/json_field.rb +180 -0
- data/lib/mr/json_field/fake_record.rb +31 -0
- data/lib/mr/json_field/record.rb +38 -0
- data/lib/mr/model.rb +67 -0
- data/lib/mr/model/associations.rb +161 -0
- data/lib/mr/model/configuration.rb +67 -0
- data/lib/mr/model/fields.rb +177 -0
- data/lib/mr/model/persistence.rb +79 -0
- data/lib/mr/query.rb +126 -0
- data/lib/mr/read_model.rb +83 -0
- data/lib/mr/read_model/data.rb +38 -0
- data/lib/mr/read_model/fields.rb +218 -0
- data/lib/mr/read_model/query_expression.rb +188 -0
- data/lib/mr/read_model/querying.rb +214 -0
- data/lib/mr/read_model/set_querying.rb +82 -0
- data/lib/mr/read_model/subquery.rb +98 -0
- data/lib/mr/record.rb +35 -0
- data/lib/mr/test_helpers.rb +229 -0
- data/lib/mr/type_converter.rb +85 -0
- data/lib/mr/version.rb +3 -0
- data/log/.gitkeep +0 -0
- data/mr.gemspec +29 -0
- data/test/helper.rb +21 -0
- data/test/support/db.rb +10 -0
- data/test/support/factory.rb +13 -0
- data/test/support/factory/area.rb +6 -0
- data/test/support/factory/comment.rb +14 -0
- data/test/support/factory/image.rb +6 -0
- data/test/support/factory/user.rb +6 -0
- data/test/support/models/area.rb +58 -0
- data/test/support/models/comment.rb +60 -0
- data/test/support/models/image.rb +53 -0
- data/test/support/models/user.rb +96 -0
- data/test/support/read_model/querying.rb +150 -0
- data/test/support/read_models/comment_with_user_data.rb +27 -0
- data/test/support/read_models/set_data.rb +49 -0
- data/test/support/read_models/subquery_data.rb +41 -0
- data/test/support/read_models/user_with_area_data.rb +15 -0
- data/test/support/schema.rb +39 -0
- data/test/support/setup_test_db.rb +10 -0
- data/test/system/factory/model_factory_tests.rb +87 -0
- data/test/system/factory/model_stack_tests.rb +30 -0
- data/test/system/factory/record_factory_tests.rb +84 -0
- data/test/system/factory/record_stack_tests.rb +51 -0
- data/test/system/factory_tests.rb +32 -0
- data/test/system/read_model_tests.rb +199 -0
- data/test/system/with_model_tests.rb +275 -0
- data/test/unit/after_commit/fake_record_tests.rb +110 -0
- data/test/unit/after_commit/record_procs_methods_tests.rb +177 -0
- data/test/unit/after_commit/record_tests.rb +134 -0
- data/test/unit/after_commit_tests.rb +113 -0
- data/test/unit/factory/config_tests.rb +651 -0
- data/test/unit/factory/model_factory_tests.rb +473 -0
- data/test/unit/factory/model_stack_tests.rb +97 -0
- data/test/unit/factory/read_model_factory_tests.rb +195 -0
- data/test/unit/factory/record_factory_tests.rb +446 -0
- data/test/unit/factory/record_stack_tests.rb +549 -0
- data/test/unit/factory_tests.rb +213 -0
- data/test/unit/fake_query_tests.rb +137 -0
- data/test/unit/fake_record/associations_tests.rb +585 -0
- data/test/unit/fake_record/attributes_tests.rb +265 -0
- data/test/unit/fake_record/persistence_tests.rb +239 -0
- data/test/unit/fake_record_tests.rb +106 -0
- data/test/unit/json_field/fake_record_tests.rb +75 -0
- data/test/unit/json_field/record_tests.rb +80 -0
- data/test/unit/json_field_tests.rb +302 -0
- data/test/unit/model/associations_tests.rb +346 -0
- data/test/unit/model/configuration_tests.rb +92 -0
- data/test/unit/model/fields_tests.rb +278 -0
- data/test/unit/model/persistence_tests.rb +114 -0
- data/test/unit/model_tests.rb +137 -0
- data/test/unit/query_tests.rb +300 -0
- data/test/unit/read_model/data_tests.rb +56 -0
- data/test/unit/read_model/fields_tests.rb +416 -0
- data/test/unit/read_model/query_expression_tests.rb +381 -0
- data/test/unit/read_model/querying_tests.rb +613 -0
- data/test/unit/read_model/set_querying_tests.rb +149 -0
- data/test/unit/read_model/subquery_tests.rb +242 -0
- data/test/unit/read_model_tests.rb +187 -0
- data/test/unit/record_tests.rb +45 -0
- data/test/unit/test_helpers_tests.rb +431 -0
- data/test/unit/type_converter_tests.rb +207 -0
- metadata +285 -0
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module MR
|
4
|
+
|
5
|
+
class TypeConverter
|
6
|
+
TYPES = [
|
7
|
+
:binary,
|
8
|
+
:boolean,
|
9
|
+
:date,
|
10
|
+
:datetime, :timestamp,
|
11
|
+
:decimal,
|
12
|
+
:float,
|
13
|
+
:integer, :primary_key,
|
14
|
+
:string, :text, :slug, :hex, :file_name, :dir_path, :file_path, :path,
|
15
|
+
:url, :email,
|
16
|
+
:time
|
17
|
+
].freeze
|
18
|
+
|
19
|
+
def self.valid?(type)
|
20
|
+
TYPES.include?(type.to_sym)
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(ar_column_class = nil)
|
24
|
+
@ar_column_class = ar_column_class || ActiveRecord::ConnectionAdapters::Column
|
25
|
+
end
|
26
|
+
|
27
|
+
def binary(value)
|
28
|
+
return if value.nil?
|
29
|
+
@ar_column_class.binary_to_string(value)
|
30
|
+
end
|
31
|
+
|
32
|
+
def boolean(value)
|
33
|
+
return if value.nil?
|
34
|
+
@ar_column_class.value_to_boolean(value)
|
35
|
+
end
|
36
|
+
|
37
|
+
def float(value)
|
38
|
+
return if value.nil?
|
39
|
+
value.to_f
|
40
|
+
end
|
41
|
+
|
42
|
+
def date(value)
|
43
|
+
return if value.nil?
|
44
|
+
@ar_column_class.string_to_date(value)
|
45
|
+
end
|
46
|
+
|
47
|
+
def datetime(value)
|
48
|
+
return if value.nil?
|
49
|
+
@ar_column_class.string_to_time(value)
|
50
|
+
end
|
51
|
+
alias :timestamp :datetime
|
52
|
+
|
53
|
+
def decimal(value)
|
54
|
+
return if value.nil?
|
55
|
+
@ar_column_class.value_to_decimal(value)
|
56
|
+
end
|
57
|
+
|
58
|
+
def integer(value)
|
59
|
+
return if value.nil?
|
60
|
+
@ar_column_class.value_to_integer(value)
|
61
|
+
end
|
62
|
+
alias :primary_key :integer
|
63
|
+
|
64
|
+
def string(value)
|
65
|
+
return if value.nil?
|
66
|
+
value.to_s
|
67
|
+
end
|
68
|
+
alias :text :string
|
69
|
+
alias :slug :string
|
70
|
+
alias :hex :string
|
71
|
+
alias :file_name :string
|
72
|
+
alias :dir_path :string
|
73
|
+
alias :file_path :string
|
74
|
+
alias :path :string
|
75
|
+
alias :url :string
|
76
|
+
alias :email :string
|
77
|
+
|
78
|
+
def time(value)
|
79
|
+
return if value.nil?
|
80
|
+
@ar_column_class.string_to_dummy_time(value)
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
data/lib/mr/version.rb
ADDED
data/log/.gitkeep
ADDED
File without changes
|
data/mr.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "mr/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "mr"
|
8
|
+
gem.version = MR::VERSION
|
9
|
+
gem.authors = ["Kelly Redding", "Collin Redding"]
|
10
|
+
gem.email = ["kelly@kellyredding.com", "collin.redding@me.com"]
|
11
|
+
gem.summary = %q{Model-record pattern}
|
12
|
+
gem.description = %q{Model-record pattern}
|
13
|
+
gem.homepage = "https://rubygems.org/gems/mr"
|
14
|
+
gem.license = 'MIT'
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
|
21
|
+
gem.add_development_dependency("ardb", ["~> 0.28.3"])
|
22
|
+
gem.add_development_dependency("sqlite3", [" = 1.3.9"])
|
23
|
+
|
24
|
+
gem.add_dependency("activerecord", ["~> 3.2"])
|
25
|
+
gem.add_dependency("activesupport", ["~> 3.2"])
|
26
|
+
gem.add_dependency("assert", ["~> 2.16.3"])
|
27
|
+
gem.add_dependency("much-plugin", ["~> 0.2.0"])
|
28
|
+
|
29
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# this file is automatically required when you run `assert`
|
2
|
+
# put any test helpers here
|
3
|
+
|
4
|
+
# add the root dir to the load path
|
5
|
+
$LOAD_PATH.unshift(File.expand_path("../..", __FILE__))
|
6
|
+
|
7
|
+
# require pry for debugging (`binding.pry`)
|
8
|
+
require 'pry'
|
9
|
+
|
10
|
+
require 'test/support/factory'
|
11
|
+
|
12
|
+
require 'json' # so the default encoder/decoder procs will work
|
13
|
+
|
14
|
+
# 1.8.7 backfills
|
15
|
+
|
16
|
+
# Array#sample
|
17
|
+
if !(a = Array.new).respond_to?(:sample) && a.respond_to?(:choice)
|
18
|
+
class Array
|
19
|
+
alias_method :sample, :choice
|
20
|
+
end
|
21
|
+
end
|
data/test/support/db.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'assert/factory'
|
2
|
+
require 'mr/read_model/query_expression'
|
3
|
+
|
4
|
+
module Factory
|
5
|
+
extend Assert::Factory
|
6
|
+
|
7
|
+
def self.read_model_set_expression(operator = nil, &block)
|
8
|
+
operator ||= MR::ReadModel::SetExpression::OPERATOR_SQL.keys.sample
|
9
|
+
block ||= proc{ }
|
10
|
+
MR::ReadModel::SetExpression.new(operator, &block)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'mr/factory'
|
2
|
+
require 'test/support/models/comment'
|
3
|
+
|
4
|
+
module Factory; end
|
5
|
+
Factory::Comment = MR::Factory.new(Comment, CommentRecord) do
|
6
|
+
default_args do
|
7
|
+
set :parent_type, 'UserRecord'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
Factory::FakeComment = MR::Factory.new(Comment, FakeCommentRecord) do
|
11
|
+
default_args do
|
12
|
+
set :parent_type, 'FakeUserRecord'
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'mr'
|
2
|
+
require 'mr/fake_record'
|
3
|
+
require 'test/support/models/user'
|
4
|
+
|
5
|
+
class AreaRecord < ActiveRecord::Base
|
6
|
+
include MR::Record
|
7
|
+
self.table_name = 'areas'
|
8
|
+
|
9
|
+
has_many :users, {
|
10
|
+
:class_name => 'UserRecord',
|
11
|
+
:foreign_key => 'area_id'
|
12
|
+
}
|
13
|
+
|
14
|
+
has_many :benchmark_users, {
|
15
|
+
:class_name => 'UserRecord',
|
16
|
+
:foreign_key => 'benchmark_area_id'
|
17
|
+
}
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
class ValidAreaRecord < AreaRecord
|
22
|
+
validates_presence_of :name
|
23
|
+
end
|
24
|
+
|
25
|
+
class Area
|
26
|
+
include MR::Model
|
27
|
+
record_class AreaRecord
|
28
|
+
|
29
|
+
field_reader :id
|
30
|
+
field_accessor :name, :active, :description, :percentage, :meeting_time
|
31
|
+
|
32
|
+
has_many :users
|
33
|
+
|
34
|
+
has_many :benchmark_users
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
class FakeAreaRecord
|
39
|
+
include MR::FakeRecord
|
40
|
+
model_class Area
|
41
|
+
|
42
|
+
attribute :name, :string
|
43
|
+
attribute :active, :boolean
|
44
|
+
attribute :description, :text
|
45
|
+
attribute :percentage, :float
|
46
|
+
attribute :meeting_time, :time
|
47
|
+
|
48
|
+
has_many :users, {
|
49
|
+
:class_name => 'FakeUserRecord',
|
50
|
+
:foreign_key => 'area_id'
|
51
|
+
}
|
52
|
+
|
53
|
+
has_many :benchmark_users, {
|
54
|
+
:class_name => 'FakeUserRecord',
|
55
|
+
:foreign_key => 'benchmark_area_id'
|
56
|
+
}
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'mr'
|
2
|
+
require 'mr/fake_record'
|
3
|
+
require 'test/support/models/user'
|
4
|
+
|
5
|
+
class CommentRecord < ActiveRecord::Base
|
6
|
+
include MR::Record
|
7
|
+
self.table_name = 'comments'
|
8
|
+
|
9
|
+
belongs_to :parent, {
|
10
|
+
:polymorphic => true
|
11
|
+
}
|
12
|
+
belongs_to :created_by, {
|
13
|
+
:class_name => 'UserRecord',
|
14
|
+
:foreign_key => 'created_by_id'
|
15
|
+
}
|
16
|
+
|
17
|
+
belongs_to :benchmark_parent, {
|
18
|
+
:polymorphic => true
|
19
|
+
}
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
class Comment
|
24
|
+
include MR::Model
|
25
|
+
record_class CommentRecord
|
26
|
+
|
27
|
+
field_reader :id, :parent_id
|
28
|
+
field_accessor :body, :parent_type, :created_at
|
29
|
+
|
30
|
+
polymorphic_belongs_to :parent
|
31
|
+
belongs_to :created_by
|
32
|
+
|
33
|
+
polymorphic_belongs_to :benchmark_parent
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
class FakeCommentRecord
|
38
|
+
include MR::FakeRecord
|
39
|
+
model_class Comment
|
40
|
+
|
41
|
+
attribute :body, :text
|
42
|
+
attribute :parent_type, :string, :null => false
|
43
|
+
attribute :parent_id, :integer, :null => false
|
44
|
+
attribute :created_at, :datetime
|
45
|
+
attribute :created_by_id, :integer
|
46
|
+
|
47
|
+
belongs_to :parent, {
|
48
|
+
:polymorphic => true
|
49
|
+
}
|
50
|
+
belongs_to :created_by, {
|
51
|
+
:class_name => 'FakeUserRecord',
|
52
|
+
:foreign_key => 'created_by_id'
|
53
|
+
}
|
54
|
+
|
55
|
+
belongs_to :benchmark_parent, {
|
56
|
+
:polymorphic => true
|
57
|
+
}
|
58
|
+
|
59
|
+
end
|
60
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'mr'
|
2
|
+
require 'mr/fake_record'
|
3
|
+
require 'test/support/models/user'
|
4
|
+
|
5
|
+
class ImageRecord < ActiveRecord::Base
|
6
|
+
include MR::Record
|
7
|
+
self.table_name = 'images'
|
8
|
+
|
9
|
+
belongs_to :user, {
|
10
|
+
:class_name => 'UserRecord',
|
11
|
+
:foreign_key => 'user_id'
|
12
|
+
}
|
13
|
+
|
14
|
+
belongs_to :benchmark_user, {
|
15
|
+
:class_name => 'UserRecord',
|
16
|
+
:foreign_key => 'benchmark_user_id'
|
17
|
+
}
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
class Image
|
22
|
+
include MR::Model
|
23
|
+
record_class ImageRecord
|
24
|
+
|
25
|
+
field_reader :id, :user_id
|
26
|
+
field_accessor :file_path, :data
|
27
|
+
|
28
|
+
belongs_to :user
|
29
|
+
|
30
|
+
belongs_to :benchmark_user
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
class FakeImageRecord
|
35
|
+
include MR::FakeRecord
|
36
|
+
model_class Image
|
37
|
+
|
38
|
+
attribute :file_path, :string
|
39
|
+
attribute :data, :binary
|
40
|
+
attribute :user_id, :integer, :null => false
|
41
|
+
|
42
|
+
belongs_to :user, {
|
43
|
+
:class_name => 'FakeUserRecord',
|
44
|
+
:foreign_key => 'user_id'
|
45
|
+
}
|
46
|
+
|
47
|
+
belongs_to :benchmark_user, {
|
48
|
+
:class_name => 'FakeUserRecord',
|
49
|
+
:foreign_key => 'benchmark_user_id'
|
50
|
+
}
|
51
|
+
|
52
|
+
end
|
53
|
+
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'mr'
|
2
|
+
require 'mr/fake_record'
|
3
|
+
require 'test/support/models/area'
|
4
|
+
require 'test/support/models/comment'
|
5
|
+
require 'test/support/models/image'
|
6
|
+
|
7
|
+
class UserRecord < ActiveRecord::Base
|
8
|
+
include MR::Record
|
9
|
+
self.table_name = 'users'
|
10
|
+
|
11
|
+
belongs_to :area, {
|
12
|
+
:class_name => 'AreaRecord',
|
13
|
+
:foreign_key => 'area_id'
|
14
|
+
}
|
15
|
+
has_one :image, {
|
16
|
+
:class_name => 'ImageRecord',
|
17
|
+
:foreign_key => 'user_id',
|
18
|
+
:autosave => false
|
19
|
+
}
|
20
|
+
has_many :comments, {
|
21
|
+
:class_name => 'CommentRecord',
|
22
|
+
:as => :parent
|
23
|
+
}
|
24
|
+
|
25
|
+
belongs_to :benchmark_area, {
|
26
|
+
:class_name => 'AreaRecord',
|
27
|
+
:foreign_key => 'benchmark_area_id'
|
28
|
+
}
|
29
|
+
has_one :benchmark_image, {
|
30
|
+
:class_name => 'ImageRecord',
|
31
|
+
:foreign_key => 'benchmark_user_id',
|
32
|
+
:autosave => false
|
33
|
+
}
|
34
|
+
has_many :benchmark_comments, {
|
35
|
+
:class_name => 'CommentRecord',
|
36
|
+
:as => :benchmark_parent
|
37
|
+
}
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
class User
|
42
|
+
include MR::Model
|
43
|
+
record_class UserRecord
|
44
|
+
|
45
|
+
field_reader :id, :area_id
|
46
|
+
field_accessor :name, :number, :salary, :started_on, :dob
|
47
|
+
|
48
|
+
belongs_to :area
|
49
|
+
has_one :image
|
50
|
+
has_many :comments
|
51
|
+
|
52
|
+
belongs_to :benchmark_area
|
53
|
+
has_one :benchmark_image
|
54
|
+
has_many :benchmark_comments
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
class FakeUserRecord
|
59
|
+
include MR::FakeRecord
|
60
|
+
model_class User
|
61
|
+
|
62
|
+
attribute :name, :string
|
63
|
+
attribute :number, :integer
|
64
|
+
attribute :salary, :float
|
65
|
+
attribute :started_on, :date
|
66
|
+
attribute :dob, :timestamp
|
67
|
+
attribute :area_id, :integer, :null => false
|
68
|
+
|
69
|
+
belongs_to :area, {
|
70
|
+
:class_name => 'FakeAreaRecord',
|
71
|
+
:foreign_key => 'area_id'
|
72
|
+
}
|
73
|
+
has_one :image, {
|
74
|
+
:class_name => 'FakeImageRecord',
|
75
|
+
:foreign_key => 'user_id'
|
76
|
+
}
|
77
|
+
has_many :comments, {
|
78
|
+
:class_name => 'FakeCommentRecord',
|
79
|
+
:as => :parent
|
80
|
+
}
|
81
|
+
|
82
|
+
belongs_to :benchmark_area, {
|
83
|
+
:class_name => 'FakeAreaRecord',
|
84
|
+
:foreign_key => 'benchmark_area_id'
|
85
|
+
}
|
86
|
+
has_one :benchmark_image, {
|
87
|
+
:class_name => 'FakeImageRecord',
|
88
|
+
:foreign_key => 'benchmark_user_id'
|
89
|
+
}
|
90
|
+
has_many :benchmark_comments, {
|
91
|
+
:class_name => 'FakeCommentRecord',
|
92
|
+
:as => :benchmark_parent
|
93
|
+
}
|
94
|
+
|
95
|
+
end
|
96
|
+
|