trax_core 0.0.76 → 0.0.77
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 +49 -0
- data/lib/trax/core/fields.rb +72 -0
- data/lib/trax/core/named_class.rb +3 -3
- data/lib/trax/core/types/enum.rb +224 -0
- data/lib/trax/core/types/enum_value.rb +62 -0
- data/lib/trax/core/types/struct.rb +127 -0
- data/lib/trax/core/types.rb +11 -0
- data/lib/trax/core.rb +3 -0
- data/lib/trax_core/version.rb +1 -1
- data/spec/support/defs.rb +26 -0
- data/spec/trax/core/definitions_spec.rb +23 -0
- data/spec/trax/core/types/enum_spec.rb +31 -0
- data/spec/trax/core/types/struct_spec.rb +28 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aa0d0a5564b1b1fb8a26de8475b203848b4c1aea
|
4
|
+
data.tar.gz: d3b6b0c5ad07512b6e767ab7f1c5b8fc850286e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 88518e6d9a919091b3a3b2db65a2ebb68cf1ec1e7508a8bf7b731bb56e68a23596973b80967c504ce9d0d433832f90e25336f7d421efcceb10c9ff6e95dbbf60
|
7
|
+
data.tar.gz: 1f66e3eb60887b82a2bd44369dfd48a26f76fe5b62ec83aaaaba3e267eda57775b9eae76b24b19e473a6836873ee14f61e3b0fcafeff4ee6bb908eb0d31dc51d
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Trax
|
2
|
+
module Core
|
3
|
+
module Definitions
|
4
|
+
def self.extended(base)
|
5
|
+
base.module_attribute(:_definitions) {
|
6
|
+
::Hashie::Mash.new
|
7
|
+
}
|
8
|
+
end
|
9
|
+
|
10
|
+
def enum(klass_name, **options, &block)
|
11
|
+
attribute_klass = if options.key?(:extend)
|
12
|
+
_klass_prototype = options[:extend].constantize.clone
|
13
|
+
::Trax::Core::NamedClass.new("#{self.name}::#{klass_name}", _klass_prototype, :parent_definition => self, &block)
|
14
|
+
else
|
15
|
+
::Trax::Core::NamedClass.new("#{self.name}::#{klass_name}", ::Trax::Core::Types::Enum, :parent_definition => self, &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
attribute_klass
|
19
|
+
end
|
20
|
+
|
21
|
+
def struct(klass_name, **options, &block)
|
22
|
+
attribute_klass = if options.key?(:extend)
|
23
|
+
_klass_prototype = options[:extend].constantize.clone
|
24
|
+
::Trax::Core::NamedClass.new("#{self.name}::#{klass_name}", _klass_prototype, :parent_definition => self, &block)
|
25
|
+
else
|
26
|
+
::Trax::Core::NamedClass.new("#{self.name}::#{klass_name}", ::Trax::Core::Types::Struct, :parent_definition => self, &block)
|
27
|
+
end
|
28
|
+
|
29
|
+
attribute_klass
|
30
|
+
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
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Trax
|
2
|
+
module Core
|
3
|
+
module Fields
|
4
|
+
def self.extended(base)
|
5
|
+
base.module_attribute(:_blank_fields_hash) {
|
6
|
+
::Hashie::Mash.new
|
7
|
+
}
|
8
|
+
end
|
9
|
+
|
10
|
+
def all
|
11
|
+
@all ||= begin
|
12
|
+
constants.map{|const_name| const_get(const_name) }.each_with_object(self._blank_fields_hash) do |klass, result|
|
13
|
+
result[klass.name.symbolize] = klass
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def by_type(*type_names)
|
19
|
+
all.select{|k,v| type_names.include?(v.type) }
|
20
|
+
.try(:with_indifferent_access)
|
21
|
+
end
|
22
|
+
|
23
|
+
def each(&block)
|
24
|
+
all.values(&block)
|
25
|
+
end
|
26
|
+
|
27
|
+
def each_pair(*args, &block)
|
28
|
+
all.each_pair(*args, &block)
|
29
|
+
end
|
30
|
+
|
31
|
+
def booleans
|
32
|
+
@booleans ||= by_type(:boolean)
|
33
|
+
end
|
34
|
+
|
35
|
+
def enums
|
36
|
+
@enums ||= by_type(:enum)
|
37
|
+
end
|
38
|
+
|
39
|
+
def structs
|
40
|
+
@structs ||= by_type(:struct)
|
41
|
+
end
|
42
|
+
|
43
|
+
def strings
|
44
|
+
@strings ||= by_type(:string)
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_schema
|
48
|
+
schema = all.inject(::Hashie::Mash.new) do |result, (k,v)|
|
49
|
+
case v.try(:type)
|
50
|
+
when :enum
|
51
|
+
result[k] = v.to_schema
|
52
|
+
when :struct
|
53
|
+
result[k] = v.to_schema
|
54
|
+
else
|
55
|
+
result[k] = v.try(:to_schema)
|
56
|
+
end
|
57
|
+
|
58
|
+
result
|
59
|
+
end
|
60
|
+
schema
|
61
|
+
end
|
62
|
+
|
63
|
+
def values
|
64
|
+
all.values
|
65
|
+
end
|
66
|
+
|
67
|
+
def [](_name)
|
68
|
+
const_get(_name.to_s.camelize)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
module Trax
|
2
2
|
module Core
|
3
3
|
class NamedClass
|
4
|
-
def self.new(_name, _parent_klass=
|
5
|
-
klass = ::Object.set_fully_qualified_constant(_name,
|
4
|
+
def self.new(_name, _parent_klass=Object, **options, &block)
|
5
|
+
klass = ::Object.set_fully_qualified_constant(_name, ::Class.new(_parent_klass) do
|
6
6
|
define_singleton_method(:name) { _name }
|
7
|
-
end)
|
7
|
+
end)
|
8
8
|
|
9
9
|
options.each_pair do |k,v|
|
10
10
|
klass.class_attribute k
|
@@ -0,0 +1,224 @@
|
|
1
|
+
# require 'trax/core/inheritance_hooks'
|
2
|
+
# require 'active_model/attribute_methods'
|
3
|
+
### Examples
|
4
|
+
# ProductCategory < Enum
|
5
|
+
# CLOTHING = 1
|
6
|
+
# SHOES = 2
|
7
|
+
# ACCESSORIES = 3
|
8
|
+
# end
|
9
|
+
# ProductCategory.keys => [:clothing, :shoes, :accessories]
|
10
|
+
|
11
|
+
# StoreYearlyRevenue < Enum
|
12
|
+
# :'0_100000' = 1
|
13
|
+
# :'100000_999999' = 2
|
14
|
+
# :'1000000_99999999' = 3
|
15
|
+
# end
|
16
|
+
|
17
|
+
### Accepts either an integer or the name when setting a value
|
18
|
+
# ProductCategory.new(1) => #{name: :clothing, :value => 1}
|
19
|
+
module Trax
|
20
|
+
module Core
|
21
|
+
module Types
|
22
|
+
class Enum < SimpleDelegator
|
23
|
+
class_attribute :allow_nil, :raise_on_invalid
|
24
|
+
|
25
|
+
### Class Methods ###
|
26
|
+
def self.define_enum_value(const_name, val=nil)
|
27
|
+
name = "#{const_name}".underscore.to_sym
|
28
|
+
const_name = name.to_s.camelize
|
29
|
+
val = (self._values_hash.length + 1) if val.nil?
|
30
|
+
|
31
|
+
raise ::Trax::Core::Errors::DuplicateEnumValue.new(:klass => self.class.name, :value => const_name) if self === name
|
32
|
+
raise ::Trax::Core::Errors::DuplicateEnumValue.new(:klass => self.class.name, :value => val) if self === val
|
33
|
+
|
34
|
+
value_klass = self.const_set(const_name, ::Class.new(::Trax::Core::Types::EnumValue){
|
35
|
+
self.tag = name
|
36
|
+
self.value = val
|
37
|
+
})
|
38
|
+
|
39
|
+
self._values_hash[val] = value_klass
|
40
|
+
self._names_hash[name] = value_klass
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.[](val)
|
44
|
+
if ::Is.numeric?(val)
|
45
|
+
self._values_hash[val]
|
46
|
+
elsif ::Is.symbolic?(val)
|
47
|
+
val = val.to_sym if val.is_a?(::String)
|
48
|
+
self._names_hash[val]
|
49
|
+
elsif val.superclass.name == "Trax::Core::Types::EnumValue"
|
50
|
+
val = val.to_sym
|
51
|
+
self._names_hash[val]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.as_json(options={})
|
56
|
+
choice.to_s
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.choices
|
60
|
+
@choices ||= self._values_hash.values
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.formatted_choices
|
64
|
+
@formatted_choices ||= choices.each_with_object({}) do |choice, hash|
|
65
|
+
hash[choice.to_i] = choice.to_s
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.select_values(*args)
|
70
|
+
args.flat_compact_uniq!
|
71
|
+
args.map{|arg| self[arg].to_i }
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.define(*args)
|
75
|
+
define_enum_value(*args)
|
76
|
+
end
|
77
|
+
|
78
|
+
#define multiple values if its iterable
|
79
|
+
def self.define_values(*args)
|
80
|
+
args.each_with_index do |arg, i|
|
81
|
+
define_enum_value(arg, (i + 1))
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.each(&block)
|
86
|
+
keys.each(&block)
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.each_pair(&block)
|
90
|
+
self._names_hash.each_pair(&block)
|
91
|
+
end
|
92
|
+
|
93
|
+
def self.keys
|
94
|
+
_names_hash.keys
|
95
|
+
end
|
96
|
+
|
97
|
+
def self.key?(name)
|
98
|
+
_names_hash.key?(name)
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.names
|
102
|
+
_names_hash.values
|
103
|
+
end
|
104
|
+
|
105
|
+
def self.no_raise_mode?
|
106
|
+
!raise_on_invalid
|
107
|
+
end
|
108
|
+
|
109
|
+
def self.valid_name?(val)
|
110
|
+
_names_as_strings.include?(val)
|
111
|
+
end
|
112
|
+
|
113
|
+
def self.valid_value?(val)
|
114
|
+
values.include?(val)
|
115
|
+
end
|
116
|
+
|
117
|
+
#because calling valid_value? in the define_enum_value method is unclear
|
118
|
+
def self.value?(val)
|
119
|
+
valid_value?(val)
|
120
|
+
end
|
121
|
+
|
122
|
+
def self.values
|
123
|
+
_names_hash.values.map(&:to_i)
|
124
|
+
end
|
125
|
+
|
126
|
+
def self.===(val)
|
127
|
+
_names_hash.values.any?{|v| v === val }
|
128
|
+
end
|
129
|
+
|
130
|
+
def self.type
|
131
|
+
:enum
|
132
|
+
end
|
133
|
+
|
134
|
+
def self.to_schema
|
135
|
+
::Trax::Core::Definition.new(
|
136
|
+
:name => self.name.demodulize.underscore,
|
137
|
+
:source => self.name,
|
138
|
+
:type => :enum,
|
139
|
+
:choices => choices.map(&:to_schema),
|
140
|
+
:values => keys
|
141
|
+
)
|
142
|
+
end
|
143
|
+
|
144
|
+
class << self
|
145
|
+
alias :enum_value :define_enum_value
|
146
|
+
alias :define :define_enum_value
|
147
|
+
attr_accessor :_values_hash
|
148
|
+
attr_accessor :_names_hash
|
149
|
+
attr_accessor :_names_as_strings
|
150
|
+
end
|
151
|
+
|
152
|
+
### Hooks ###
|
153
|
+
def self.inherited(subklass)
|
154
|
+
subklass.instance_variable_set(:@_values_hash, ::Hash.new)
|
155
|
+
subklass.instance_variable_set(:@_names_hash, ::Hash.new)
|
156
|
+
subklass.allow_nil = false
|
157
|
+
subklass.raise_on_invalid = false
|
158
|
+
end
|
159
|
+
|
160
|
+
### Instance Methods ###
|
161
|
+
attr_reader :choice
|
162
|
+
|
163
|
+
def initialize(val)
|
164
|
+
self.choice = val unless val.nil? && self.class.allow_nil
|
165
|
+
end
|
166
|
+
|
167
|
+
def current_index
|
168
|
+
self.class.names.index(choice)
|
169
|
+
end
|
170
|
+
|
171
|
+
def choice=(val)
|
172
|
+
@choice = valid_choice?(val) ? self.class[val] : nil
|
173
|
+
|
174
|
+
raise ::Trax::Core::Errors::InvalidEnumValue.new(
|
175
|
+
:field => self.class.name,
|
176
|
+
:value => val
|
177
|
+
) if self.class.raise_on_invalid && !@choice
|
178
|
+
|
179
|
+
@choice
|
180
|
+
end
|
181
|
+
|
182
|
+
def __getobj__
|
183
|
+
@choice || nil
|
184
|
+
end
|
185
|
+
|
186
|
+
def next_value
|
187
|
+
return choice if self.class.names.length == current_index
|
188
|
+
self.class.names[(current_index + 1)]
|
189
|
+
end
|
190
|
+
|
191
|
+
def next_value?
|
192
|
+
!(current_index == (self.class.names.length - 1))
|
193
|
+
end
|
194
|
+
|
195
|
+
#set choice if next value exists, return selected choi
|
196
|
+
def select_next_value
|
197
|
+
self.choice = next_value.to_sym if next_value?
|
198
|
+
self
|
199
|
+
end
|
200
|
+
|
201
|
+
def select_previous_value
|
202
|
+
self.choice = previous_value.to_sym if previous_value?
|
203
|
+
self
|
204
|
+
end
|
205
|
+
|
206
|
+
def previous_value
|
207
|
+
self.class.names[(current_index - 1)]
|
208
|
+
end
|
209
|
+
|
210
|
+
def previous_value?
|
211
|
+
!!current_index
|
212
|
+
end
|
213
|
+
|
214
|
+
def to_s
|
215
|
+
choice.to_s
|
216
|
+
end
|
217
|
+
|
218
|
+
def valid_choice?(val)
|
219
|
+
self.class === val
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'trax/core/abstract_methods'
|
2
|
+
module Trax
|
3
|
+
module Core
|
4
|
+
module Types
|
5
|
+
class EnumValue
|
6
|
+
include ::Trax::Core::AbstractMethods
|
7
|
+
|
8
|
+
abstract_class_attribute :tag, :value
|
9
|
+
|
10
|
+
def self.as_json(options={})
|
11
|
+
tag.to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.enum
|
15
|
+
parent
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.to_s
|
19
|
+
tag.to_s
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.to_sym
|
23
|
+
tag
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.to_i
|
27
|
+
value
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.is_enum_value?(val)
|
31
|
+
val == parent
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.to_schema
|
35
|
+
::Trax::Core::Definition.new(
|
36
|
+
:source => self.name,
|
37
|
+
:name => to_s,
|
38
|
+
:type => :enum_value,
|
39
|
+
:integer_value => to_i
|
40
|
+
)
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.inspect
|
44
|
+
":#{tag}"
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.include?(val)
|
48
|
+
self.=== val
|
49
|
+
end
|
50
|
+
|
51
|
+
#maybe this is a bad idea, not entirely sure
|
52
|
+
def self.==(val)
|
53
|
+
self.=== val
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.===(val)
|
57
|
+
[tag, to_s, to_i].include?(val)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
require 'hashie/extensions/coercion'
|
2
|
+
require 'hashie/extensions/indifferent_access'
|
3
|
+
require 'hashie/extensions/dash/indifferent_access'
|
4
|
+
|
5
|
+
module Trax
|
6
|
+
module Core
|
7
|
+
module Types
|
8
|
+
class Struct < ::Hashie::Dash
|
9
|
+
include ::Hashie::Extensions::Dash::IndifferentAccess
|
10
|
+
include ::Hashie::Extensions::Coercion
|
11
|
+
include ::Hashie::Extensions::IgnoreUndeclared
|
12
|
+
include ::Hashie::Extensions::Dash::PropertyTranslation
|
13
|
+
|
14
|
+
# note that we must explicitly set default or blank values for all properties.
|
15
|
+
# It defeats the whole purpose of being a 'struct'
|
16
|
+
# if we fail to do so, and it makes our data far more error prone
|
17
|
+
DEFAULT_VALUES_FOR_PROPERTY_TYPES = {
|
18
|
+
:boolean_property => nil,
|
19
|
+
:string_property => "",
|
20
|
+
:struct_property => {},
|
21
|
+
:enum_property => nil,
|
22
|
+
:integer_property => nil
|
23
|
+
}.with_indifferent_access.freeze
|
24
|
+
|
25
|
+
def self.fields_module
|
26
|
+
@fields_module ||= begin
|
27
|
+
module_name = "#{self.name}::Fields"
|
28
|
+
::Trax::Core::NamedModule.new(module_name, ::Trax::Core::Fields)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.fields
|
33
|
+
fields_module
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.boolean_property(name, *args, **options, &block)
|
37
|
+
name = name.is_a?(Symbol) ? name.to_s : name
|
38
|
+
klass_name = "#{fields_module.name.underscore}/#{name}".camelize
|
39
|
+
options[:default] = options.key?(:default) ? options[:default] : DEFAULT_VALUES_FOR_PROPERTY_TYPES[__method__]
|
40
|
+
property(name.to_sym, *args, **options)
|
41
|
+
coerce_key(name.to_sym, ->(value) { !!value })
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.integer_property(name, *args, **options, &block)
|
45
|
+
name = name.is_a?(Symbol) ? name.to_s : 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)
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.string_property(name, *args, **options, &block)
|
52
|
+
name = name.is_a?(Symbol) ? name.to_s : 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)
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.struct_property(name, *args, **options, &block)
|
59
|
+
name = name.is_a?(Symbol) ? name.to_s : 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)
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.enum_property(name, *args, **options, &block)
|
76
|
+
name = name.is_a?(Symbol) ? name.to_s : name
|
77
|
+
klass_name = "#{fields_module.name.underscore}/#{name}".camelize
|
78
|
+
|
79
|
+
attribute_klass = if options.key?(:extend)
|
80
|
+
_klass_prototype = options[:extend].constantize.clone
|
81
|
+
_klass = ::Trax::Core::NamedClass.new(klass_name, _klass_prototype, :parent_definition => self, &block)
|
82
|
+
_klass
|
83
|
+
else
|
84
|
+
::Trax::Core::NamedClass.new(klass_name, ::Trax::Core::Types::Enum, :parent_definition => self, &block)
|
85
|
+
end
|
86
|
+
|
87
|
+
options[:default] = options.key?(:default) ? options[:default] : DEFAULT_VALUES_FOR_PROPERTY_TYPES[__method__]
|
88
|
+
property(name.to_sym, *args, **options)
|
89
|
+
coerce_key(name.to_sym, attribute_klass)
|
90
|
+
end
|
91
|
+
|
92
|
+
def self.to_schema
|
93
|
+
::Trax::Core::Definition.new(
|
94
|
+
:source => self.name,
|
95
|
+
:name => self.name.demodulize.underscore,
|
96
|
+
:type => :struct,
|
97
|
+
:fields => self.fields_module.to_schema
|
98
|
+
)
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.type; :struct end;
|
102
|
+
|
103
|
+
def to_serializable_hash
|
104
|
+
_serializable_hash = to_hash
|
105
|
+
|
106
|
+
self.class.fields_module.enums.keys.each do |attribute_name|
|
107
|
+
_serializable_hash[attribute_name] = _serializable_hash[attribute_name].try(:to_i)
|
108
|
+
end if self.class.fields_module.enums.keys.any?
|
109
|
+
|
110
|
+
_serializable_hash
|
111
|
+
end
|
112
|
+
|
113
|
+
class << self
|
114
|
+
alias :boolean :boolean_property
|
115
|
+
alias :enum :enum_property
|
116
|
+
alias :integer :integer_property
|
117
|
+
alias :struct :struct_property
|
118
|
+
alias :string :string_property
|
119
|
+
end
|
120
|
+
|
121
|
+
def value
|
122
|
+
self
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
data/lib/trax/core.rb
CHANGED
@@ -21,9 +21,11 @@ module Trax
|
|
21
21
|
autoload :Configuration
|
22
22
|
autoload :Concern
|
23
23
|
autoload :Definition
|
24
|
+
autoload :Definitions
|
24
25
|
autoload :EagerLoadNamespace
|
25
26
|
autoload :EagerAutoloadNamespace
|
26
27
|
autoload :Errors
|
28
|
+
autoload :Fields
|
27
29
|
autoload :FS
|
28
30
|
autoload :HasMixins
|
29
31
|
autoload :InheritanceHooks
|
@@ -31,5 +33,6 @@ module Trax
|
|
31
33
|
autoload :Mixable
|
32
34
|
autoload :NamedClass
|
33
35
|
autoload :NamedModule
|
36
|
+
autoload :Types
|
34
37
|
end
|
35
38
|
end
|
data/lib/trax_core/version.rb
CHANGED
@@ -0,0 +1,26 @@
|
|
1
|
+
module Defs
|
2
|
+
extend ::Trax::Core::Definitions
|
3
|
+
|
4
|
+
enum :Category do
|
5
|
+
define :default, 1
|
6
|
+
define :clothing, 2
|
7
|
+
define :shoes, 3
|
8
|
+
define :accessories, 4
|
9
|
+
end
|
10
|
+
|
11
|
+
struct :ProductAttributes do
|
12
|
+
string :name, :default => ""
|
13
|
+
integer :price, :default => ""
|
14
|
+
boolean :is_active, :default => false
|
15
|
+
end
|
16
|
+
|
17
|
+
struct :ShoesAttributes, :extend => "Defs::ProductAttributes" do
|
18
|
+
enum :size do
|
19
|
+
define :mens_8, 1
|
20
|
+
define :mens_9, 2
|
21
|
+
define :mens_10, 3
|
22
|
+
define :mens_11, 4
|
23
|
+
define :mens_12, 5
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ::Trax::Core::Definitions do
|
4
|
+
subject { ::Defs }
|
5
|
+
|
6
|
+
context "enum" do
|
7
|
+
let(:test_subject) { subject::Category.new(1) }
|
8
|
+
|
9
|
+
it { expect(test_subject).to be_a(::Trax::Core::Types::Enum) }
|
10
|
+
it { expect(test_subject.name).to eq "Defs::Category::Default" }
|
11
|
+
end
|
12
|
+
|
13
|
+
context "struct" do
|
14
|
+
it { expect(subject::ProductAttributes.new).to be_a(::Trax::Core::Types::Struct) }
|
15
|
+
end
|
16
|
+
|
17
|
+
context "inheritance" do
|
18
|
+
it { expect(subject::ProductAttributes.new).to_not have_key(:size) }
|
19
|
+
it { expect(subject::ShoesAttributes.new).to have_key(:size) }
|
20
|
+
it { expect(subject::ShoesAttributes.new).to have_key(:price) }
|
21
|
+
it { expect(subject::ShoesAttributes.new(:size => :mens_8).size.to_i).to eq 1 }
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ::Trax::Core::Types::Enum do
|
4
|
+
before(:all) do
|
5
|
+
module MyFakeEnumNamespace
|
6
|
+
extend ::Trax::Core::Definitions
|
7
|
+
|
8
|
+
enum :Locale do
|
9
|
+
define :en, 1
|
10
|
+
define :da, 2
|
11
|
+
define :ca, 3
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
subject { "::MyFakeEnumNamespace::Locale".constantize.new(:en) }
|
17
|
+
|
18
|
+
it { expect(subject.to_i).to eq 1 }
|
19
|
+
|
20
|
+
context "integer value" do
|
21
|
+
subject { "::MyFakeEnumNamespace::Locale".constantize.new(1) }
|
22
|
+
|
23
|
+
it { expect(subject).to eq :en }
|
24
|
+
end
|
25
|
+
|
26
|
+
context "non existent value" do
|
27
|
+
subject { "::MyFakeEnumNamespace::Locale".constantize.new(:blah) }
|
28
|
+
|
29
|
+
it { expect(subject).to eq nil }
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ::Trax::Core::Types::Struct do
|
4
|
+
before(:all) do
|
5
|
+
module MyFakeStructNamespace
|
6
|
+
extend ::Trax::Core::Definitions
|
7
|
+
|
8
|
+
struct :Locale do
|
9
|
+
string :en, :default => ""
|
10
|
+
string :da, :default => ""
|
11
|
+
string :ca, :default => "eh"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
subject { "::MyFakeStructNamespace::Locale".constantize.new(:en => "something") }
|
17
|
+
|
18
|
+
it { expect(subject).to have_key("en") }
|
19
|
+
it { expect(subject.en).to eq "something" }
|
20
|
+
it { expect(subject.da).to eq "" }
|
21
|
+
it { expect(subject.ca).to eq "eh" }
|
22
|
+
|
23
|
+
context "unknown value" do
|
24
|
+
subject { "::MyFakeStructNamespace::Locale".constantize.new(:blah => "something") }
|
25
|
+
it { expect(subject).to_not respond_to(:blah) }
|
26
|
+
it { expect(subject.en).to eq "" }
|
27
|
+
end
|
28
|
+
end
|
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.77
|
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-09-
|
11
|
+
date: 2015-09-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashie
|
@@ -184,6 +184,7 @@ files:
|
|
184
184
|
- lib/trax/core/concern.rb
|
185
185
|
- lib/trax/core/configuration.rb
|
186
186
|
- lib/trax/core/definition.rb
|
187
|
+
- lib/trax/core/definitions.rb
|
187
188
|
- lib/trax/core/eager_autoload_namespace.rb
|
188
189
|
- lib/trax/core/eager_load_namespace.rb
|
189
190
|
- lib/trax/core/errors.rb
|
@@ -197,6 +198,7 @@ files:
|
|
197
198
|
- lib/trax/core/ext/string.rb
|
198
199
|
- lib/trax/core/ext/symbol.rb
|
199
200
|
- lib/trax/core/ext/uri.rb
|
201
|
+
- lib/trax/core/fields.rb
|
200
202
|
- lib/trax/core/fs.rb
|
201
203
|
- lib/trax/core/has_mixins.rb
|
202
204
|
- lib/trax/core/inheritance_hooks.rb
|
@@ -207,10 +209,15 @@ files:
|
|
207
209
|
- lib/trax/core/named_module.rb
|
208
210
|
- lib/trax/core/primitives/enum.rb
|
209
211
|
- lib/trax/core/primitives/enum_value.rb
|
212
|
+
- lib/trax/core/types.rb
|
213
|
+
- lib/trax/core/types/enum.rb
|
214
|
+
- lib/trax/core/types/enum_value.rb
|
215
|
+
- lib/trax/core/types/struct.rb
|
210
216
|
- lib/trax_core.rb
|
211
217
|
- lib/trax_core/version.rb
|
212
218
|
- spec/spec_helper.rb
|
213
219
|
- spec/support/category_enum.rb
|
220
|
+
- spec/support/defs.rb
|
214
221
|
- spec/support/ecom.rb
|
215
222
|
- spec/support/ecom/widget.rb
|
216
223
|
- spec/support/ecom/widget_category.rb
|
@@ -226,6 +233,7 @@ files:
|
|
226
233
|
- spec/support/storefront/product.rb
|
227
234
|
- spec/trax/array_spec.rb
|
228
235
|
- spec/trax/core/concern_spec.rb
|
236
|
+
- spec/trax/core/definitions_spec.rb
|
229
237
|
- spec/trax/core/eager_autoload_namespace_spec.rb
|
230
238
|
- spec/trax/core/errors_spec.rb
|
231
239
|
- spec/trax/core/ext/array_spec.rb
|
@@ -237,6 +245,8 @@ files:
|
|
237
245
|
- spec/trax/core/inheritance_spec.rb
|
238
246
|
- spec/trax/core/named_class_spec.rb
|
239
247
|
- spec/trax/core/named_module_spec.rb
|
248
|
+
- spec/trax/core/types/enum_spec.rb
|
249
|
+
- spec/trax/core/types/struct_spec.rb
|
240
250
|
- spec/trax/core_spec.rb
|
241
251
|
- spec/trax/enum_spec.rb
|
242
252
|
- spec/trax/hash_spec.rb
|
@@ -268,6 +278,7 @@ summary: Core Trax Dependencies
|
|
268
278
|
test_files:
|
269
279
|
- spec/spec_helper.rb
|
270
280
|
- spec/support/category_enum.rb
|
281
|
+
- spec/support/defs.rb
|
271
282
|
- spec/support/ecom.rb
|
272
283
|
- spec/support/ecom/widget.rb
|
273
284
|
- spec/support/ecom/widget_category.rb
|
@@ -283,6 +294,7 @@ test_files:
|
|
283
294
|
- spec/support/storefront/product.rb
|
284
295
|
- spec/trax/array_spec.rb
|
285
296
|
- spec/trax/core/concern_spec.rb
|
297
|
+
- spec/trax/core/definitions_spec.rb
|
286
298
|
- spec/trax/core/eager_autoload_namespace_spec.rb
|
287
299
|
- spec/trax/core/errors_spec.rb
|
288
300
|
- spec/trax/core/ext/array_spec.rb
|
@@ -294,6 +306,8 @@ test_files:
|
|
294
306
|
- spec/trax/core/inheritance_spec.rb
|
295
307
|
- spec/trax/core/named_class_spec.rb
|
296
308
|
- spec/trax/core/named_module_spec.rb
|
309
|
+
- spec/trax/core/types/enum_spec.rb
|
310
|
+
- spec/trax/core/types/struct_spec.rb
|
297
311
|
- spec/trax/core_spec.rb
|
298
312
|
- spec/trax/enum_spec.rb
|
299
313
|
- spec/trax/hash_spec.rb
|