faceter 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +2 -0
- data/.gitignore +9 -0
- data/.metrics +9 -0
- data/.rspec +2 -0
- data/.rubocop.yml +2 -0
- data/.travis.yml +18 -0
- data/.yardopts +3 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +9 -0
- data/Guardfile +10 -0
- data/LICENSE +21 -0
- data/README.md +295 -0
- data/Rakefile +37 -0
- data/benchmark/data.json +1 -0
- data/benchmark/faceter.rb +73 -0
- data/benchmark/rom.rb +85 -0
- data/benchmark/run.rb +54 -0
- data/config/metrics/STYLEGUIDE +230 -0
- data/config/metrics/cane.yml +5 -0
- data/config/metrics/churn.yml +6 -0
- data/config/metrics/flay.yml +2 -0
- data/config/metrics/metric_fu.yml +14 -0
- data/config/metrics/reek.yml +1 -0
- data/config/metrics/roodi.yml +24 -0
- data/config/metrics/rubocop.yml +75 -0
- data/config/metrics/saikuro.yml +3 -0
- data/config/metrics/simplecov.yml +6 -0
- data/config/metrics/yardstick.yml +37 -0
- data/faceter.gemspec +29 -0
- data/lib/faceter.rb +54 -0
- data/lib/faceter/coercers.rb +68 -0
- data/lib/faceter/functions.rb +30 -0
- data/lib/faceter/functions/add_prefix.rb +25 -0
- data/lib/faceter/functions/claster.rb +27 -0
- data/lib/faceter/functions/clean.rb +27 -0
- data/lib/faceter/functions/drop_prefix.rb +29 -0
- data/lib/faceter/functions/exclude.rb +27 -0
- data/lib/faceter/functions/group.rb +41 -0
- data/lib/faceter/functions/keep_symbol.rb +27 -0
- data/lib/faceter/functions/split.rb +28 -0
- data/lib/faceter/functions/ungroup.rb +31 -0
- data/lib/faceter/functions/unwrap.rb +32 -0
- data/lib/faceter/functions/wrap.rb +32 -0
- data/lib/faceter/mapper.rb +38 -0
- data/lib/faceter/nodes/add_prefix.rb +21 -0
- data/lib/faceter/nodes/change_prefix.rb +51 -0
- data/lib/faceter/nodes/create.rb +43 -0
- data/lib/faceter/nodes/exclude.rb +25 -0
- data/lib/faceter/nodes/field.rb +25 -0
- data/lib/faceter/nodes/fold.rb +25 -0
- data/lib/faceter/nodes/group.rb +26 -0
- data/lib/faceter/nodes/list.rb +25 -0
- data/lib/faceter/nodes/remove_prefix.rb +21 -0
- data/lib/faceter/nodes/rename.rb +25 -0
- data/lib/faceter/nodes/stringify_keys.rb +26 -0
- data/lib/faceter/nodes/symbolize_keys.rb +26 -0
- data/lib/faceter/nodes/unfold.rb +25 -0
- data/lib/faceter/nodes/ungroup.rb +26 -0
- data/lib/faceter/nodes/unwrap.rb +26 -0
- data/lib/faceter/nodes/wrap.rb +26 -0
- data/lib/faceter/rules/append_nested.rb +28 -0
- data/lib/faceter/rules/merge_branches.rb +41 -0
- data/lib/faceter/rules/merge_excludes.rb +29 -0
- data/lib/faceter/rules/merge_renames.rb +27 -0
- data/lib/faceter/rules/order_fields.rb +27 -0
- data/lib/faceter/rules/prepend_nested.rb +51 -0
- data/lib/faceter/version.rb +11 -0
- data/spec/integration/commands/add_prefix_spec.rb +37 -0
- data/spec/integration/commands/create_spec.rb +33 -0
- data/spec/integration/commands/exclude_spec.rb +55 -0
- data/spec/integration/commands/fold_spec.rb +41 -0
- data/spec/integration/commands/group_spec.rb +64 -0
- data/spec/integration/commands/remove_prefix_spec.rb +63 -0
- data/spec/integration/commands/rename_spec.rb +45 -0
- data/spec/integration/commands/stringify_keys_spec.rb +65 -0
- data/spec/integration/commands/symbolize_keys_spec.rb +49 -0
- data/spec/integration/commands/unfold_spec.rb +41 -0
- data/spec/integration/commands/ungroup_spec.rb +64 -0
- data/spec/integration/commands/unwrap_spec.rb +51 -0
- data/spec/integration/commands/wrap_spec.rb +51 -0
- data/spec/integration/rom_spec.rb +39 -0
- data/spec/spec_helper.rb +25 -0
- data/spec/unit/coercers/create_spec.rb +16 -0
- data/spec/unit/coercers/exclude_spec.rb +31 -0
- data/spec/unit/coercers/field_spec.rb +11 -0
- data/spec/unit/coercers/fold_spec.rb +11 -0
- data/spec/unit/coercers/prefix_spec.rb +38 -0
- data/spec/unit/coercers/rename_spec.rb +11 -0
- data/spec/unit/coercers/unfold_spec.rb +11 -0
- data/spec/unit/coercers/unwrap_spec.rb +35 -0
- data/spec/unit/coercers/wrap_spec.rb +35 -0
- data/spec/unit/functions/add_prefix_spec.rb +12 -0
- data/spec/unit/functions/claster_spec.rb +12 -0
- data/spec/unit/functions/clean_spec.rb +18 -0
- data/spec/unit/functions/drop_prefix_spec.rb +33 -0
- data/spec/unit/functions/exclude_spec.rb +64 -0
- data/spec/unit/functions/group_spec.rb +176 -0
- data/spec/unit/functions/keep_symbol_spec.rb +21 -0
- data/spec/unit/functions/split_spec.rb +64 -0
- data/spec/unit/functions/ungroup_spec.rb +87 -0
- data/spec/unit/functions/unwrap_spec.rb +54 -0
- data/spec/unit/functions/wrap_spec.rb +67 -0
- data/spec/unit/nodes/add_prefix_spec.rb +62 -0
- data/spec/unit/nodes/create_spec.rb +53 -0
- data/spec/unit/nodes/exclude_spec.rb +18 -0
- data/spec/unit/nodes/field_spec.rb +30 -0
- data/spec/unit/nodes/fold_spec.rb +19 -0
- data/spec/unit/nodes/group_spec.rb +163 -0
- data/spec/unit/nodes/list_spec.rb +27 -0
- data/spec/unit/nodes/remove_prefix_spec.rb +62 -0
- data/spec/unit/nodes/rename_spec.rb +16 -0
- data/spec/unit/nodes/stringify_keys_spec.rb +21 -0
- data/spec/unit/nodes/symbolize_keys_spec.rb +21 -0
- data/spec/unit/nodes/unfold_spec.rb +19 -0
- data/spec/unit/nodes/ungroup_spec.rb +92 -0
- data/spec/unit/nodes/unwrap_spec.rb +47 -0
- data/spec/unit/nodes/wrap_spec.rb +33 -0
- data/spec/unit/rules/append_nested_spec.rb +41 -0
- data/spec/unit/rules/merge_branches_spec.rb +58 -0
- data/spec/unit/rules/merge_excludes_spec.rb +31 -0
- data/spec/unit/rules/merge_renames_spec.rb +29 -0
- data/spec/unit/rules/order_fields_spec.rb +31 -0
- data/spec/unit/rules/prepend_nested_spec.rb +41 -0
- metadata +315 -0
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
describe "symbolize_keys" do
|
4
|
+
|
5
|
+
subject { mapper.new.call input }
|
6
|
+
|
7
|
+
let(:input) do
|
8
|
+
[
|
9
|
+
{
|
10
|
+
"id" => 1,
|
11
|
+
"user" => {
|
12
|
+
"name" => "joe",
|
13
|
+
emails: [{ "address" => "joe@doe.com" }]
|
14
|
+
},
|
15
|
+
"roles" => [{ "name" => "admin" }]
|
16
|
+
}
|
17
|
+
]
|
18
|
+
end
|
19
|
+
|
20
|
+
let(:output) do
|
21
|
+
[
|
22
|
+
{
|
23
|
+
id: 1,
|
24
|
+
user: {
|
25
|
+
name: "joe",
|
26
|
+
emails: [{ address: "joe@doe.com" }]
|
27
|
+
},
|
28
|
+
roles: [{ "name" => "admin" }]
|
29
|
+
}
|
30
|
+
]
|
31
|
+
end
|
32
|
+
|
33
|
+
let(:mapper) do
|
34
|
+
Class.new(Faceter::Mapper) do
|
35
|
+
list do
|
36
|
+
symbolize_keys nested: false
|
37
|
+
|
38
|
+
field :user do
|
39
|
+
symbolize_keys
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
it "works" do
|
46
|
+
expect(subject).to eql output
|
47
|
+
end
|
48
|
+
|
49
|
+
end # describe symbolize_keys
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
describe "fold" do
|
4
|
+
|
5
|
+
subject { mapper.new.call input }
|
6
|
+
|
7
|
+
let(:input) do
|
8
|
+
[
|
9
|
+
{
|
10
|
+
name: "Joe",
|
11
|
+
emails: [{ address: "joe@doe.com" }, { address: "joe@doe.org" }]
|
12
|
+
},
|
13
|
+
{
|
14
|
+
name: "Jane",
|
15
|
+
emails: [{ address: "jane@doe.com" }]
|
16
|
+
}
|
17
|
+
]
|
18
|
+
end
|
19
|
+
|
20
|
+
let(:output) do
|
21
|
+
[
|
22
|
+
{ name: "Joe", emails: ["joe@doe.com", "joe@doe.org"] },
|
23
|
+
{ name: "Jane", emails: ["jane@doe.com"] }
|
24
|
+
]
|
25
|
+
end
|
26
|
+
|
27
|
+
let(:mapper) do
|
28
|
+
Class.new(Faceter::Mapper) do
|
29
|
+
list do
|
30
|
+
field :emails do
|
31
|
+
list { unfold from: :address }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
it "works" do
|
38
|
+
expect(subject).to eql output
|
39
|
+
end
|
40
|
+
|
41
|
+
end # describe fold
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
describe "ungroup" do
|
4
|
+
|
5
|
+
subject { mapper.new.call input }
|
6
|
+
|
7
|
+
let(:input) do
|
8
|
+
[
|
9
|
+
{
|
10
|
+
role: "admin",
|
11
|
+
users: [
|
12
|
+
{
|
13
|
+
name: "Joe",
|
14
|
+
contacts: [
|
15
|
+
{ email: "joe@doe.com", type: "job" },
|
16
|
+
{ email: "joe@doe.org", type: "job" }
|
17
|
+
]
|
18
|
+
},
|
19
|
+
{
|
20
|
+
name: "Ian",
|
21
|
+
contacts: [
|
22
|
+
{ email: "ian@doe.com", type: "job" },
|
23
|
+
{ email: "ian@doe.org", type: "job" }
|
24
|
+
]
|
25
|
+
}
|
26
|
+
]
|
27
|
+
}
|
28
|
+
]
|
29
|
+
end
|
30
|
+
|
31
|
+
let(:output) do
|
32
|
+
[
|
33
|
+
{
|
34
|
+
name: "Joe",
|
35
|
+
role: "admin",
|
36
|
+
type: "job",
|
37
|
+
contacts: [{ email: "joe@doe.com" }, { email: "joe@doe.org" }]
|
38
|
+
},
|
39
|
+
{
|
40
|
+
name: "Ian",
|
41
|
+
role: "admin",
|
42
|
+
type: "job",
|
43
|
+
contacts: [{ email: "ian@doe.com" }, { email: "ian@doe.org" }]
|
44
|
+
}
|
45
|
+
]
|
46
|
+
end
|
47
|
+
|
48
|
+
let(:mapper) do
|
49
|
+
Class.new(Faceter::Mapper) do
|
50
|
+
list do
|
51
|
+
field :users do
|
52
|
+
ungroup :type, from: :contacts
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
ungroup :name, :contacts, :type, from: :users
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
it "works" do
|
61
|
+
expect(subject).to eql output
|
62
|
+
end
|
63
|
+
|
64
|
+
end # describe ungroup
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
describe "unwrap" do
|
4
|
+
|
5
|
+
subject { mapper.new.call input }
|
6
|
+
|
7
|
+
let(:input) do
|
8
|
+
[
|
9
|
+
{
|
10
|
+
user: { id: 1, name: "joe" },
|
11
|
+
contacts: [{ email: { address: "joe@doe.com", type: "job" } }]
|
12
|
+
},
|
13
|
+
{
|
14
|
+
user: { id: 2, name: "jane" },
|
15
|
+
contacts: [{ email: { address: "jane@doe.com", type: "job" } }]
|
16
|
+
}
|
17
|
+
]
|
18
|
+
end
|
19
|
+
|
20
|
+
let(:output) do
|
21
|
+
[
|
22
|
+
{
|
23
|
+
id: 1, name: "joe",
|
24
|
+
contacts: [{ email: { address: "joe@doe.com" }, type: "job" }]
|
25
|
+
},
|
26
|
+
{
|
27
|
+
id: 2, name: "jane",
|
28
|
+
contacts: [{ email: { address: "jane@doe.com" }, type: "job" }]
|
29
|
+
}
|
30
|
+
]
|
31
|
+
end
|
32
|
+
|
33
|
+
let(:mapper) do
|
34
|
+
Class.new(Faceter::Mapper) do
|
35
|
+
list do
|
36
|
+
unwrap from: :user, only: :id
|
37
|
+
|
38
|
+
unwrap :name, from: :user # alternative syntax
|
39
|
+
|
40
|
+
field :contacts do
|
41
|
+
list { unwrap from: :email, except: :address }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it "works" do
|
48
|
+
expect(subject).to eql output
|
49
|
+
end
|
50
|
+
|
51
|
+
end # describe unwrap
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
describe "wrap" do
|
4
|
+
|
5
|
+
subject { mapper.new.call input }
|
6
|
+
|
7
|
+
let(:input) do
|
8
|
+
[
|
9
|
+
{
|
10
|
+
id: 1, name: "joe",
|
11
|
+
contacts: [{ email: { address: "joe@doe.com" }, type: "job" }]
|
12
|
+
},
|
13
|
+
{
|
14
|
+
id: 2, name: "jane",
|
15
|
+
contacts: [{ email: { address: "jane@doe.com" }, type: "job" }]
|
16
|
+
}
|
17
|
+
]
|
18
|
+
end
|
19
|
+
|
20
|
+
let(:output) do
|
21
|
+
[
|
22
|
+
{
|
23
|
+
user: { id: 1, name: "joe" },
|
24
|
+
contacts: [{ email: { address: "joe@doe.com", type: "job" } }]
|
25
|
+
},
|
26
|
+
{
|
27
|
+
user: { id: 2, name: "jane" },
|
28
|
+
contacts: [{ email: { address: "jane@doe.com", type: "job" } }]
|
29
|
+
}
|
30
|
+
]
|
31
|
+
end
|
32
|
+
|
33
|
+
let(:mapper) do
|
34
|
+
Class.new(Faceter::Mapper) do
|
35
|
+
list do
|
36
|
+
wrap to: :user, only: :id
|
37
|
+
|
38
|
+
wrap :name, to: :user # alternative syntax
|
39
|
+
|
40
|
+
field :contacts do
|
41
|
+
list { wrap to: :email, except: :email }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it "works" do
|
48
|
+
expect(subject).to eql output
|
49
|
+
end
|
50
|
+
|
51
|
+
end # describe wrap
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "rom"
|
2
|
+
require "rom/memory"
|
3
|
+
|
4
|
+
describe "ROM integration" do
|
5
|
+
|
6
|
+
let(:setup) { ROM.setup :memory }
|
7
|
+
let(:rom) { ROM.finalize.env }
|
8
|
+
|
9
|
+
before do
|
10
|
+
class UserMapper < Faceter::Mapper
|
11
|
+
group :email, to: :emails
|
12
|
+
|
13
|
+
list do
|
14
|
+
field :emails do
|
15
|
+
list { unfold from: :email }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
setup.relation(:users)
|
21
|
+
|
22
|
+
setup.mappers do
|
23
|
+
register(:users, mapped: UserMapper.new)
|
24
|
+
end
|
25
|
+
|
26
|
+
users = setup.default.dataset(:users)
|
27
|
+
users.insert(id: 1, name: "Joe", email: "joe@doe.com")
|
28
|
+
users.insert(id: 1, name: "Joe", email: "joe@doe.org")
|
29
|
+
end
|
30
|
+
|
31
|
+
it "works" do
|
32
|
+
expect(rom.relation(:users).as(:mapped).to_a).to eql [
|
33
|
+
{ id: 1, name: "Joe", emails: ["joe@doe.com", "joe@doe.org"] }
|
34
|
+
]
|
35
|
+
end
|
36
|
+
|
37
|
+
after { Object.send :remove_const, :UserMapper }
|
38
|
+
|
39
|
+
end # describe "ROM integration"
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
begin
|
4
|
+
require "hexx-suit"
|
5
|
+
Hexx::Suit.load_metrics_for(self)
|
6
|
+
rescue LoadError
|
7
|
+
require "hexx-rspec"
|
8
|
+
Hexx::RSpec.load_metrics_for(self)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Loads the code under test
|
12
|
+
require "faceter"
|
13
|
+
|
14
|
+
# Loads shared examples
|
15
|
+
require "abstract_mapper/rspec"
|
16
|
+
require "transproc/rspec"
|
17
|
+
|
18
|
+
# Creates the temporary Test module for test-specific constants
|
19
|
+
RSpec.configure do |config|
|
20
|
+
config.around do |example|
|
21
|
+
class Faceter::Test; end
|
22
|
+
example.run
|
23
|
+
Faceter.send :remove_const, :Test
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
describe Faceter::Coercers, ".create" do
|
4
|
+
|
5
|
+
subject { described_class[:create] }
|
6
|
+
|
7
|
+
it "coerces arguments" do
|
8
|
+
expect(subject[:foo, from: :bar]).to eq(name: :foo, keys: :bar)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "coerces arguments" do
|
12
|
+
expect(subject[:foo, from: [:bar, :baz]])
|
13
|
+
.to eq(name: :foo, keys: [:bar, :baz])
|
14
|
+
end
|
15
|
+
|
16
|
+
end # describe Faceter::Coercers.create
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
describe Faceter::Coercers, ".exclude" do
|
4
|
+
|
5
|
+
subject { described_class[:exclude] }
|
6
|
+
|
7
|
+
it "coerces arguments" do
|
8
|
+
expect(subject[:foo]).to eq(
|
9
|
+
selector: Selector.new(only: [:foo])
|
10
|
+
)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "coerces arguments" do
|
14
|
+
expect(subject[:foo, :bar]).to eq(
|
15
|
+
selector: Selector.new(only: [:foo, :bar])
|
16
|
+
)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "coerces arguments" do
|
20
|
+
expect(subject[only: /foo/]).to eq(
|
21
|
+
selector: Selector.new(only: /foo/)
|
22
|
+
)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "coerces arguments" do
|
26
|
+
expect(subject[except: /foo/]).to eq(
|
27
|
+
selector: Selector.new(except: /foo/)
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
end # describe Faceter::Coercers.exclude
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
describe Faceter::Coercers, ".prefix" do
|
4
|
+
|
5
|
+
subject { described_class[:prefix] }
|
6
|
+
|
7
|
+
it "coerces arguments" do
|
8
|
+
expect(subject[:foo]).to eql(
|
9
|
+
prefix: :foo,
|
10
|
+
separator: "_",
|
11
|
+
nested: false,
|
12
|
+
selector: nil
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "coerces arguments" do
|
17
|
+
attributes = subject[:foo, separator: ".", nested: true, only: /foo/]
|
18
|
+
|
19
|
+
expect(attributes).to eq(
|
20
|
+
prefix: :foo,
|
21
|
+
separator: ".",
|
22
|
+
nested: true,
|
23
|
+
selector: Selector.new(only: /foo/)
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "coerces arguments" do
|
28
|
+
attributes = subject[:foo, separator: ".", nested: true, except: /foo/]
|
29
|
+
|
30
|
+
expect(attributes).to eq(
|
31
|
+
prefix: :foo,
|
32
|
+
separator: ".",
|
33
|
+
nested: true,
|
34
|
+
selector: Selector.new(except: /foo/)
|
35
|
+
)
|
36
|
+
end
|
37
|
+
|
38
|
+
end # describe Faceter::Coercers.prefix
|