mr 0.35.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/Gemfile +13 -0
  4. data/LICENSE +22 -0
  5. data/README.md +29 -0
  6. data/bench/all.rb +4 -0
  7. data/bench/factory.rb +68 -0
  8. data/bench/fake_record.rb +174 -0
  9. data/bench/model.rb +201 -0
  10. data/bench/read_model.rb +191 -0
  11. data/bench/results/factory.txt +21 -0
  12. data/bench/results/fake_record.txt +37 -0
  13. data/bench/results/model.txt +44 -0
  14. data/bench/results/read_model.txt +46 -0
  15. data/bench/setup.rb +132 -0
  16. data/lib/mr.rb +11 -0
  17. data/lib/mr/after_commit.rb +49 -0
  18. data/lib/mr/after_commit/fake_record.rb +39 -0
  19. data/lib/mr/after_commit/record.rb +48 -0
  20. data/lib/mr/after_commit/record_procs_methods.rb +82 -0
  21. data/lib/mr/factory.rb +82 -0
  22. data/lib/mr/factory/config.rb +240 -0
  23. data/lib/mr/factory/model_factory.rb +103 -0
  24. data/lib/mr/factory/model_stack.rb +28 -0
  25. data/lib/mr/factory/read_model_factory.rb +104 -0
  26. data/lib/mr/factory/record_factory.rb +130 -0
  27. data/lib/mr/factory/record_stack.rb +219 -0
  28. data/lib/mr/fake_query.rb +53 -0
  29. data/lib/mr/fake_record.rb +58 -0
  30. data/lib/mr/fake_record/associations.rb +257 -0
  31. data/lib/mr/fake_record/attributes.rb +168 -0
  32. data/lib/mr/fake_record/persistence.rb +116 -0
  33. data/lib/mr/json_field.rb +180 -0
  34. data/lib/mr/json_field/fake_record.rb +31 -0
  35. data/lib/mr/json_field/record.rb +38 -0
  36. data/lib/mr/model.rb +67 -0
  37. data/lib/mr/model/associations.rb +161 -0
  38. data/lib/mr/model/configuration.rb +67 -0
  39. data/lib/mr/model/fields.rb +177 -0
  40. data/lib/mr/model/persistence.rb +79 -0
  41. data/lib/mr/query.rb +126 -0
  42. data/lib/mr/read_model.rb +83 -0
  43. data/lib/mr/read_model/data.rb +38 -0
  44. data/lib/mr/read_model/fields.rb +218 -0
  45. data/lib/mr/read_model/query_expression.rb +188 -0
  46. data/lib/mr/read_model/querying.rb +214 -0
  47. data/lib/mr/read_model/set_querying.rb +82 -0
  48. data/lib/mr/read_model/subquery.rb +98 -0
  49. data/lib/mr/record.rb +35 -0
  50. data/lib/mr/test_helpers.rb +229 -0
  51. data/lib/mr/type_converter.rb +85 -0
  52. data/lib/mr/version.rb +3 -0
  53. data/log/.gitkeep +0 -0
  54. data/mr.gemspec +29 -0
  55. data/test/helper.rb +21 -0
  56. data/test/support/db.rb +10 -0
  57. data/test/support/factory.rb +13 -0
  58. data/test/support/factory/area.rb +6 -0
  59. data/test/support/factory/comment.rb +14 -0
  60. data/test/support/factory/image.rb +6 -0
  61. data/test/support/factory/user.rb +6 -0
  62. data/test/support/models/area.rb +58 -0
  63. data/test/support/models/comment.rb +60 -0
  64. data/test/support/models/image.rb +53 -0
  65. data/test/support/models/user.rb +96 -0
  66. data/test/support/read_model/querying.rb +150 -0
  67. data/test/support/read_models/comment_with_user_data.rb +27 -0
  68. data/test/support/read_models/set_data.rb +49 -0
  69. data/test/support/read_models/subquery_data.rb +41 -0
  70. data/test/support/read_models/user_with_area_data.rb +15 -0
  71. data/test/support/schema.rb +39 -0
  72. data/test/support/setup_test_db.rb +10 -0
  73. data/test/system/factory/model_factory_tests.rb +87 -0
  74. data/test/system/factory/model_stack_tests.rb +30 -0
  75. data/test/system/factory/record_factory_tests.rb +84 -0
  76. data/test/system/factory/record_stack_tests.rb +51 -0
  77. data/test/system/factory_tests.rb +32 -0
  78. data/test/system/read_model_tests.rb +199 -0
  79. data/test/system/with_model_tests.rb +275 -0
  80. data/test/unit/after_commit/fake_record_tests.rb +110 -0
  81. data/test/unit/after_commit/record_procs_methods_tests.rb +177 -0
  82. data/test/unit/after_commit/record_tests.rb +134 -0
  83. data/test/unit/after_commit_tests.rb +113 -0
  84. data/test/unit/factory/config_tests.rb +651 -0
  85. data/test/unit/factory/model_factory_tests.rb +473 -0
  86. data/test/unit/factory/model_stack_tests.rb +97 -0
  87. data/test/unit/factory/read_model_factory_tests.rb +195 -0
  88. data/test/unit/factory/record_factory_tests.rb +446 -0
  89. data/test/unit/factory/record_stack_tests.rb +549 -0
  90. data/test/unit/factory_tests.rb +213 -0
  91. data/test/unit/fake_query_tests.rb +137 -0
  92. data/test/unit/fake_record/associations_tests.rb +585 -0
  93. data/test/unit/fake_record/attributes_tests.rb +265 -0
  94. data/test/unit/fake_record/persistence_tests.rb +239 -0
  95. data/test/unit/fake_record_tests.rb +106 -0
  96. data/test/unit/json_field/fake_record_tests.rb +75 -0
  97. data/test/unit/json_field/record_tests.rb +80 -0
  98. data/test/unit/json_field_tests.rb +302 -0
  99. data/test/unit/model/associations_tests.rb +346 -0
  100. data/test/unit/model/configuration_tests.rb +92 -0
  101. data/test/unit/model/fields_tests.rb +278 -0
  102. data/test/unit/model/persistence_tests.rb +114 -0
  103. data/test/unit/model_tests.rb +137 -0
  104. data/test/unit/query_tests.rb +300 -0
  105. data/test/unit/read_model/data_tests.rb +56 -0
  106. data/test/unit/read_model/fields_tests.rb +416 -0
  107. data/test/unit/read_model/query_expression_tests.rb +381 -0
  108. data/test/unit/read_model/querying_tests.rb +613 -0
  109. data/test/unit/read_model/set_querying_tests.rb +149 -0
  110. data/test/unit/read_model/subquery_tests.rb +242 -0
  111. data/test/unit/read_model_tests.rb +187 -0
  112. data/test/unit/record_tests.rb +45 -0
  113. data/test/unit/test_helpers_tests.rb +431 -0
  114. data/test/unit/type_converter_tests.rb +207 -0
  115. 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
@@ -0,0 +1,3 @@
1
+ module MR
2
+ VERSION = "0.35.2"
3
+ end
File without changes
@@ -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
@@ -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
@@ -0,0 +1,10 @@
1
+ require 'logger'
2
+
3
+ Ardb.configure do |c|
4
+ c.root_path = File.expand_path("../../..", __FILE__)
5
+ c.schema_path = 'test/support/schema'
6
+ c.logger = Logger.new(File.open("log/db.log", 'w'))
7
+
8
+ c.adapter = 'sqlite3'
9
+ c.database = ':memory:'
10
+ end
@@ -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,6 @@
1
+ require 'mr/factory'
2
+ require 'test/support/models/area'
3
+
4
+ module Factory; end
5
+ Factory::Area = MR::Factory.new(Area, AreaRecord)
6
+ Factory::FakeArea = MR::Factory.new(Area, FakeAreaRecord)
@@ -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,6 @@
1
+ require 'mr/factory'
2
+ require 'test/support/models/image'
3
+
4
+ module Factory; end
5
+ Factory::Image = MR::Factory.new(Image, ImageRecord)
6
+ Factory::FakeImage = MR::Factory.new(Image, FakeImageRecord)
@@ -0,0 +1,6 @@
1
+ require 'mr/factory'
2
+ require 'test/support/models/user'
3
+
4
+ module Factory; end
5
+ Factory::User = MR::Factory.new(User, UserRecord)
6
+ Factory::FakeUser = MR::Factory.new(User, FakeUserRecord)
@@ -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
+