trax_core 0.0.77 → 0.0.78
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.
- checksums.yaml +4 -4
- data/lib/trax/core/definitions.rb +1 -19
- data/lib/trax/core/ext/object.rb +1 -1
- data/lib/trax/core/fields.rb +2 -0
- data/lib/trax/core/named_class.rb +2 -2
- data/lib/trax/core/types/array.rb +11 -0
- data/lib/trax/core/types/boolean.rb +11 -0
- data/lib/trax/core/types/enum.rb +3 -11
- data/lib/trax/core/types/enum_value.rb +1 -4
- data/lib/trax/core/types/float.rb +11 -0
- data/lib/trax/core/types/integer.rb +11 -0
- data/lib/trax/core/types/string.rb +11 -0
- data/lib/trax/core/types/struct.rb +55 -45
- data/lib/trax/core/types/value_object.rb +23 -0
- data/lib/trax/core/types.rb +6 -0
- data/lib/trax_core/version.rb +1 -1
- data/spec/support/defs.rb +3 -1
- data/spec/trax/core/definitions_spec.rb +22 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e2b7a8eaa68c5ba0e58ce17e426d9888212bb2d2
|
4
|
+
data.tar.gz: d6c4d99b50c554de6dfc0090f6c2853a4af1882a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3d6f6e388fd4e729b4c012dce5dca53c76e794b25a660beb24b106b7a141b9f8a9cde451903d0fbbce49fcfef3272b859aad9535392e9e67a903e286921e767
|
7
|
+
data.tar.gz: 2ec398604380438ff8414522351a79835077a18870521b8332276383456c54578c37c23ac23232bfb999a40c050fb53ce1bad989c95c103e5bf3b46b938dec95
|
@@ -2,9 +2,7 @@ module Trax
|
|
2
2
|
module Core
|
3
3
|
module Definitions
|
4
4
|
def self.extended(base)
|
5
|
-
base.
|
6
|
-
::Hashie::Mash.new
|
7
|
-
}
|
5
|
+
base.extend(Trax::Core::Fields)
|
8
6
|
end
|
9
7
|
|
10
8
|
def enum(klass_name, **options, &block)
|
@@ -28,22 +26,6 @@ module Trax
|
|
28
26
|
|
29
27
|
attribute_klass
|
30
28
|
end
|
31
|
-
|
32
|
-
def all
|
33
|
-
@all ||= begin
|
34
|
-
constants.map{|const_name| const_get(const_name) }.each_with_object(self._definitions) do |klass, result|
|
35
|
-
result[klass.name.symbolize] = klass
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def values
|
41
|
-
all.values
|
42
|
-
end
|
43
|
-
|
44
|
-
def [](_name)
|
45
|
-
const_get(_name.to_s.camelize)
|
46
|
-
end
|
47
29
|
end
|
48
30
|
end
|
49
31
|
end
|
data/lib/trax/core/ext/object.rb
CHANGED
@@ -60,7 +60,7 @@ class Object
|
|
60
60
|
segs = const_name.split("::")
|
61
61
|
|
62
62
|
raise(::StandardError.new("Set fully qualified constant requires a preexisting namespace to set under")) unless segs.length > 1
|
63
|
-
|
63
|
+
|
64
64
|
as, on = segs.pop, segs.join("::").constantize
|
65
65
|
on.const_set(as, value)
|
66
66
|
end
|
data/lib/trax/core/fields.rb
CHANGED
@@ -2,9 +2,9 @@ module Trax
|
|
2
2
|
module Core
|
3
3
|
class NamedClass
|
4
4
|
def self.new(_name, _parent_klass=Object, **options, &block)
|
5
|
-
klass = ::Object.set_fully_qualified_constant(_name, ::Class.new(_parent_klass)
|
5
|
+
klass = ::Object.set_fully_qualified_constant(_name, ::Class.new(_parent_klass) {
|
6
6
|
define_singleton_method(:name) { _name }
|
7
|
-
|
7
|
+
})
|
8
8
|
|
9
9
|
options.each_pair do |k,v|
|
10
10
|
klass.class_attribute k
|
data/lib/trax/core/types/enum.rb
CHANGED
@@ -1,19 +1,11 @@
|
|
1
|
-
# require 'trax/core/inheritance_hooks'
|
2
|
-
# require 'active_model/attribute_methods'
|
3
1
|
### Examples
|
4
2
|
# ProductCategory < Enum
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
3
|
+
# define :clothing, 1
|
4
|
+
# define :shoes, 2
|
5
|
+
# define :accessories, 3
|
8
6
|
# end
|
9
7
|
# ProductCategory.keys => [:clothing, :shoes, :accessories]
|
10
8
|
|
11
|
-
# StoreYearlyRevenue < Enum
|
12
|
-
# :'0_100000' = 1
|
13
|
-
# :'100000_999999' = 2
|
14
|
-
# :'1000000_99999999' = 3
|
15
|
-
# end
|
16
|
-
|
17
9
|
### Accepts either an integer or the name when setting a value
|
18
10
|
# ProductCategory.new(1) => #{name: :clothing, :value => 1}
|
19
11
|
module Trax
|
@@ -15,7 +15,9 @@ module Trax
|
|
15
15
|
# It defeats the whole purpose of being a 'struct'
|
16
16
|
# if we fail to do so, and it makes our data far more error prone
|
17
17
|
DEFAULT_VALUES_FOR_PROPERTY_TYPES = {
|
18
|
+
:array_property => [],
|
18
19
|
:boolean_property => nil,
|
20
|
+
:float_property => 0.0,
|
19
21
|
:string_property => "",
|
20
22
|
:struct_property => {},
|
21
23
|
:enum_property => nil,
|
@@ -25,7 +27,9 @@ module Trax
|
|
25
27
|
def self.fields_module
|
26
28
|
@fields_module ||= begin
|
27
29
|
module_name = "#{self.name}::Fields"
|
28
|
-
::Trax::Core::NamedModule.new(module_name, ::Trax::Core::Fields)
|
30
|
+
mod = ::Trax::Core::NamedModule.new(module_name, ::Trax::Core::Fields.clone)
|
31
|
+
mod.include(superclass.fields) if superclass.instance_variable_defined?("@fields_module")
|
32
|
+
mod
|
29
33
|
end
|
30
34
|
end
|
31
35
|
|
@@ -33,60 +37,34 @@ module Trax
|
|
33
37
|
fields_module
|
34
38
|
end
|
35
39
|
|
36
|
-
def self.
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
property(name.to_sym, *args, **options)
|
41
|
-
coerce_key(name.to_sym, ->(value) { !!value })
|
40
|
+
def self.array_property(name, *args, of:false, **options, &block)
|
41
|
+
of_object = of && of.is_a?(::String) ? of.safe_constantize : of
|
42
|
+
coercer = of_object ? ::Array[of_object] : ::Array
|
43
|
+
define_attribute_class_for_type(:array, name, *args, :coerce => coercer, **options, &block)
|
42
44
|
end
|
43
45
|
|
44
|
-
def self.
|
45
|
-
name
|
46
|
-
options[:default] = options.key?(:default) ? options[:default] : DEFAULT_VALUES_FOR_PROPERTY_TYPES[__method__]
|
47
|
-
property(name.to_sym, *args, **options)
|
48
|
-
coerce_key(name.to_sym, Integer)
|
46
|
+
def self.boolean_property(name, *args, **options, &block)
|
47
|
+
define_attribute_class_for_type(:boolean, name, *args, :coerce => ->(value){!!value}, **options, &block)
|
49
48
|
end
|
50
49
|
|
51
|
-
def self.
|
52
|
-
name
|
53
|
-
options[:default] = options.key?(:default) ? options[:default] : DEFAULT_VALUES_FOR_PROPERTY_TYPES[__method__]
|
54
|
-
property(name.to_sym, *args, **options)
|
55
|
-
coerce_key(name.to_sym, String)
|
50
|
+
def self.enum_property(name, *args, **options, &block)
|
51
|
+
define_attribute_class_for_type(:enum, name, *args, :coerce => true, **options, &block)
|
56
52
|
end
|
57
53
|
|
58
|
-
def self.
|
59
|
-
name
|
60
|
-
klass_name = "#{fields_module.name.underscore}/#{name}".camelize
|
61
|
-
|
62
|
-
attribute_klass = if options.key?(:extend)
|
63
|
-
_klass_prototype = options[:extend].constantize.clone
|
64
|
-
_klass = ::Trax::Core::NamedClass.new(klass_name, _klass_prototype, :parent_definition => self, &block)
|
65
|
-
_klass
|
66
|
-
else
|
67
|
-
::Trax::Core::NamedClass.new(klass_name, ::Trax::Core::Types::Struct, :parent_definition => self, &block)
|
68
|
-
end
|
69
|
-
|
70
|
-
options[:default] = options.key?(:default) ? options[:default] : DEFAULT_VALUES_FOR_PROPERTY_TYPES[__method__]
|
71
|
-
property(name.to_sym, *args, **options)
|
72
|
-
coerce_key(name.to_sym, attribute_klass)
|
54
|
+
def self.float_property(name, *args, **options, &block)
|
55
|
+
define_attribute_class_for_type(:float, name, *args, :coerce => ::Float, **options, &block)
|
73
56
|
end
|
74
57
|
|
75
|
-
def self.
|
76
|
-
name
|
77
|
-
|
58
|
+
def self.integer_property(name, *args, **options, &block)
|
59
|
+
define_attribute_class_for_type(:integer, name, *args, :coerce => ::Integer, **options, &block)
|
60
|
+
end
|
78
61
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
_klass
|
83
|
-
else
|
84
|
-
::Trax::Core::NamedClass.new(klass_name, ::Trax::Core::Types::Enum, :parent_definition => self, &block)
|
85
|
-
end
|
62
|
+
def self.string_property(name, *args, **options, &block)
|
63
|
+
define_attribute_class_for_type(:string, name, *args, :coerce => ::String, **options, &block)
|
64
|
+
end
|
86
65
|
|
87
|
-
|
88
|
-
|
89
|
-
coerce_key(name.to_sym, attribute_klass)
|
66
|
+
def self.struct_property(name, *args, **options, &block)
|
67
|
+
define_attribute_class_for_type(:struct, name, *args, :coerce => true, **options, &block)
|
90
68
|
end
|
91
69
|
|
92
70
|
def self.to_schema
|
@@ -111,8 +89,10 @@ module Trax
|
|
111
89
|
end
|
112
90
|
|
113
91
|
class << self
|
92
|
+
alias :array :array_property
|
114
93
|
alias :boolean :boolean_property
|
115
94
|
alias :enum :enum_property
|
95
|
+
alias :float :float_property
|
116
96
|
alias :integer :integer_property
|
117
97
|
alias :struct :struct_property
|
118
98
|
alias :string :string_property
|
@@ -121,6 +101,36 @@ module Trax
|
|
121
101
|
def value
|
122
102
|
self
|
123
103
|
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
#By default, strings/int/bool wont get cast to value objects
|
108
|
+
#mainly for the sake of performance/avoid unneccessary object allocation
|
109
|
+
def self.define_attribute_class_for_type(type_name, property_name, *args, coerce:false, **options, &block)
|
110
|
+
name = name.is_a?(Symbol) ? name.to_s : name
|
111
|
+
klass_name = "#{fields_module.name.underscore}/#{property_name}".camelize
|
112
|
+
|
113
|
+
attribute_klass = if options.key?(:extend)
|
114
|
+
_klass_prototype = options[:extend].constantize.clone
|
115
|
+
_klass = ::Trax::Core::NamedClass.new(klass_name, _klass_prototype, :parent_definition => self, &block)
|
116
|
+
_klass
|
117
|
+
else
|
118
|
+
::Trax::Core::NamedClass.new(klass_name, "::Trax::Core::Types::#{type_name.to_s.classify}".constantize, :parent_definition => self, &block)
|
119
|
+
end
|
120
|
+
|
121
|
+
options[:default] = options.key?(:default) ? options[:default] : DEFAULT_VALUES_FOR_PROPERTY_TYPES[__method__]
|
122
|
+
property(property_name.to_sym, *args, **options)
|
123
|
+
|
124
|
+
if coerce.is_a?(::Proc)
|
125
|
+
coerce_key(property_name.to_sym, &coerce)
|
126
|
+
elsif coerce.is_a?(::Array)
|
127
|
+
coerce_key(property_name.to_sym, coerce)
|
128
|
+
elsif [ ::Integer, ::Float, ::String ].include?(coerce)
|
129
|
+
coerce_key(property_name.to_sym, coerce)
|
130
|
+
elsif coerce
|
131
|
+
coerce_key(property_name.to_sym, attribute_klass)
|
132
|
+
end
|
133
|
+
end
|
124
134
|
end
|
125
135
|
end
|
126
136
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Trax
|
2
|
+
module Core
|
3
|
+
module Types
|
4
|
+
class ValueObject < SimpleDelegator
|
5
|
+
def initialize(val)
|
6
|
+
@value = val
|
7
|
+
end
|
8
|
+
|
9
|
+
def __getobj__
|
10
|
+
@value
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.symbolic_name
|
14
|
+
name.demodulize.underscore.to_sym
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.to_sym
|
18
|
+
:value
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/trax/core/types.rb
CHANGED
@@ -3,9 +3,15 @@ module Trax
|
|
3
3
|
module Types
|
4
4
|
extend ::ActiveSupport::Autoload
|
5
5
|
|
6
|
+
autoload :Array
|
7
|
+
autoload :Boolean
|
6
8
|
autoload :Enum
|
7
9
|
autoload :EnumValue
|
10
|
+
autoload :Float
|
11
|
+
autoload :Integer
|
8
12
|
autoload :Struct
|
13
|
+
autoload :String
|
14
|
+
autoload :ValueObject
|
9
15
|
end
|
10
16
|
end
|
11
17
|
end
|
data/lib/trax_core/version.rb
CHANGED
data/spec/support/defs.rb
CHANGED
@@ -10,8 +10,10 @@ module Defs
|
|
10
10
|
|
11
11
|
struct :ProductAttributes do
|
12
12
|
string :name, :default => ""
|
13
|
-
|
13
|
+
float :price, :default => 9.99
|
14
|
+
integer :quantity_in_stock, :default => 0
|
14
15
|
boolean :is_active, :default => false
|
16
|
+
array :categories, :of => "Defs::Category", :default => []
|
15
17
|
end
|
16
18
|
|
17
19
|
struct :ShoesAttributes, :extend => "Defs::ProductAttributes" do
|
@@ -3,6 +3,10 @@ require 'spec_helper'
|
|
3
3
|
describe ::Trax::Core::Definitions do
|
4
4
|
subject { ::Defs }
|
5
5
|
|
6
|
+
context "fields" do
|
7
|
+
it { subject[:category].should eq subject::Category }
|
8
|
+
end
|
9
|
+
|
6
10
|
context "enum" do
|
7
11
|
let(:test_subject) { subject::Category.new(1) }
|
8
12
|
|
@@ -12,6 +16,24 @@ describe ::Trax::Core::Definitions do
|
|
12
16
|
|
13
17
|
context "struct" do
|
14
18
|
it { expect(subject::ProductAttributes.new).to be_a(::Trax::Core::Types::Struct) }
|
19
|
+
|
20
|
+
context "float" do
|
21
|
+
it { expect(subject::ProductAttributes.new.price).to eq 9.99 }
|
22
|
+
it { expect(subject::ProductAttributes.new(:price => 9).price).to eq 9.0 }
|
23
|
+
end
|
24
|
+
|
25
|
+
context "integer" do
|
26
|
+
it { expect(subject::ProductAttributes.new.quantity_in_stock).to eq 0 }
|
27
|
+
it { expect(subject::ProductAttributes.new(:quantity_in_stock => 9.19).quantity_in_stock).to eq 9 }
|
28
|
+
end
|
29
|
+
|
30
|
+
context "array" do
|
31
|
+
it { expect(subject::ProductAttributes.new.categories).to be_a(Array) }
|
32
|
+
context "it instantiates members into objects if provided" do
|
33
|
+
it { expect(subject::ProductAttributes.new(:categories => [ :clothing ]).categories[0].to_i).to eq 2 }
|
34
|
+
it { expect(subject::ProductAttributes.new(:categories => [ 2 ]).categories[0].to_s).to eq "clothing" }
|
35
|
+
end
|
36
|
+
end
|
15
37
|
end
|
16
38
|
|
17
39
|
context "inheritance" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trax_core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.78
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Ayre
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashie
|
@@ -210,9 +210,15 @@ files:
|
|
210
210
|
- lib/trax/core/primitives/enum.rb
|
211
211
|
- lib/trax/core/primitives/enum_value.rb
|
212
212
|
- lib/trax/core/types.rb
|
213
|
+
- lib/trax/core/types/array.rb
|
214
|
+
- lib/trax/core/types/boolean.rb
|
213
215
|
- lib/trax/core/types/enum.rb
|
214
216
|
- lib/trax/core/types/enum_value.rb
|
217
|
+
- lib/trax/core/types/float.rb
|
218
|
+
- lib/trax/core/types/integer.rb
|
219
|
+
- lib/trax/core/types/string.rb
|
215
220
|
- lib/trax/core/types/struct.rb
|
221
|
+
- lib/trax/core/types/value_object.rb
|
216
222
|
- lib/trax_core.rb
|
217
223
|
- lib/trax_core/version.rb
|
218
224
|
- spec/spec_helper.rb
|