trax_core 0.0.84 → 0.0.85
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +2 -2
- data/lib/trax/core/definitions.rb +4 -4
- data/lib/trax/core/ext/hash.rb +15 -5
- data/lib/trax/core/ext/method.rb +95 -0
- data/lib/trax/core/ext/object.rb +7 -2
- data/lib/trax/core/fields.rb +4 -0
- data/lib/trax/core/has_mixins.rb +5 -1
- data/lib/trax/core/transformer.rb +244 -0
- data/lib/trax/core/types/boolean.rb +1 -0
- data/lib/trax/core/types/enum.rb +7 -8
- data/lib/trax/core/types/enum_value.rb +4 -10
- data/lib/trax/core/types/json.rb +8 -0
- data/lib/trax/core/types/struct.rb +45 -4
- data/lib/trax/core/types/value_object.rb +7 -1
- data/lib/trax/core.rb +13 -0
- data/lib/trax_core/version.rb +1 -1
- data/spec/support/defs.rb +29 -1
- data/spec/support/storefront/product.rb +2 -0
- data/spec/trax/array_spec.rb +2 -6
- data/spec/trax/core/definitions_spec.rb +76 -2
- data/spec/trax/core/eager_autoload_namespace_spec.rb +4 -4
- data/spec/trax/core/errors_spec.rb +10 -15
- data/spec/trax/core/ext/array_spec.rb +2 -2
- data/spec/trax/core/ext/class_spec.rb +3 -3
- data/spec/trax/core/ext/hash_spec.rb +10 -0
- data/spec/trax/core/ext/method_spec.rb +104 -0
- data/spec/trax/core/ext/module_spec.rb +5 -5
- data/spec/trax/core/ext/object_spec.rb +5 -5
- data/spec/trax/core/transformer_spec.rb +170 -0
- data/spec/trax/core/types/array_spec.rb +4 -4
- data/spec/trax/core/types/enum_spec.rb +23 -18
- data/spec/trax/core/types/struct_spec.rb +50 -7
- data/spec/trax/core/types/value_object_spec.rb +6 -0
- data/spec/trax/core_spec.rb +1 -3
- data/spec/trax/hash_spec.rb +13 -15
- data/trax_core.gemspec +7 -6
- metadata +97 -5
- data/spec/trax/core/inheritance_spec.rb +0 -0
data/lib/trax/core.rb
CHANGED
@@ -4,6 +4,7 @@ require_relative './core/fs'
|
|
4
4
|
require_relative './core/ext/array'
|
5
5
|
require_relative './core/ext/class'
|
6
6
|
require_relative './core/ext/enumerable'
|
7
|
+
require_relative './core/ext/method'
|
7
8
|
require_relative './core/ext/module'
|
8
9
|
require_relative './core/ext/hash'
|
9
10
|
require_relative './core/ext/object'
|
@@ -14,6 +15,17 @@ require_relative './core/ext/uri'
|
|
14
15
|
require_relative './core/primitives/enum_value'
|
15
16
|
require_relative './core/primitives/enum'
|
16
17
|
|
18
|
+
if(::RUBY_VERSION < '2.3')
|
19
|
+
begin
|
20
|
+
require 'ruby_dig'
|
21
|
+
rescue LoadError => e
|
22
|
+
raise unless e.message =~ /ruby_dig/
|
23
|
+
error = e.exception("Trax::Core::Transformer relies on ruby_dig shim, add gem 'ruby_dig' to gemfile")
|
24
|
+
error.set_backtrace(e.backtrace)
|
25
|
+
raise error
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
17
29
|
module Trax
|
18
30
|
module Core
|
19
31
|
extend ::ActiveSupport::Autoload
|
@@ -37,6 +49,7 @@ module Trax
|
|
37
49
|
autoload :NamedClass
|
38
50
|
autoload :NamedModule
|
39
51
|
autoload :SilenceWarnings
|
52
|
+
autoload :Transformer
|
40
53
|
autoload :Types
|
41
54
|
autoload :PathPermutations
|
42
55
|
end
|
data/lib/trax_core/version.rb
CHANGED
data/spec/support/defs.rb
CHANGED
@@ -8,6 +8,19 @@ module Defs
|
|
8
8
|
define :accessories, 4
|
9
9
|
end
|
10
10
|
|
11
|
+
struct :ShipmentAttributes do
|
12
|
+
string :tracking_number
|
13
|
+
float :weight
|
14
|
+
integer :insurance_amount
|
15
|
+
boolean :is_insured
|
16
|
+
array :notes
|
17
|
+
enum :shipping_type do
|
18
|
+
define :standard, 1
|
19
|
+
define :overnight, 2
|
20
|
+
define :same_day, 3
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
11
24
|
struct :ProductAttributes do
|
12
25
|
string :name, :default => ""
|
13
26
|
float :price, :default => 9.99
|
@@ -16,7 +29,7 @@ module Defs
|
|
16
29
|
array :categories, :of => "Defs::Category", :default => []
|
17
30
|
end
|
18
31
|
|
19
|
-
struct :ShoesAttributes, :
|
32
|
+
struct :ShoesAttributes, :extends => "Defs::ProductAttributes" do
|
20
33
|
enum :size do
|
21
34
|
define :mens_8, 1
|
22
35
|
define :mens_9, 2
|
@@ -25,4 +38,19 @@ module Defs
|
|
25
38
|
define :mens_12, 5
|
26
39
|
end
|
27
40
|
end
|
41
|
+
|
42
|
+
struct :ShirtAttributes, :extends => "Defs::ProductAttributes" do
|
43
|
+
string :name, :default => "Three-Fifty"
|
44
|
+
float :price, :default => 3.50
|
45
|
+
integer :quantity_in_stock, :default => 5
|
46
|
+
boolean :is_active, :default => true
|
47
|
+
array :categories, :of => "Defs::Category", :default => [:default, :clothing]
|
48
|
+
enum :size, :default => :womens_m do
|
49
|
+
define :womens_xs, 1
|
50
|
+
define :womens_s, 2
|
51
|
+
define :womens_m, 3
|
52
|
+
define :womens_l, 4
|
53
|
+
define :womens_xl, 5
|
54
|
+
end
|
55
|
+
end
|
28
56
|
end
|
data/spec/trax/array_spec.rb
CHANGED
@@ -9,9 +9,7 @@ describe ::Array do
|
|
9
9
|
end
|
10
10
|
|
11
11
|
describe "#to_proc" do
|
12
|
-
it{
|
13
|
-
subject.map(&[:name, :price]).map(&:last).sum.should eq 90
|
14
|
-
}
|
12
|
+
it { expect(subject.map(&[:name, :price]).map(&:last).sum).to eq 90 }
|
15
13
|
|
16
14
|
context "array of non hash objects" do
|
17
15
|
subject {
|
@@ -21,9 +19,7 @@ describe ::Array do
|
|
21
19
|
]
|
22
20
|
}
|
23
21
|
|
24
|
-
it {
|
25
|
-
subject.map(&[:name, :price]).map(&[:price]).sum.should eq 90
|
26
|
-
}
|
22
|
+
it { expect(subject.map(&[:name, :price]).map(&[:price]).sum).to eq 90 }
|
27
23
|
end
|
28
24
|
end
|
29
25
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe ::Trax::Core::Definitions do
|
4
|
-
subject { ::Defs }
|
4
|
+
subject(:definitions) { ::Defs }
|
5
5
|
|
6
6
|
context "fields" do
|
7
|
-
it { subject[:category].
|
7
|
+
it { expect(subject[:category]).to eq subject::Category }
|
8
8
|
end
|
9
9
|
|
10
10
|
context "enum" do
|
@@ -42,4 +42,78 @@ describe ::Trax::Core::Definitions do
|
|
42
42
|
it { expect(subject::ShoesAttributes.new).to have_key(:price) }
|
43
43
|
it { expect(subject::ShoesAttributes.new(:size => :mens_8).size.to_i).to eq 1 }
|
44
44
|
end
|
45
|
+
|
46
|
+
context "schema" do
|
47
|
+
let(:schema) { definitions.to_schema }
|
48
|
+
|
49
|
+
context "field defaults" do
|
50
|
+
context "with defaults" do
|
51
|
+
subject { schema[:shirt_attributes][:fields][field_name][:default] }
|
52
|
+
|
53
|
+
context "string" do
|
54
|
+
let(:field_name) { :name }
|
55
|
+
it { is_expected.to eq("Three-Fifty") }
|
56
|
+
end
|
57
|
+
|
58
|
+
context "float" do
|
59
|
+
let(:field_name) { :price }
|
60
|
+
it { is_expected.to eq(3.50) }
|
61
|
+
end
|
62
|
+
|
63
|
+
context "integer" do
|
64
|
+
let(:field_name) { :quantity_in_stock }
|
65
|
+
it { is_expected.to eq(5) }
|
66
|
+
end
|
67
|
+
|
68
|
+
context "boolean" do
|
69
|
+
let(:field_name) { :is_active }
|
70
|
+
it { is_expected.to eq(true) }
|
71
|
+
end
|
72
|
+
|
73
|
+
context "array" do
|
74
|
+
let(:field_name) { :categories }
|
75
|
+
it { is_expected.to eq([:default, :clothing]) }
|
76
|
+
end
|
77
|
+
|
78
|
+
context "enum" do
|
79
|
+
let(:field_name) { :size }
|
80
|
+
it { is_expected.to eq(:womens_m) }
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context "without defaults" do
|
85
|
+
subject { schema[:shipment_attributes][:fields][field_name] }
|
86
|
+
|
87
|
+
context "string" do
|
88
|
+
let(:field_name) { :tracking_number }
|
89
|
+
it { is_expected.to_not have_key(:default) }
|
90
|
+
end
|
91
|
+
|
92
|
+
context "float" do
|
93
|
+
let(:field_name) { :weight }
|
94
|
+
it { is_expected.to_not have_key(:default) }
|
95
|
+
end
|
96
|
+
|
97
|
+
context "integer" do
|
98
|
+
let(:field_name) { :insurance_amount }
|
99
|
+
it { is_expected.to_not have_key(:default) }
|
100
|
+
end
|
101
|
+
|
102
|
+
context "boolean" do
|
103
|
+
let(:field_name) { :is_insured }
|
104
|
+
it { is_expected.to_not have_key(:default) }
|
105
|
+
end
|
106
|
+
|
107
|
+
context "array" do
|
108
|
+
let(:field_name) { :notes }
|
109
|
+
it { is_expected.to_not have_key(:default) }
|
110
|
+
end
|
111
|
+
|
112
|
+
context "enum" do
|
113
|
+
let(:field_name) { :shipping_type }
|
114
|
+
it { is_expected.to_not have_key(:default) }
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
45
119
|
end
|
@@ -3,8 +3,8 @@ require 'spec_helper'
|
|
3
3
|
describe ::Trax::Core::EagerAutoloadNamespace do
|
4
4
|
subject { ::Ecom }
|
5
5
|
|
6
|
-
its('autoload_class_names.length') {
|
7
|
-
its('autoload_file_paths.length') {
|
8
|
-
its(:eager_autoload_filepath) {
|
9
|
-
its(:module_path) {
|
6
|
+
its('autoload_class_names.length') { is_expected.to eq 2 }
|
7
|
+
its('autoload_file_paths.length') { is_expected.to eq 2 }
|
8
|
+
its(:eager_autoload_filepath) { is_expected.to include('ecom') }
|
9
|
+
its(:module_path) { is_expected.to be_a(::Pathname) }
|
10
10
|
end
|
@@ -1,23 +1,18 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe ::Trax::Core::Errors do
|
4
|
+
let(:error_attributes) do
|
5
|
+
{
|
6
|
+
:request_url => 'http://www.somewhere.com',
|
7
|
+
:request_id => 'asdasdsdad1231421'
|
8
|
+
}
|
9
|
+
end
|
10
|
+
|
4
11
|
subject { ::Errors::SiteBrokenError }
|
5
12
|
|
6
|
-
it { expect{ subject.new }.to raise_error(ArgumentError) }
|
13
|
+
it { expect { subject.new }.to raise_error(ArgumentError) }
|
7
14
|
|
8
|
-
it
|
9
|
-
expect do
|
10
|
-
raise subject.new(
|
11
|
-
:request_url => 'http://www.somewhere.com',
|
12
|
-
:request_id => 'asdasdsdad1231421'
|
13
|
-
)
|
14
|
-
end.to raise_error(subject)
|
15
|
-
end
|
15
|
+
it { expect { raise subject.new(error_attributes) }.to raise_error(subject) }
|
16
16
|
|
17
|
-
it
|
18
|
-
subject.new(
|
19
|
-
:request_url => 'http://www.somewhere.com',
|
20
|
-
:request_id => 'asdasdsdad1231421'
|
21
|
-
).to_s.should include('http://www.somewhere.com')
|
22
|
-
end
|
17
|
+
it { expect(subject.new(error_attributes).to_s).to include('http://www.somewhere.com') }
|
23
18
|
end
|
@@ -3,9 +3,9 @@ require 'spec_helper'
|
|
3
3
|
describe Array do
|
4
4
|
subject { ["one", "two", "three", "one", ["two"]]}
|
5
5
|
|
6
|
-
it { subject.flat_compact_uniq
|
6
|
+
it { expect(subject.flat_compact_uniq!).to eq ["one", "two", "three"] }
|
7
7
|
it "modifies array in place" do
|
8
8
|
test_subject = subject.flat_compact_uniq!
|
9
|
-
test_subject.object_id.
|
9
|
+
expect(test_subject.object_id).to eq subject.object_id
|
10
10
|
end
|
11
11
|
end
|
@@ -2,11 +2,11 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe ::Class do
|
4
4
|
describe ".superclasses_until" do
|
5
|
-
it
|
6
|
-
::InheritanceChainNamespace::D.superclasses_until(::InheritanceChainNamespace::A).
|
5
|
+
it do
|
6
|
+
expect(::InheritanceChainNamespace::D.superclasses_until(::InheritanceChainNamespace::A)).to eq [
|
7
7
|
::InheritanceChainNamespace::B,
|
8
8
|
::InheritanceChainNamespace::C
|
9
9
|
]
|
10
|
-
|
10
|
+
end
|
11
11
|
end
|
12
12
|
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hash do
|
4
|
+
subject { {:one => :two, :three => :four } }
|
5
|
+
|
6
|
+
describe "#assert_required_keys" do
|
7
|
+
it { expect { subject.assert_required_keys(:five) }.to raise_error(ArgumentError) }
|
8
|
+
it { expect { subject.assert_required_keys(:one) }.not_to raise_error }
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Method do
|
4
|
+
before(:all) do
|
5
|
+
class MyNewFakeClass < OpenStruct
|
6
|
+
def self.i_dont_accept_arguments
|
7
|
+
"one"
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.i_accept_ordinal_arguments(one, two)
|
11
|
+
return [one, two]
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.i_accept_optional_ordinal_arguments(one=nil, two=nil)
|
15
|
+
return [one, two]
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.i_accept_splat_arguments(*args)
|
19
|
+
return args
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.i_accept_keyword_arguments(three:nil, one:, two:)
|
23
|
+
return [one, two]
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.i_accept_keyword_arguments_splat(**options)
|
27
|
+
return options
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
subject { MyNewFakeClass.method(target_method) }
|
33
|
+
|
34
|
+
let(:fake_args) { ['something'] }
|
35
|
+
let(:fake_params) { {:else => 'anything'} }
|
36
|
+
|
37
|
+
context "method knows what it accepts" do
|
38
|
+
describe '.i_dont_accept_arguments' do
|
39
|
+
let(:target_method) { 'i_dont_accept_arguments' }
|
40
|
+
|
41
|
+
it { expect(subject.accepts_arguments?).to eq false }
|
42
|
+
it { expect(subject.accepts_keywords?).to eq false }
|
43
|
+
it { expect(subject.accepts_arguments_splat?).to eq false }
|
44
|
+
it {
|
45
|
+
expect(subject).to receive(:strategy_for_method_without_arguments)
|
46
|
+
subject.execute_call_strategy(*fake_args, **fake_params)
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
describe '.i_accept_ordinal_arguments' do
|
51
|
+
let(:target_method) { 'i_accept_ordinal_arguments' }
|
52
|
+
|
53
|
+
it { expect(subject.accepts_arguments?).to eq true }
|
54
|
+
it { expect(subject.accepts_keywords?).to eq false }
|
55
|
+
it { expect(subject.accepts_arguments_splat?).to eq false }
|
56
|
+
it {
|
57
|
+
expect(subject).to receive(:strategy_for_method_with_arguments)
|
58
|
+
subject.execute_call_strategy(*fake_args, **fake_params)
|
59
|
+
}
|
60
|
+
end
|
61
|
+
|
62
|
+
describe '.i_accept_optional_ordinal_arguments' do
|
63
|
+
let(:target_method) { 'i_accept_optional_ordinal_arguments' }
|
64
|
+
|
65
|
+
it { expect(subject.accepts_arguments?).to eq true }
|
66
|
+
it { expect(subject.accepts_optional_arguments?).to eq true }
|
67
|
+
it { expect(subject.accepts_keywords?).to eq false }
|
68
|
+
it { expect(subject.accepts_arguments_splat?).to eq false }
|
69
|
+
it {
|
70
|
+
expect(subject).to receive(:strategy_for_method_with_arguments)
|
71
|
+
subject.execute_call_strategy(*fake_args, **fake_params)
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
describe '.i_accept_splat_arguments' do
|
76
|
+
let(:target_method) { 'i_accept_splat_arguments' }
|
77
|
+
|
78
|
+
it { expect(subject.accepts_arguments?).to eq true }
|
79
|
+
it { expect(subject.accepts_keywords?).to eq false }
|
80
|
+
it { expect(subject.accepts_arguments_splat?).to eq true }
|
81
|
+
it {
|
82
|
+
expect(subject).to receive(:strategy_for_method_with_arguments)
|
83
|
+
subject.execute_call_strategy(*fake_args, **fake_params)
|
84
|
+
}
|
85
|
+
end
|
86
|
+
|
87
|
+
describe '.i_accept_keyword_arguments' do
|
88
|
+
let(:target_method) { 'i_accept_keyword_arguments' }
|
89
|
+
|
90
|
+
it { expect(subject.accepts_arguments?).to eq false }
|
91
|
+
it { expect(subject.accepts_keywords?).to eq true }
|
92
|
+
it { expect(subject.accepts_arguments_splat?).to eq false }
|
93
|
+
end
|
94
|
+
|
95
|
+
describe '.i_accept_keyword_arguments_splat' do
|
96
|
+
let(:target_method) { 'i_accept_keyword_arguments_splat' }
|
97
|
+
|
98
|
+
it { expect(subject.accepts_arguments?).to eq false }
|
99
|
+
it { expect(subject.accepts_arguments_splat?).to eq false }
|
100
|
+
it { expect(subject.accepts_keywords?).to eq true }
|
101
|
+
it { expect(subject.accepts_keywords_splat?).to eq true }
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -10,7 +10,7 @@ describe ::Module do
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
it { Trax.some_fake_hash.
|
13
|
+
it { expect(Trax.some_fake_hash).to be_a(Hash) }
|
14
14
|
end
|
15
15
|
|
16
16
|
describe ".recursively_define_namespaced_class" do
|
@@ -27,7 +27,7 @@ describe ::Module do
|
|
27
27
|
Trax.recursively_define_namespaced_class("Core::FakeKlass", Hash)
|
28
28
|
end
|
29
29
|
|
30
|
-
it { Trax::Core::FakeKlass.superclass.
|
30
|
+
it { expect(Trax::Core::FakeKlass.superclass).to eq Hash }
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -42,14 +42,14 @@ describe ::Module do
|
|
42
42
|
|
43
43
|
subject{ ::Trax::Core::FakeNamespace::Configuration }
|
44
44
|
|
45
|
-
its(:superclass) {
|
45
|
+
its(:superclass) { is_expected.to eq ::Trax::Core::Configuration }
|
46
46
|
|
47
47
|
it "sets up configuration options" do
|
48
48
|
::Trax::Core::FakeNamespace.configure do |config|
|
49
49
|
config.something = 'anything'
|
50
50
|
end
|
51
51
|
|
52
|
-
::Trax::Core::FakeNamespace.config.something.
|
52
|
+
expect(::Trax::Core::FakeNamespace.config.something).to eq 'anything'
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
@@ -84,7 +84,7 @@ describe ::Module do
|
|
84
84
|
config.base_url = 'somewhere'
|
85
85
|
end
|
86
86
|
|
87
|
-
::Trax::Core::YetAnotherFakeClass.config.allowed_ips[0].
|
87
|
+
expect(::Trax::Core::YetAnotherFakeClass.config.allowed_ips[0]).to eq '192.231.1234'
|
88
88
|
end
|
89
89
|
end
|
90
90
|
end
|
@@ -12,22 +12,22 @@ describe ::Object do
|
|
12
12
|
|
13
13
|
describe ".try_chain" do
|
14
14
|
it "wraps multiple calls to try and calls try on return value" do
|
15
|
-
subject.try_chain(:name, :underscore).
|
15
|
+
expect(subject.try_chain(:name, :underscore)).to eq "some_fake_class"
|
16
16
|
end
|
17
17
|
|
18
18
|
it "handles return if nil is in chain" do
|
19
|
-
subject.try_chain(:name, :somefakemethod).
|
19
|
+
expect(subject.try_chain(:name, :somefakemethod)).to be_nil
|
20
20
|
end
|
21
21
|
|
22
22
|
it "handles instances of class" do
|
23
|
-
SomeFakeClass.new.try_chain(:class, :name, :underscore).
|
23
|
+
expect(SomeFakeClass.new.try_chain(:class, :name, :underscore)).to eq "some_fake_class"
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
27
|
describe ".set_fully_qualified_constant" do
|
28
28
|
it "sets a fully qualified constant" do
|
29
29
|
result = Object.set_fully_qualified_constant("SomeFakeClass::SomeFakeNestedClass", Class.new)
|
30
|
-
result.name.
|
30
|
+
expect(result.name).to eq "SomeFakeClass::SomeFakeNestedClass"
|
31
31
|
end
|
32
32
|
|
33
33
|
it "raises error if no valid namespace to set constant upon is passed" do
|
@@ -40,7 +40,7 @@ describe ::Object do
|
|
40
40
|
obj = SomeFakeClass.new
|
41
41
|
obj.instance_variable_set(:@someivar, "anything")
|
42
42
|
obj.reset_instance_variables(:someivar)
|
43
|
-
obj.instance_variable_get(:@someivar).
|
43
|
+
expect(obj.instance_variable_get(:@someivar)).to be nil
|
44
44
|
end
|
45
45
|
end
|
46
46
|
end
|
@@ -0,0 +1,170 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ::Trax::Core::Transformer do
|
4
|
+
let(:payload) do
|
5
|
+
{
|
6
|
+
"name" => "Uber",
|
7
|
+
"legalName" => "Uber, Inc.",
|
8
|
+
"url" => "http =>//uber.com",
|
9
|
+
"metrics" => {
|
10
|
+
"raised" => 1502450000,
|
11
|
+
"annualRevenue" => 20000
|
12
|
+
},
|
13
|
+
"somethingElse" => "somethingElseValue",
|
14
|
+
"some_value_transform" => 10,
|
15
|
+
"some_value_to_times_by" => 2,
|
16
|
+
"some_value" => 30,
|
17
|
+
"stats" => {
|
18
|
+
"number_of_widgets" => 20,
|
19
|
+
"number_of_employees" => 40
|
20
|
+
},
|
21
|
+
"some_object" => {
|
22
|
+
"thing" => 1
|
23
|
+
},
|
24
|
+
"some_other_object" => {
|
25
|
+
"some_value" => 2
|
26
|
+
},
|
27
|
+
"some_unmapped_var" => 2
|
28
|
+
}.with_indifferent_access
|
29
|
+
end
|
30
|
+
|
31
|
+
before do
|
32
|
+
class PayloadTransformer < ::Trax::Core::Transformer
|
33
|
+
property "name"
|
34
|
+
property "something", :default => ->(v) { "anything" }
|
35
|
+
property "some_non_proc_default", :default => 5
|
36
|
+
property "legal_name", :from => "legalName"
|
37
|
+
|
38
|
+
property "some_value"
|
39
|
+
property "some_value_to_times_by"
|
40
|
+
property "some_value_transform", :default => ->(i){ 1 } do |value, instance|
|
41
|
+
value * instance["some_value_to_times_by"]
|
42
|
+
end
|
43
|
+
|
44
|
+
transformer "stats" do
|
45
|
+
property "number_of_employees"
|
46
|
+
property "raised", :from_parent => "metrics/raised"
|
47
|
+
property "google_rank", :from_parent => "metrics/googleRank"
|
48
|
+
end
|
49
|
+
|
50
|
+
transformer "website" do
|
51
|
+
property "url", :from_parent => "url"
|
52
|
+
end
|
53
|
+
|
54
|
+
transformer "some_object" do
|
55
|
+
property "thing"
|
56
|
+
|
57
|
+
after_transform do
|
58
|
+
OpenStruct.new(self)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
transformer "some_other_object" do
|
63
|
+
property "some_value"
|
64
|
+
|
65
|
+
after_transform do |result|
|
66
|
+
self['result'] = result['some_value'] * self.parent.input["some_unmapped_var"]
|
67
|
+
OpenStruct.new(self)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
subject {
|
74
|
+
PayloadTransformer.new(payload)
|
75
|
+
}
|
76
|
+
|
77
|
+
context "class methods" do
|
78
|
+
describe ".transformer" do
|
79
|
+
it {
|
80
|
+
expect(subject.class.properties['stats'].is_nested?).to eq true
|
81
|
+
}
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context "property mapping strategies" do
|
86
|
+
it "property declration without options" do
|
87
|
+
expect(subject["name"]).to eq "Uber"
|
88
|
+
end
|
89
|
+
|
90
|
+
context "property with default value" do
|
91
|
+
it "value is a proc" do
|
92
|
+
expect(subject["something"]).to eq "anything"
|
93
|
+
end
|
94
|
+
|
95
|
+
it "value is not a proc" do
|
96
|
+
expect(subject["some_non_proc_default"]).to eq 5
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context "property key translated" do
|
101
|
+
it {
|
102
|
+
expect(subject["legal_name"]).to eq "Uber, Inc."
|
103
|
+
}
|
104
|
+
|
105
|
+
it "removes translated value from hash" do
|
106
|
+
expect(subject.to_hash).to_not have_key("legalName")
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context "nested properties" do
|
111
|
+
it "mapping from source hash" do
|
112
|
+
expect(subject["stats"]["number_of_employees"]).to eq 40
|
113
|
+
end
|
114
|
+
|
115
|
+
it "mapping from parent nested property" do
|
116
|
+
expect(subject["stats"]["raised"]).to eq payload["metrics"]["raised"]
|
117
|
+
end
|
118
|
+
|
119
|
+
it "brand new nested property" do
|
120
|
+
expect(subject["website"]["url"]).to eq payload["url"]
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
context "callbacks" do
|
125
|
+
context "after transform" do
|
126
|
+
it "runs after transform callbacks which can mutate return result" do
|
127
|
+
expect(subject["some_object"].thing).to eq 1
|
128
|
+
end
|
129
|
+
|
130
|
+
it "passes instance" do
|
131
|
+
expect(subject["some_other_object"].result).to eq 4
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
context "after transform" do
|
136
|
+
it "runs after transform callbacks which can mutate return result" do
|
137
|
+
expect(subject["some_object"].thing).to eq 1
|
138
|
+
end
|
139
|
+
|
140
|
+
it "wraps return value in class" do
|
141
|
+
expect(subject["some_object"].__getobj__.is_a?(::OpenStruct)).to eq true
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
context "to_hash" do
|
147
|
+
it "unwraps nested transformers" do
|
148
|
+
expect(subject.to_hash["some_object"].is_a?(::OpenStruct)).to eq true
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
context "to_recursive_hash" do
|
153
|
+
it "unwraps nested transformers" do
|
154
|
+
expect(subject.to_recursive_hash["some_object"].is_a?(::OpenStruct)).to eq true
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
it {
|
159
|
+
expect(subject["some_value"]).to eq 30
|
160
|
+
}
|
161
|
+
|
162
|
+
it {
|
163
|
+
expect(subject["some_value_to_times_by"]).to eq 2
|
164
|
+
}
|
165
|
+
|
166
|
+
it {
|
167
|
+
expect(subject["some_value_transform"]).to eq 20
|
168
|
+
}
|
169
|
+
end
|
170
|
+
end
|
@@ -15,8 +15,8 @@ describe ::Trax::Core::Types::Array do
|
|
15
15
|
|
16
16
|
context ".of" do
|
17
17
|
let(:test_subject) { subject.new({:width => 1}) }
|
18
|
-
it { test_subject[0].width.
|
19
|
-
it { test_subject[0].height.
|
18
|
+
it { expect(test_subject[0].width).to eq 1 }
|
19
|
+
it { expect(test_subject[0].height).to eq 0 }
|
20
20
|
|
21
21
|
context "does not duplicate values" do
|
22
22
|
it {
|
@@ -35,7 +35,7 @@ describe ::Trax::Core::Types::Array do
|
|
35
35
|
context "ArrayOf" do
|
36
36
|
subject { ::Trax::Core::Types::ArrayOf[::AnotherFakeStructNamespace::Widget] }
|
37
37
|
let(:test_subject) { subject.new({:width => 1}) }
|
38
|
-
it { test_subject[0].width.
|
39
|
-
it { test_subject[0].height.
|
38
|
+
it { expect(test_subject[0].width).to eq 1 }
|
39
|
+
it { expect(test_subject[0].height).to eq 0 }
|
40
40
|
end
|
41
41
|
end
|