faceter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +2 -0
  3. data/.gitignore +9 -0
  4. data/.metrics +9 -0
  5. data/.rspec +2 -0
  6. data/.rubocop.yml +2 -0
  7. data/.travis.yml +18 -0
  8. data/.yardopts +3 -0
  9. data/CHANGELOG.md +3 -0
  10. data/Gemfile +9 -0
  11. data/Guardfile +10 -0
  12. data/LICENSE +21 -0
  13. data/README.md +295 -0
  14. data/Rakefile +37 -0
  15. data/benchmark/data.json +1 -0
  16. data/benchmark/faceter.rb +73 -0
  17. data/benchmark/rom.rb +85 -0
  18. data/benchmark/run.rb +54 -0
  19. data/config/metrics/STYLEGUIDE +230 -0
  20. data/config/metrics/cane.yml +5 -0
  21. data/config/metrics/churn.yml +6 -0
  22. data/config/metrics/flay.yml +2 -0
  23. data/config/metrics/metric_fu.yml +14 -0
  24. data/config/metrics/reek.yml +1 -0
  25. data/config/metrics/roodi.yml +24 -0
  26. data/config/metrics/rubocop.yml +75 -0
  27. data/config/metrics/saikuro.yml +3 -0
  28. data/config/metrics/simplecov.yml +6 -0
  29. data/config/metrics/yardstick.yml +37 -0
  30. data/faceter.gemspec +29 -0
  31. data/lib/faceter.rb +54 -0
  32. data/lib/faceter/coercers.rb +68 -0
  33. data/lib/faceter/functions.rb +30 -0
  34. data/lib/faceter/functions/add_prefix.rb +25 -0
  35. data/lib/faceter/functions/claster.rb +27 -0
  36. data/lib/faceter/functions/clean.rb +27 -0
  37. data/lib/faceter/functions/drop_prefix.rb +29 -0
  38. data/lib/faceter/functions/exclude.rb +27 -0
  39. data/lib/faceter/functions/group.rb +41 -0
  40. data/lib/faceter/functions/keep_symbol.rb +27 -0
  41. data/lib/faceter/functions/split.rb +28 -0
  42. data/lib/faceter/functions/ungroup.rb +31 -0
  43. data/lib/faceter/functions/unwrap.rb +32 -0
  44. data/lib/faceter/functions/wrap.rb +32 -0
  45. data/lib/faceter/mapper.rb +38 -0
  46. data/lib/faceter/nodes/add_prefix.rb +21 -0
  47. data/lib/faceter/nodes/change_prefix.rb +51 -0
  48. data/lib/faceter/nodes/create.rb +43 -0
  49. data/lib/faceter/nodes/exclude.rb +25 -0
  50. data/lib/faceter/nodes/field.rb +25 -0
  51. data/lib/faceter/nodes/fold.rb +25 -0
  52. data/lib/faceter/nodes/group.rb +26 -0
  53. data/lib/faceter/nodes/list.rb +25 -0
  54. data/lib/faceter/nodes/remove_prefix.rb +21 -0
  55. data/lib/faceter/nodes/rename.rb +25 -0
  56. data/lib/faceter/nodes/stringify_keys.rb +26 -0
  57. data/lib/faceter/nodes/symbolize_keys.rb +26 -0
  58. data/lib/faceter/nodes/unfold.rb +25 -0
  59. data/lib/faceter/nodes/ungroup.rb +26 -0
  60. data/lib/faceter/nodes/unwrap.rb +26 -0
  61. data/lib/faceter/nodes/wrap.rb +26 -0
  62. data/lib/faceter/rules/append_nested.rb +28 -0
  63. data/lib/faceter/rules/merge_branches.rb +41 -0
  64. data/lib/faceter/rules/merge_excludes.rb +29 -0
  65. data/lib/faceter/rules/merge_renames.rb +27 -0
  66. data/lib/faceter/rules/order_fields.rb +27 -0
  67. data/lib/faceter/rules/prepend_nested.rb +51 -0
  68. data/lib/faceter/version.rb +11 -0
  69. data/spec/integration/commands/add_prefix_spec.rb +37 -0
  70. data/spec/integration/commands/create_spec.rb +33 -0
  71. data/spec/integration/commands/exclude_spec.rb +55 -0
  72. data/spec/integration/commands/fold_spec.rb +41 -0
  73. data/spec/integration/commands/group_spec.rb +64 -0
  74. data/spec/integration/commands/remove_prefix_spec.rb +63 -0
  75. data/spec/integration/commands/rename_spec.rb +45 -0
  76. data/spec/integration/commands/stringify_keys_spec.rb +65 -0
  77. data/spec/integration/commands/symbolize_keys_spec.rb +49 -0
  78. data/spec/integration/commands/unfold_spec.rb +41 -0
  79. data/spec/integration/commands/ungroup_spec.rb +64 -0
  80. data/spec/integration/commands/unwrap_spec.rb +51 -0
  81. data/spec/integration/commands/wrap_spec.rb +51 -0
  82. data/spec/integration/rom_spec.rb +39 -0
  83. data/spec/spec_helper.rb +25 -0
  84. data/spec/unit/coercers/create_spec.rb +16 -0
  85. data/spec/unit/coercers/exclude_spec.rb +31 -0
  86. data/spec/unit/coercers/field_spec.rb +11 -0
  87. data/spec/unit/coercers/fold_spec.rb +11 -0
  88. data/spec/unit/coercers/prefix_spec.rb +38 -0
  89. data/spec/unit/coercers/rename_spec.rb +11 -0
  90. data/spec/unit/coercers/unfold_spec.rb +11 -0
  91. data/spec/unit/coercers/unwrap_spec.rb +35 -0
  92. data/spec/unit/coercers/wrap_spec.rb +35 -0
  93. data/spec/unit/functions/add_prefix_spec.rb +12 -0
  94. data/spec/unit/functions/claster_spec.rb +12 -0
  95. data/spec/unit/functions/clean_spec.rb +18 -0
  96. data/spec/unit/functions/drop_prefix_spec.rb +33 -0
  97. data/spec/unit/functions/exclude_spec.rb +64 -0
  98. data/spec/unit/functions/group_spec.rb +176 -0
  99. data/spec/unit/functions/keep_symbol_spec.rb +21 -0
  100. data/spec/unit/functions/split_spec.rb +64 -0
  101. data/spec/unit/functions/ungroup_spec.rb +87 -0
  102. data/spec/unit/functions/unwrap_spec.rb +54 -0
  103. data/spec/unit/functions/wrap_spec.rb +67 -0
  104. data/spec/unit/nodes/add_prefix_spec.rb +62 -0
  105. data/spec/unit/nodes/create_spec.rb +53 -0
  106. data/spec/unit/nodes/exclude_spec.rb +18 -0
  107. data/spec/unit/nodes/field_spec.rb +30 -0
  108. data/spec/unit/nodes/fold_spec.rb +19 -0
  109. data/spec/unit/nodes/group_spec.rb +163 -0
  110. data/spec/unit/nodes/list_spec.rb +27 -0
  111. data/spec/unit/nodes/remove_prefix_spec.rb +62 -0
  112. data/spec/unit/nodes/rename_spec.rb +16 -0
  113. data/spec/unit/nodes/stringify_keys_spec.rb +21 -0
  114. data/spec/unit/nodes/symbolize_keys_spec.rb +21 -0
  115. data/spec/unit/nodes/unfold_spec.rb +19 -0
  116. data/spec/unit/nodes/ungroup_spec.rb +92 -0
  117. data/spec/unit/nodes/unwrap_spec.rb +47 -0
  118. data/spec/unit/nodes/wrap_spec.rb +33 -0
  119. data/spec/unit/rules/append_nested_spec.rb +41 -0
  120. data/spec/unit/rules/merge_branches_spec.rb +58 -0
  121. data/spec/unit/rules/merge_excludes_spec.rb +31 -0
  122. data/spec/unit/rules/merge_renames_spec.rb +29 -0
  123. data/spec/unit/rules/order_fields_spec.rb +31 -0
  124. data/spec/unit/rules/prepend_nested_spec.rb +41 -0
  125. metadata +315 -0
@@ -0,0 +1,25 @@
1
+ module Faceter
2
+
3
+ module Nodes
4
+
5
+ # The node describes exclusion of the field from a tuple
6
+ #
7
+ # @api private
8
+ #
9
+ class Fold < AbstractMapper::Node
10
+
11
+ attribute :key
12
+
13
+ # Transformer function, defined by the node
14
+ #
15
+ # @return [Transproc::Function]
16
+ #
17
+ def transproc
18
+ Functions[-> value { { key => value } }]
19
+ end
20
+
21
+ end # class Fold
22
+
23
+ end # module Nodes
24
+
25
+ end # module Faceter
@@ -0,0 +1,26 @@
1
+ module Faceter
2
+
3
+ module Nodes
4
+
5
+ # The node describes grouping fields from tuples into nested arrays
6
+ #
7
+ # @api private
8
+ #
9
+ class Group < AbstractMapper::Node
10
+
11
+ attribute :key
12
+ attribute :selector
13
+
14
+ # Transformer function, defined by the node
15
+ #
16
+ # @return [Transproc::Function]
17
+ #
18
+ def transproc
19
+ Functions[:group, key, selector]
20
+ end
21
+
22
+ end # class Group
23
+
24
+ end # module Nodes
25
+
26
+ end # module Faceter
@@ -0,0 +1,25 @@
1
+ module Faceter
2
+
3
+ # The collection of faceter-specific nodes of the AST
4
+ #
5
+ module Nodes
6
+
7
+ # The composed node that describes an array of the input data
8
+ #
9
+ # @api private
10
+ #
11
+ class List < AbstractMapper::Branch
12
+
13
+ # Builds a transproc function for the node from its child nodes
14
+ #
15
+ # @return [Transproc::Function]
16
+ #
17
+ def transproc
18
+ Functions[:map_array, super]
19
+ end
20
+
21
+ end # class List
22
+
23
+ end # module Nodes
24
+
25
+ end # module Faceter
@@ -0,0 +1,21 @@
1
+ module Faceter
2
+
3
+ module Nodes
4
+
5
+ # The node describes removing prefix from tuples' keys
6
+ #
7
+ # @api private
8
+ #
9
+ class RemovePrefix < ChangePrefix
10
+
11
+ private
12
+
13
+ def __operation__
14
+ :drop_prefix
15
+ end
16
+
17
+ end # class RemovePrefix
18
+
19
+ end # module Nodes
20
+
21
+ end # module Faceter
@@ -0,0 +1,25 @@
1
+ module Faceter
2
+
3
+ module Nodes
4
+
5
+ # The node describes renaming a key of tuples
6
+ #
7
+ # @api private
8
+ #
9
+ class Rename < AbstractMapper::Node
10
+
11
+ attribute :keys, default: {}
12
+
13
+ # Transformer function, defined by the node
14
+ #
15
+ # @return [Transproc::Function]
16
+ #
17
+ def transproc
18
+ Functions[:rename_keys, keys]
19
+ end
20
+
21
+ end # class Rename
22
+
23
+ end # module Nodes
24
+
25
+ end # module Faceter
@@ -0,0 +1,26 @@
1
+ module Faceter
2
+
3
+ module Nodes
4
+
5
+ # The node describes stringifying keys of tuple (and its nested tuples)
6
+ #
7
+ # @api private
8
+ #
9
+ class StringifyKeys < AbstractMapper::Node
10
+
11
+ attribute :nested, default: true
12
+
13
+ # Transformer function, defined by the node
14
+ #
15
+ # @return [Transproc::Function]
16
+ #
17
+ def transproc
18
+ fn = Functions[:stringify_keys]
19
+ nested ? Functions[:recursion, Functions[:is, Hash, fn]] : fn
20
+ end
21
+
22
+ end # class StringifyKeys
23
+
24
+ end # module Nodes
25
+
26
+ end # module Faceter
@@ -0,0 +1,26 @@
1
+ module Faceter
2
+
3
+ module Nodes
4
+
5
+ # The node describes stringifying keys of tuple (and its nested tuples)
6
+ #
7
+ # @api private
8
+ #
9
+ class SymbolizeKeys < AbstractMapper::Node
10
+
11
+ attribute :nested, default: true
12
+
13
+ # Transformer function, defined by the node
14
+ #
15
+ # @return [Transproc::Function]
16
+ #
17
+ def transproc
18
+ fn = Functions[:symbolize_keys]
19
+ nested ? Functions[:recursion, Functions[:is, Hash, fn]] : fn
20
+ end
21
+
22
+ end # class SymbolizeKeys
23
+
24
+ end # module Nodes
25
+
26
+ end # module Faceter
@@ -0,0 +1,25 @@
1
+ module Faceter
2
+
3
+ module Nodes
4
+
5
+ # The node describes exclusion of the field from a tuple
6
+ #
7
+ # @api private
8
+ #
9
+ class Unfold < AbstractMapper::Node
10
+
11
+ attribute :key
12
+
13
+ # Transformer function, defined by the node
14
+ #
15
+ # @return [Transproc::Function]
16
+ #
17
+ def transproc
18
+ Functions[-> hash { hash[key] }]
19
+ end
20
+
21
+ end # class Unfold
22
+
23
+ end # module Nodes
24
+
25
+ end # module Faceter
@@ -0,0 +1,26 @@
1
+ module Faceter
2
+
3
+ module Nodes
4
+
5
+ # The node describes grouping fields from nested arrays to tuples
6
+ #
7
+ # @api private
8
+ #
9
+ class Ungroup < AbstractMapper::Node
10
+
11
+ attribute :key
12
+ attribute :selector
13
+
14
+ # Transformer function, defined by the node
15
+ #
16
+ # @return [Transproc::Function]
17
+ #
18
+ def transproc
19
+ Functions[:ungroup, key, selector]
20
+ end
21
+
22
+ end # class Ungroup
23
+
24
+ end # module Nodes
25
+
26
+ end # module Faceter
@@ -0,0 +1,26 @@
1
+ module Faceter
2
+
3
+ module Nodes
4
+
5
+ # The node describes unwrapping values from the nested tuples
6
+ #
7
+ # @api private
8
+ #
9
+ class Unwrap < AbstractMapper::Node
10
+
11
+ attribute :key
12
+ attribute :selector
13
+
14
+ # Transformer function, defined by the node
15
+ #
16
+ # @return [Transproc::Function]
17
+ #
18
+ def transproc
19
+ Functions[:unwrap, key, selector]
20
+ end
21
+
22
+ end # class Unwrap
23
+
24
+ end # module Nodes
25
+
26
+ end # module Faceter
@@ -0,0 +1,26 @@
1
+ module Faceter
2
+
3
+ module Nodes
4
+
5
+ # The node describes wrapping hash values into nested tuple
6
+ #
7
+ # @api private
8
+ #
9
+ class Wrap < AbstractMapper::Node
10
+
11
+ attribute :key
12
+ attribute :selector
13
+
14
+ # Transformer function, defined by the node
15
+ #
16
+ # @return [Transproc::Function]
17
+ #
18
+ def transproc
19
+ Functions[:wrap, key, selector]
20
+ end
21
+
22
+ end # class Wrap
23
+
24
+ end # module Nodes
25
+
26
+ end # module Faceter
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+
3
+ module Faceter
4
+
5
+ module Rules
6
+
7
+ # Does the same as [Faceter::Rules::PrependNested] in case
8
+ # 'nested' transformation follows the `List`, not prepends it.
9
+ #
10
+ # @api private
11
+ #
12
+ class AppendNested < AbstractMapper::PairRule
13
+
14
+ # @private
15
+ def optimize?
16
+ left.instance_of?(Nodes::List) && right.respond_to?(:nested)
17
+ end
18
+
19
+ # @private
20
+ def optimize
21
+ Nodes::List.new { left.entries + (right.nested ? [right] : []) }
22
+ end
23
+
24
+ end # class AppendNested
25
+
26
+ end # module Rules
27
+
28
+ end # module Faceter
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+
3
+ module Faceter
4
+
5
+ # Collection of optimization rules for AST
6
+ #
7
+ module Rules
8
+
9
+ # Merges two consecutive branches of the same type
10
+ #
11
+ # @api private
12
+ #
13
+ class MergeBranches < AbstractMapper::PairRule
14
+
15
+ # @private
16
+ def optimize?
17
+ left.is_a?(AbstractMapper::Branch) &&
18
+ right.class.equal?(__left_node__) &&
19
+ right.attributes.eql?(__left_attributes__)
20
+ end
21
+
22
+ # @private
23
+ def optimize
24
+ __left_node__.new(__left_attributes__) { nodes.flat_map(&:entries) }
25
+ end
26
+
27
+ private
28
+
29
+ def __left_node__
30
+ left.class
31
+ end
32
+
33
+ def __left_attributes__
34
+ left.attributes
35
+ end
36
+
37
+ end # class MergeBranches
38
+
39
+ end # module Rules
40
+
41
+ end # module Faceter
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ module Faceter
4
+
5
+ # Collection of optimization rules for AST
6
+ #
7
+ module Rules
8
+
9
+ # Merges two consecutive nodes that exclude keys
10
+ #
11
+ # @api private
12
+ #
13
+ class MergeExcludes < AbstractMapper::PairRule
14
+
15
+ # @private
16
+ def optimize?
17
+ nodes.map { |node| node.instance_of? Nodes::Exclude }.reduce(:&)
18
+ end
19
+
20
+ # @private
21
+ def optimize
22
+ Nodes::Exclude.new(selector: nodes.map(&:selector).reduce(:|))
23
+ end
24
+
25
+ end # class MergeExcludes
26
+
27
+ end # module Rules
28
+
29
+ end # module Faceter
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ module Faceter
4
+
5
+ module Rules
6
+
7
+ # Merges two consecutive renames
8
+ #
9
+ # @api private
10
+ #
11
+ class MergeRenames < AbstractMapper::PairRule
12
+
13
+ # @private
14
+ def optimize?
15
+ nodes.map { |node| node.instance_of? Nodes::Rename }.reduce(:&)
16
+ end
17
+
18
+ # @private
19
+ def optimize
20
+ Nodes::Rename.new keys: nodes.map(&:keys).reduce(:merge)
21
+ end
22
+
23
+ end # class MergeRenames
24
+
25
+ end # module Rules
26
+
27
+ end # module Faceter
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ module Faceter
4
+
5
+ module Rules
6
+
7
+ # Orders consecutive fields by their names to allow merging them later
8
+ #
9
+ class OrderFields < AbstractMapper::Rule
10
+
11
+ # @private
12
+ def optimize?
13
+ true
14
+ end
15
+
16
+ # @private
17
+ def optimize
18
+ Functions[:claster, -> v { v.class }][nodes].flat_map do |list|
19
+ list.last.instance_of?(Nodes::Field) ? list.sort_by(&:key) : list
20
+ end
21
+ end
22
+
23
+ end # class OrderFields
24
+
25
+ end # module Rules
26
+
27
+ end # module Faceter