trax_model 0.0.98 → 0.0.99

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.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +4 -0
  3. data/lib/trax/model.rb +6 -16
  4. data/lib/trax/model/attributes.rb +1 -1
  5. data/lib/trax/model/attributes/definitions.rb +4 -0
  6. data/lib/trax/model/attributes/{mixin.rb → dsl.rb} +2 -12
  7. data/lib/trax/model/attributes/types/boolean.rb +1 -2
  8. data/lib/trax/model/attributes/types/enum.rb +0 -2
  9. data/lib/trax/model/attributes/types/integer.rb +2 -2
  10. data/lib/trax/model/attributes/types/set.rb +67 -0
  11. data/lib/trax/model/attributes/types/string.rb +3 -3
  12. data/lib/trax/model/attributes/types/struct.rb +5 -3
  13. data/lib/trax/model/attributes/types/uuid_array.rb +0 -2
  14. data/lib/trax/model/{sti.rb → core_extensions.rb} +2 -2
  15. data/lib/trax/model/core_extensions/string.rb +17 -0
  16. data/lib/trax/model/extensions_for.rb +18 -0
  17. data/lib/trax/model/extensions_for/base.rb +31 -0
  18. data/lib/trax/model/extensions_for/boolean.rb +29 -0
  19. data/lib/trax/model/extensions_for/enumerable.rb +18 -0
  20. data/lib/trax/model/extensions_for/integer.rb +10 -0
  21. data/lib/trax/model/extensions_for/numeric.rb +46 -0
  22. data/lib/trax/model/extensions_for/set.rb +10 -0
  23. data/lib/trax/model/extensions_for/string.rb +30 -0
  24. data/lib/trax/model/extensions_for/struct.rb +191 -0
  25. data/lib/trax/model/extensions_for/struct_fields.rb +21 -0
  26. data/lib/trax/model/extensions_for/struct_fields/boolean.rb +32 -0
  27. data/lib/trax/model/extensions_for/struct_fields/enum.rb +20 -0
  28. data/lib/trax/model/extensions_for/struct_fields/float.rb +12 -0
  29. data/lib/trax/model/extensions_for/struct_fields/integer.rb +12 -0
  30. data/lib/trax/model/extensions_for/struct_fields/numeric.rb +52 -0
  31. data/lib/trax/model/extensions_for/struct_fields/string.rb +29 -0
  32. data/lib/trax/model/extensions_for/struct_fields/time.rb +54 -0
  33. data/lib/trax/model/matchable.rb +3 -10
  34. data/lib/trax/model/mixins.rb +3 -0
  35. data/lib/trax/model/mixins/freezable.rb +31 -0
  36. data/lib/trax/model/mixins/restorable.rb +63 -0
  37. data/lib/trax/model/mixins/sti_enum.rb +4 -5
  38. data/lib/trax/model/mixins/unique_id.rb +85 -0
  39. data/lib/trax/model/railtie.rb +4 -2
  40. data/lib/trax/validators/json_attribute_validator.rb +1 -0
  41. data/lib/trax_model/version.rb +1 -1
  42. data/spec/db/schema/default_tables.rb +1 -17
  43. data/spec/db/schema/pg_tables.rb +15 -0
  44. data/spec/support/models.rb +11 -16
  45. data/spec/support/pg/models.rb +31 -6
  46. data/spec/trax/model/attributes/types/set_spec.rb +68 -0
  47. data/spec/trax/model/attributes/types/struct_spec.rb +6 -0
  48. data/spec/trax/model/extensions_for/boolean_spec.rb +26 -0
  49. data/spec/trax/model/extensions_for/numeric_spec.rb +53 -0
  50. data/spec/trax/model/extensions_for/string_spec.rb +25 -0
  51. data/spec/trax/model/extensions_for/struct_spec.rb +223 -0
  52. data/spec/trax/model/extensions_for_spec.rb +0 -0
  53. data/spec/trax/model/{freezable_spec.rb → mixins/freezable_spec.rb} +1 -1
  54. data/spec/trax/model/{restorable_spec.rb → mixins/restorable_spec.rb} +1 -1
  55. data/spec/trax/model/{unique_id_spec.rb → mixins/unique_id_spec.rb} +1 -1
  56. data/trax_model.gemspec +4 -5
  57. metadata +49 -59
  58. data/lib/trax/model/enum.rb +0 -64
  59. data/lib/trax/model/freezable.rb +0 -29
  60. data/lib/trax/model/mti.rb +0 -11
  61. data/lib/trax/model/mti/abstract.rb +0 -65
  62. data/lib/trax/model/mti/entity.rb +0 -62
  63. data/lib/trax/model/mti/namespace.rb +0 -21
  64. data/lib/trax/model/restorable.rb +0 -61
  65. data/lib/trax/model/scopes.rb +0 -16
  66. data/lib/trax/model/sti/attributes.rb +0 -94
  67. data/lib/trax/model/string_extensions.rb +0 -11
  68. data/lib/trax/model/struct_extensions.rb +0 -185
  69. data/lib/trax/model/unique_id.rb +0 -83
  70. data/spec/trax/model/sti/attributes_spec.rb +0 -15
  71. data/spec/trax/model/struct_extensions_spec.rb +0 -16
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a56cf1df33c14218b82e1c7b26c649c52adb18df
4
- data.tar.gz: e12109ca5ea6fb9abccb869c7c67d87431614f9c
3
+ metadata.gz: aff3b6b4389df830349e17d6b8ed2a378effd6af
4
+ data.tar.gz: c2e220e45e53c87812eabf18c772db0067bd9f90
5
5
  SHA512:
6
- metadata.gz: dc8eb0602e23a03e13a87daf5420f3ea51f1dab49708cee5128c242e175609f795a852185f60c2b26f8a250d42c7a2873a75a8b9c1d8007111cd5afcb6fe338a
7
- data.tar.gz: ced26e12c60dcff2b03ccb64bfb1c0bae10e526eb9cb2e9748be3a528e67db668634f47d1ac5f6f4e5b6feca75cf39e1390677c415ba7f8b3365ff3c13fe0708
6
+ metadata.gz: 6c808f53684e7ed77a9509597cd2d8c4b11d52313f6e7246de544aa54db24d83695a7780331afa6626c32f8552e6c8a786132e0e7226347b56c43247d70c5341
7
+ data.tar.gz: 91bcf1d6bf4f70155ac262f58f82b38fc15aff0dca066cccc901d93028c1b24cb739c0f582e6575354e264d21afecb10c92eb4368ae90c9382d0cf70a7b47fb8
data/Gemfile CHANGED
@@ -1,3 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ gem 'hashie', :github => "jasonayre/hashie"
4
+ # gem 'trax_core', :github => "jasonayre/trax_core"
5
+ # gem 'trax_core', :path => "~/gems/trax_core"
6
+
3
7
  gemspec
@@ -4,7 +4,6 @@ require 'hashie/dash'
4
4
  require 'hashie/mash'
5
5
  require 'hashie/trash'
6
6
  require 'hashie/extensions/dash/indifferent_access'
7
- require 'simple_enum'
8
7
  require_relative './validators/boolean_validator'
9
8
  require_relative './validators/email_validator'
10
9
  require_relative './validators/frozen_validator'
@@ -24,23 +23,16 @@ module Trax
24
23
 
25
24
  autoload :Attributes
26
25
  autoload :Config
27
- autoload :Enum
26
+ autoload :CoreExtensions
27
+ autoload :ExtensionsFor
28
28
  autoload :Errors
29
- autoload :Freezable
30
29
  autoload :Registry
31
30
  autoload :UUID
32
31
  autoload :UUIDPrefix
33
- autoload :UniqueId
34
32
  autoload :Matchable
35
33
  autoload :Mixin
36
34
  autoload :Mixins
37
- autoload :MTI
38
- autoload :Restorable
39
35
  autoload :Railtie
40
- autoload :STI
41
- autoload :StringExtensions
42
- autoload :Struct
43
- autoload :StructExtensions
44
36
  autoload :Validators
45
37
 
46
38
  include ::Trax::Model::Matchable
@@ -77,15 +69,13 @@ module Trax
77
69
  end
78
70
 
79
71
  def self.eager_autoload_mixins!
80
- ::Trax::Model::Attributes::Mixin
81
- ::Trax::Model::Enum
82
- ::Trax::Model::Freezable
83
- ::Trax::Model::Restorable
84
- ::Trax::Model::UniqueId
85
72
  ::Trax::Model::Mixins::FieldScopes
73
+ ::Trax::Model::Mixins::Freezable
86
74
  ::Trax::Model::Mixins::IdScopes
75
+ ::Trax::Model::Mixins::Restorable
87
76
  ::Trax::Model::Mixins::SortByScopes
88
77
  ::Trax::Model::Mixins::StiEnum
78
+ ::Trax::Model::Mixins::UniqueId
89
79
  end
90
80
 
91
81
  eager_autoload_mixins!
@@ -157,7 +147,7 @@ module Trax
157
147
  end
158
148
  end
159
149
 
160
- ::String.include(::Trax::Model::StringExtensions)
150
+ ::String.include(::Trax::Model::CoreExtensions::String)
161
151
 
162
152
  ::ActiveSupport.run_load_hooks(:trax_model, self)
163
153
  end
@@ -6,8 +6,8 @@ module Trax
6
6
  extend ::ActiveSupport::Autoload
7
7
 
8
8
  autoload :Attribute
9
- autoload :Mixin
10
9
  autoload :Definitions
10
+ autoload :Dsl
11
11
  autoload :Errors
12
12
  autoload :Fields
13
13
  autoload :Types
@@ -26,6 +26,10 @@ module Trax
26
26
  attribute(*args, type: :integer, **options, &block)
27
27
  end
28
28
 
29
+ def set(*args, **options, &block)
30
+ attribute(*args, :type => :set, **options, &block)
31
+ end
32
+
29
33
  def string(*args, **options, &block)
30
34
  attribute(*args, :type => :string, **options, &block)
31
35
  end
@@ -1,15 +1,9 @@
1
1
  module Trax
2
2
  module Model
3
3
  module Attributes
4
- module Mixin
4
+ module Dsl
5
5
  extend ::Trax::Core::Concern
6
6
 
7
- included do
8
- ::Trax::Model::Attributes.config.attribute_types.each_pair do |key, mod|
9
- include mod::Mixin if mod.const_defined?("Mixin")
10
- end
11
- end
12
-
13
7
  after_included do
14
8
  evaluate_attribute_definitions_blocks
15
9
  end
@@ -45,11 +39,7 @@ module Trax
45
39
  def trax_attribute(name, type:, **options, &block)
46
40
  raise ::Trax::Model::Attributes::Errors::UnknownAttributeType.new(type: type) unless ::Trax::Model::Attributes.key?(type)
47
41
 
48
- if ::Trax::Model::Attributes[type].const_defined?("Mixin")
49
- attribute_type_definition_method = ::Trax::Model::Attributes[type]::Mixin::ClassMethods.instance_methods.first
50
- self.send(attribute_type_definition_method, name, **options, &block)
51
- self.validates(name, options[:validates]) if options.key?(:validates)
52
- elsif ::Trax::Model::Attributes[type].respond_to?(:define_attribute)
42
+ if ::Trax::Model::Attributes[type].respond_to?(:define_attribute)
53
43
  ::Trax::Model::Attributes[type].define_attribute(self, name, **options, &block)
54
44
  self.validates(name, options[:validates]) if options.key?(:validates)
55
45
  else
@@ -1,5 +1,3 @@
1
- require 'hashie/extensions/ignore_undeclared'
2
-
3
1
  module Trax
4
2
  module Model
5
3
  module Attributes
@@ -20,6 +18,7 @@ module Trax
20
18
  end
21
19
 
22
20
  class Attribute < ::Trax::Model::Attributes::Attribute
21
+ include ::Trax::Model::ExtensionsFor::Boolean
23
22
  self.type = :boolean
24
23
 
25
24
  def self.to_schema
@@ -1,5 +1,3 @@
1
- require 'hashie/extensions/ignore_undeclared'
2
-
3
1
  module Trax
4
2
  module Model
5
3
  module Attributes
@@ -1,5 +1,3 @@
1
- require 'hashie/extensions/ignore_undeclared'
2
-
3
1
  module Trax
4
2
  module Model
5
3
  module Attributes
@@ -18,6 +16,8 @@ module Trax
18
16
  end
19
17
 
20
18
  class Value < ::Trax::Model::Attributes::Value
19
+ include ::Trax::Model::ExtensionsFor::Integer
20
+
21
21
  def self.type; :integer end;
22
22
  end
23
23
 
@@ -0,0 +1,67 @@
1
+ module Trax
2
+ module Model
3
+ module Attributes
4
+ module Types
5
+ class Set < ::Trax::Model::Attributes::Type
6
+ def self.define_attribute(klass, attribute_name, **options, &block)
7
+ klass_name = "#{klass.fields_module.name.underscore}/#{attribute_name}".camelize
8
+ attribute_klass = if options.key?(:extend)
9
+ _klass_prototype = options[:extend].is_a?(::String) ? options[:extend].safe_constantize : options[:extend]
10
+ _klass = ::Trax::Core::NamedClass.new(klass_name, _klass_prototype, :parent_definition => klass, &block)
11
+ _klass
12
+ else
13
+ ::Trax::Core::NamedClass.new(klass_name, Value, :parent_definition => klass, &block)
14
+ end
15
+
16
+ klass.attribute(attribute_name, typecaster_klass.new(target_klass: attribute_klass))
17
+ klass.default_value_for(attribute_name) { attribute_klass.new }
18
+ end
19
+
20
+ class Value < ::Trax::Model::Attributes::Value
21
+ include ::Trax::Model::ExtensionsFor::Set
22
+
23
+ def initialize(*args)
24
+ @value = ::Set.new(*args)
25
+ end
26
+ end
27
+
28
+ class TypeCaster < ActiveRecord::Type::Value
29
+ include ::ActiveRecord::Type::Mutable
30
+
31
+ def initialize(*args, target_klass:)
32
+ super(*args)
33
+
34
+ @target_klass = target_klass
35
+ end
36
+
37
+ def type
38
+ :set
39
+ end
40
+
41
+ def type_cast_from_user(value)
42
+ case value.class.name
43
+ when "Array", "Set"
44
+ @target_klass.new(value)
45
+ when @target_klass.name
46
+ @target_klass
47
+ else
48
+ @target_klass.new
49
+ end
50
+ end
51
+
52
+ def type_cast_from_database(value)
53
+ value.present? ? @target_klass.new(::JSON.parse(value)) : value
54
+ end
55
+
56
+ def type_cast_for_database(value)
57
+ value.try(:to_json)
58
+ end
59
+ end
60
+
61
+ self.value_klass = ::Trax::Model::Attributes::Types::Set::Value
62
+ self.typecaster_klass = ::Trax::Model::Attributes::Types::Set::TypeCaster
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -1,5 +1,3 @@
1
- require 'hashie/extensions/ignore_undeclared'
2
-
3
1
  module Trax
4
2
  module Model
5
3
  module Attributes
@@ -18,6 +16,8 @@ module Trax
18
16
  end
19
17
 
20
18
  class Value < ::Trax::Model::Attributes::Value
19
+ include ::Trax::Model::ExtensionsFor::String
20
+
21
21
  def self.type; :string end;
22
22
  end
23
23
 
@@ -29,7 +29,7 @@ module Trax
29
29
  end
30
30
 
31
31
  def type_cast_from_user(value)
32
- value.is_a?(@target_klass) ? @target_klass : @target_klass.new(value || {})
32
+ value.is_a?(@target_klass) ? @target_klass : @target_klass.new(value)
33
33
  end
34
34
 
35
35
  def type_cast_from_database(value)
@@ -10,7 +10,7 @@ module Trax
10
10
  attribute_klass = if options.key?(:extend)
11
11
  _klass_prototype = options[:extend].is_a?(::String) ? options[:extend].safe_constantize : options[:extend]
12
12
  _klass = ::Trax::Core::NamedClass.new(klass_name, _klass_prototype, :parent_definition => klass, &block)
13
- _klass.include(::Trax::Model::StructExtensions)
13
+ _klass.include(::Trax::Model::ExtensionsFor::Struct)
14
14
  _klass
15
15
  else
16
16
  ::Trax::Core::NamedClass.new(klass_name, Value, :parent_definition => klass, &block)
@@ -23,7 +23,7 @@ module Trax
23
23
  end
24
24
 
25
25
  class Value < ::Trax::Core::Types::Struct
26
- include ::Trax::Model::StructExtensions
26
+ include ::Trax::Model::ExtensionsFor::Struct
27
27
  end
28
28
 
29
29
  class TypeCaster < ActiveRecord::Type::Value
@@ -48,7 +48,7 @@ module Trax
48
48
  end
49
49
 
50
50
  def type_cast_for_database(value)
51
- value.present? ? value.to_serializable_hash.to_json : nil
51
+ value.present? ? value.to_serializable_hash.to_json : {}.to_json
52
52
  end
53
53
  end
54
54
 
@@ -82,6 +82,8 @@ module Trax
82
82
  end
83
83
 
84
84
  model.delegate(getter_method, :to => attribute_name)
85
+
86
+ model.attribute_names << _property.to_s unless model.attribute_names.include?(_property.to_s)
85
87
  end
86
88
  end
87
89
  end
@@ -1,5 +1,3 @@
1
- require 'hashie/extensions/ignore_undeclared'
2
-
3
1
  module Trax
4
2
  module Model
5
3
  module Attributes
@@ -1,9 +1,9 @@
1
1
  module Trax
2
2
  module Model
3
- module STI
3
+ module CoreExtensions
4
4
  extend ::ActiveSupport::Autoload
5
5
 
6
- autoload :Attributes
6
+ autoload :String
7
7
  end
8
8
  end
9
9
  end
@@ -0,0 +1,17 @@
1
+ module Trax
2
+ module Model
3
+ module CoreExtensions
4
+ module String
5
+ extend ::ActiveSupport::Concern
6
+
7
+ def to_matchable
8
+ "%#{self.strip}%"
9
+ end
10
+
11
+ def uuid
12
+ ::Trax::Model::UUID === self ? ::Trax::Model::UUID.new(self) : nil
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ #trax model attribute dsl/trax core type extensions
2
+ module Trax
3
+ module Model
4
+ module ExtensionsFor
5
+ extend ::ActiveSupport::Autoload
6
+
7
+ autoload :Base
8
+ autoload :Boolean
9
+ autoload :Enumerable
10
+ autoload :Integer
11
+ autoload :Numeric
12
+ autoload :Set
13
+ autoload :Struct
14
+ autoload :StructFields
15
+ autoload :String
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,31 @@
1
+ module Trax
2
+ module Model
3
+ module ExtensionsFor
4
+ module Base
5
+ extend ::ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def field_name
9
+ name.demodulize.underscore
10
+ end
11
+
12
+ def model_class
13
+ @model_class ||= begin
14
+ model_class_for_property(self)
15
+ end
16
+ end
17
+
18
+ def model_class_for_property(property_klass)
19
+ result = if property_klass.parent_definition.ancestors.include?(::ActiveRecord::Base)
20
+ property_klass.parent_definition
21
+ else
22
+ model_class_for_property(property_klass.parent_definition)
23
+ end
24
+
25
+ result
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,29 @@
1
+ module Trax
2
+ module Model
3
+ module ExtensionsFor
4
+ module Boolean
5
+ extend ::ActiveSupport::Concern
6
+ include ::Trax::Model::ExtensionsFor::Base
7
+
8
+ module ClassMethods
9
+ def eq(*_scope_values)
10
+ _scope_values.flat_compact_uniq!
11
+ model_class.where({field_name => _scope_values})
12
+ end
13
+
14
+ def is_nil
15
+ eq(nil)
16
+ end
17
+
18
+ def is_false
19
+ eq(false)
20
+ end
21
+
22
+ def is_true
23
+ eq(true)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,18 @@
1
+ module Trax
2
+ module Model
3
+ module ExtensionsFor
4
+ module Enumerable
5
+ extend ::ActiveSupport::Concern
6
+
7
+ include ::Trax::Model::ExtensionsFor::Base
8
+
9
+ module ClassMethods
10
+ def contains(*_values)
11
+ _values.flat_compact_uniq!
12
+ model_class.where("#{field_name} ?| array[:values]", :values => _values)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ module Trax
2
+ module Model
3
+ module ExtensionsFor
4
+ module Integer
5
+ extend ::ActiveSupport::Concern
6
+ include ::Trax::Model::ExtensionsFor::Numeric
7
+ end
8
+ end
9
+ end
10
+ end