trax_core 0.0.82 → 0.0.83
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/trax/core.rb +1 -0
- data/lib/trax/core/types.rb +3 -1
- data/lib/trax/core/types/array.rb +1 -0
- data/lib/trax/core/types/array_of.rb +2 -0
- data/lib/trax/core/types/behaviors/array_of_members.rb +1 -1
- data/lib/trax/core/types/json.rb +11 -0
- data/lib/trax/core/types/struct.rb +32 -15
- data/lib/trax/core/types/time.rb +11 -0
- data/lib/trax_core/version.rb +1 -1
- data/spec/trax/core/definitions_spec.rb +1 -1
- data/spec/trax/core/types/struct_spec.rb +83 -0
- data/trax_core.gemspec +1 -0
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6d3edf83c6a6d1b9b57bfacf6e6b185139c5e9e
|
4
|
+
data.tar.gz: fe41bc8fa78daa685df68da9d513bb3d5377b421
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c3579335b54d9831cd2bf708aad4ad3d6bcd011b208b0514ccf75ab43a272dd3aa52fdab1aff545382606d26535740bdb6ba56dd7879676f027368ef755bbc9
|
7
|
+
data.tar.gz: 0c29fc5fd805a2476345dd692ed5b6a19720241343b8bcba259c428e6cd31d16e257fa5d033209a838d488567c4732809f22586da9b644749598f48c598401b6
|
data/lib/trax/core.rb
CHANGED
data/lib/trax/core/types.rb
CHANGED
@@ -15,13 +15,16 @@ 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
|
19
|
-
:
|
20
|
-
:
|
21
|
-
:
|
22
|
-
:
|
23
|
-
:
|
24
|
-
:
|
18
|
+
:array => [],
|
19
|
+
:array_of => [],
|
20
|
+
:boolean => nil,
|
21
|
+
:enum => nil,
|
22
|
+
:float => 0.0,
|
23
|
+
:integer => nil,
|
24
|
+
:json => {},
|
25
|
+
:string => "",
|
26
|
+
:struct => {},
|
27
|
+
:time => nil
|
25
28
|
}.with_indifferent_access.freeze
|
26
29
|
|
27
30
|
def self.fields_module
|
@@ -38,13 +41,17 @@ module Trax
|
|
38
41
|
end
|
39
42
|
|
40
43
|
def self.array_property(name, *args, of:false, **options, &block)
|
41
|
-
of_object = of && of.is_a?(::String) ? of
|
42
|
-
coercer = of_object ?
|
43
|
-
|
44
|
+
of_object = of && of.is_a?(::String) ? const_get(of) : of
|
45
|
+
coercer = of_object ? true : ::Array
|
46
|
+
options.merge!(:member_class => of_object) if of
|
47
|
+
array_or_array_of = of ? :array_of : :array
|
48
|
+
define_attribute_class_for_type(array_or_array_of, name, *args, :coerce => coercer, **options, &block)
|
44
49
|
end
|
45
50
|
|
46
51
|
def self.boolean_property(name, *args, **options, &block)
|
47
|
-
define_attribute_class_for_type(:boolean, name, *args, :coerce => ->(value){
|
52
|
+
define_attribute_class_for_type(:boolean, name, *args, :coerce => ->(value){
|
53
|
+
[true, false].include?(value) ? value : value.to_b
|
54
|
+
}, **options, &block)
|
48
55
|
end
|
49
56
|
|
50
57
|
def self.enum_property(name, *args, **options, &block)
|
@@ -59,6 +66,10 @@ module Trax
|
|
59
66
|
define_attribute_class_for_type(:integer, name, *args, :coerce => ::Integer, **options, &block)
|
60
67
|
end
|
61
68
|
|
69
|
+
def self.json_property(name, *args, **options, &block)
|
70
|
+
define_attribute_class_for_type(:json, name, *args, **options, &block)
|
71
|
+
end
|
72
|
+
|
62
73
|
def self.string_property(name, *args, **options, &block)
|
63
74
|
define_attribute_class_for_type(:string, name, *args, :coerce => ::String, **options, &block)
|
64
75
|
end
|
@@ -67,6 +78,10 @@ module Trax
|
|
67
78
|
define_attribute_class_for_type(:struct, name, *args, :coerce => true, **options, &block)
|
68
79
|
end
|
69
80
|
|
81
|
+
def self.time_property(name, *args, **options, &block)
|
82
|
+
define_attribute_class_for_type(:time, name, *args, :coerce => ->(value){ ::Time.parse(value) if value }, **options, &block)
|
83
|
+
end
|
84
|
+
|
70
85
|
def self.to_schema
|
71
86
|
::Trax::Core::Definition.new(
|
72
87
|
:source => self.name,
|
@@ -94,8 +109,10 @@ module Trax
|
|
94
109
|
alias :enum :enum_property
|
95
110
|
alias :float :float_property
|
96
111
|
alias :integer :integer_property
|
97
|
-
alias :
|
112
|
+
alias :json :json_property
|
98
113
|
alias :string :string_property
|
114
|
+
alias :struct :struct_property
|
115
|
+
alias :time :time_property
|
99
116
|
end
|
100
117
|
|
101
118
|
def value
|
@@ -112,17 +129,17 @@ module Trax
|
|
112
129
|
|
113
130
|
attribute_klass = if options.key?(:extend)
|
114
131
|
_klass_prototype = options[:extend].is_a?(::String) ? options[:extend].safe_constantize : options[:extend]
|
115
|
-
_klass = ::Trax::Core::NamedClass.new(klass_name, _klass_prototype, :parent_definition => self, &block)
|
132
|
+
_klass = ::Trax::Core::NamedClass.new(klass_name, _klass_prototype, :parent_definition => self, **options, &block)
|
116
133
|
_klass
|
117
134
|
else
|
118
|
-
::Trax::Core::NamedClass.new(klass_name, "::Trax::Core::Types::#{type_name.to_s.classify}".constantize, :parent_definition => self, &block)
|
135
|
+
::Trax::Core::NamedClass.new(klass_name, "::Trax::Core::Types::#{type_name.to_s.classify}".constantize, :parent_definition => self, **options, &block)
|
119
136
|
end
|
120
137
|
|
121
138
|
options[:default] = options.key?(:default) ? options[:default] : DEFAULT_VALUES_FOR_PROPERTY_TYPES[type_name]
|
122
139
|
property(property_name.to_sym, *args, **options)
|
123
140
|
|
124
141
|
if coerce.is_a?(::Proc)
|
125
|
-
coerce_key(property_name.to_sym,
|
142
|
+
coerce_key(property_name.to_sym, coerce)
|
126
143
|
elsif coerce.is_a?(::Array)
|
127
144
|
coerce_key(property_name.to_sym, coerce)
|
128
145
|
elsif [ ::Integer, ::Float, ::String ].include?(coerce)
|
data/lib/trax_core/version.rb
CHANGED
@@ -28,7 +28,7 @@ describe ::Trax::Core::Definitions do
|
|
28
28
|
end
|
29
29
|
|
30
30
|
context "array" do
|
31
|
-
it { expect(subject::ProductAttributes.new.categories).to be_a(Array) }
|
31
|
+
it { expect(subject::ProductAttributes.new.categories.__getobj__).to be_a(::Array) }
|
32
32
|
context "it instantiates members into objects if provided" do
|
33
33
|
it { expect(subject::ProductAttributes.new(:categories => [ :clothing ]).categories[0].to_i).to eq 2 }
|
34
34
|
it { expect(subject::ProductAttributes.new(:categories => [ 2 ]).categories[0].to_s).to eq "clothing" }
|
@@ -2,9 +2,22 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe ::Trax::Core::Types::Struct do
|
4
4
|
before(:all) do
|
5
|
+
|
5
6
|
module MyFakeStructNamespace
|
6
7
|
extend ::Trax::Core::Definitions
|
7
8
|
|
9
|
+
struct :Location do
|
10
|
+
string :region
|
11
|
+
enum :continent do
|
12
|
+
define :north_america, 1
|
13
|
+
define :south_america, 2
|
14
|
+
define :Europe, 3
|
15
|
+
define :Asia, 4
|
16
|
+
end
|
17
|
+
|
18
|
+
time :created_at
|
19
|
+
end
|
20
|
+
|
8
21
|
struct :Locale do
|
9
22
|
string :en, :default => ""
|
10
23
|
string :da, :default => ""
|
@@ -13,21 +26,91 @@ describe ::Trax::Core::Types::Struct do
|
|
13
26
|
struct :territories do
|
14
27
|
string :en, :default => "US"
|
15
28
|
end
|
29
|
+
|
30
|
+
array :locations, :of => "MyFakeStructNamespace::Location"
|
31
|
+
|
32
|
+
boolean :is_whatever, :default => true
|
33
|
+
|
34
|
+
json :phone_formats
|
35
|
+
json :date_formats, :default => {:en_US => "%m-%d-%y"}
|
16
36
|
end
|
17
37
|
end
|
18
38
|
end
|
19
39
|
|
20
40
|
subject { "::MyFakeStructNamespace::Locale".constantize.new(:en => "something") }
|
21
41
|
|
42
|
+
let!(:fake_time1) {
|
43
|
+
@fake_time1 = ::Time.stub(:now).and_return(::Time.mktime(1970,1,1))
|
44
|
+
}
|
45
|
+
let!(:fake_time2) {
|
46
|
+
@fake_time2 = ::Time.stub(:now).and_return(::Time.mktime(1971,1,1))
|
47
|
+
}
|
48
|
+
|
22
49
|
it { expect(subject).to have_key("en") }
|
23
50
|
it { expect(subject.en).to eq "something" }
|
24
51
|
it { expect(subject.da).to eq "" }
|
25
52
|
it { expect(subject.ca).to eq "eh" }
|
53
|
+
|
26
54
|
it { expect(subject.territories.en).to eq "US" }
|
27
55
|
|
56
|
+
it { expect(subject.phone_formats).to eq({}) }
|
57
|
+
it { expect(subject.date_formats).to eq('en_US' => "%m-%d-%y") }
|
58
|
+
|
28
59
|
context "unknown value" do
|
29
60
|
subject { "::MyFakeStructNamespace::Locale".constantize.new(:blah => "something") }
|
30
61
|
it { expect(subject).to_not respond_to(:blah) }
|
31
62
|
it { expect(subject.en).to eq "" }
|
32
63
|
end
|
64
|
+
|
65
|
+
context "with json property" do
|
66
|
+
let(:locale) { :en_GB }
|
67
|
+
let(:date_format) { "%d/%m/%Y" }
|
68
|
+
let(:definition){ "::MyFakeStructNamespace::Locale".constantize.new(:date_formats => {locale => date_format}) }
|
69
|
+
subject { definition.date_formats }
|
70
|
+
|
71
|
+
it { expect(subject[locale.to_s]).to eq date_format }
|
72
|
+
it { expect(subject[locale.to_sym]).to eq date_format }
|
73
|
+
end
|
74
|
+
|
75
|
+
context "with array_of property" do
|
76
|
+
let(:definition) { "::MyFakeStructNamespace::Locale".constantize.new(:locations => [{:continent => :asia}]) }
|
77
|
+
|
78
|
+
subject { definition }
|
79
|
+
it { expect(definition.locations.first.continent.to_i).to eq(4) }
|
80
|
+
|
81
|
+
context "casts to empty array" do
|
82
|
+
let(:definition) { "::MyFakeStructNamespace::Locale".constantize.new }
|
83
|
+
it { expect(definition.locations.length).to eq 0 }
|
84
|
+
it { expect(definition.locations).to_not eq nil }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context "with boolean property" do
|
89
|
+
let(:definition) { "::MyFakeStructNamespace::Locale".constantize }
|
90
|
+
subject { definition.new(:is_whatever => false) }
|
91
|
+
|
92
|
+
it { expect(subject.is_whatever).to eq false }
|
93
|
+
context "default" do
|
94
|
+
subject { definition.new }
|
95
|
+
it{ expect(subject.is_whatever).to eq true }
|
96
|
+
end
|
97
|
+
|
98
|
+
context "coercion" do
|
99
|
+
subject { definition.new(:is_whatever => '') }
|
100
|
+
it{ expect(subject.is_whatever).to eq false }
|
101
|
+
it{ expect(subject.is_whatever).to be_falsey }
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context "with time property" do
|
106
|
+
let(:definition) { "::MyFakeStructNamespace::Location".constantize }
|
107
|
+
subject { definition.new(:created_at => fake_time1) }
|
108
|
+
it { expect(subject.created_at).to eq fake_time1 }
|
109
|
+
|
110
|
+
context do
|
111
|
+
let(:db_timestamp) { "2015-12-05 15:34:57.701289" }
|
112
|
+
let(:test_subject) { definition.new(:created_at => db_timestamp) }
|
113
|
+
it { test_subject.created_at.should be_a(::Time) }
|
114
|
+
end
|
115
|
+
end
|
33
116
|
end
|
data/trax_core.gemspec
CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_dependency "hashie"
|
22
22
|
spec.add_dependency "activesupport"
|
23
23
|
spec.add_dependency "activemodel"
|
24
|
+
spec.add_dependency "wannabe_bool"
|
24
25
|
|
25
26
|
spec.add_development_dependency "bundler", "~> 1.6"
|
26
27
|
spec.add_development_dependency "rake"
|
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.83
|
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-12-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashie
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: wannabe_bool
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: bundler
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -224,8 +238,10 @@ files:
|
|
224
238
|
- lib/trax/core/types/enum_value.rb
|
225
239
|
- lib/trax/core/types/float.rb
|
226
240
|
- lib/trax/core/types/integer.rb
|
241
|
+
- lib/trax/core/types/json.rb
|
227
242
|
- lib/trax/core/types/string.rb
|
228
243
|
- lib/trax/core/types/struct.rb
|
244
|
+
- lib/trax/core/types/time.rb
|
229
245
|
- lib/trax/core/types/value_object.rb
|
230
246
|
- lib/trax_core.rb
|
231
247
|
- lib/trax_core/version.rb
|