abstract_mapper 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +0 -4
  3. data/CHANGELOG.md +26 -0
  4. data/README.md +5 -5
  5. data/Rakefile +1 -1
  6. data/abstract_mapper.gemspec +1 -0
  7. data/lib/abstract_mapper.rb +4 -15
  8. data/lib/abstract_mapper/ast.rb +16 -0
  9. data/lib/abstract_mapper/ast/branch.rb +121 -0
  10. data/lib/abstract_mapper/ast/node.rb +91 -0
  11. data/lib/abstract_mapper/builder.rb +2 -2
  12. data/lib/abstract_mapper/commands.rb +4 -2
  13. data/lib/abstract_mapper/commands/base.rb +69 -0
  14. data/lib/abstract_mapper/dsl.rb +2 -2
  15. data/lib/abstract_mapper/errors.rb +17 -0
  16. data/lib/abstract_mapper/errors/wrong_node.rb +1 -1
  17. data/lib/abstract_mapper/errors/wrong_rule.rb +1 -1
  18. data/lib/abstract_mapper/optimizer.rb +1 -1
  19. data/lib/abstract_mapper/rules.rb +10 -5
  20. data/lib/abstract_mapper/rules/base.rb +74 -0
  21. data/lib/abstract_mapper/rules/pair.rb +68 -0
  22. data/lib/abstract_mapper/rules/sole.rb +60 -0
  23. data/lib/abstract_mapper/settings.rb +22 -30
  24. data/lib/abstract_mapper/version.rb +1 -1
  25. data/lib/rspec/nodes.rb +2 -2
  26. data/spec/integration/faceter.rb +4 -4
  27. data/spec/integration/rspec_examples_spec.rb +0 -6
  28. data/spec/unit/abstract_mapper/{branch_spec.rb → ast/branch_spec.rb} +28 -61
  29. data/spec/unit/abstract_mapper/{node_spec.rb → ast/node_spec.rb} +16 -53
  30. data/spec/unit/abstract_mapper/builder_spec.rb +9 -24
  31. data/spec/unit/abstract_mapper/{command_spec.rb → commands/base_spec.rb} +10 -25
  32. data/spec/unit/abstract_mapper/commands_spec.rb +9 -14
  33. data/spec/unit/abstract_mapper/dsl_spec.rb +23 -15
  34. data/spec/unit/abstract_mapper/errors/unknown_command_spec.rb +1 -4
  35. data/spec/unit/abstract_mapper/errors/wrong_node_spec.rb +5 -4
  36. data/spec/unit/abstract_mapper/errors/wrong_rule_spec.rb +5 -5
  37. data/spec/unit/abstract_mapper/functions/compact_spec.rb +0 -2
  38. data/spec/unit/abstract_mapper/functions/filter_spec.rb +0 -2
  39. data/spec/unit/abstract_mapper/functions/identity_spec.rb +0 -2
  40. data/spec/unit/abstract_mapper/functions/restrict_spec.rb +0 -3
  41. data/spec/unit/abstract_mapper/functions/subclass_spec.rb +0 -2
  42. data/spec/unit/abstract_mapper/optimizer_spec.rb +13 -17
  43. data/spec/unit/abstract_mapper/{rule_spec.rb → rules/base_spec.rb} +17 -34
  44. data/spec/unit/abstract_mapper/{pair_rule_spec.rb → rules/pair_spec.rb} +8 -8
  45. data/spec/unit/abstract_mapper/{sole_rule_spec.rb → rules/sole_spec.rb} +5 -5
  46. data/spec/unit/abstract_mapper/rules_spec.rb +24 -37
  47. data/spec/unit/abstract_mapper/settings_spec.rb +38 -32
  48. data/spec/unit/abstract_mapper_spec.rb +9 -16
  49. metadata +37 -22
  50. data/lib/abstract_mapper/attributes.rb +0 -65
  51. data/lib/abstract_mapper/branch.rb +0 -120
  52. data/lib/abstract_mapper/command.rb +0 -68
  53. data/lib/abstract_mapper/node.rb +0 -87
  54. data/lib/abstract_mapper/pair_rule.rb +0 -64
  55. data/lib/abstract_mapper/rule.rb +0 -73
  56. data/lib/abstract_mapper/sole_rule.rb +0 -56
@@ -1,64 +0,0 @@
1
- # encoding: utf-8
2
-
3
- class AbstractMapper
4
-
5
- # The base class for rules to be applied to pairs of nodes.
6
- #
7
- # The subclass should implement two instance methods:
8
- #
9
- # * `#optimize?` to check if the optimization should be applied to the nodes
10
- # * `#optimize` that should return the merged node
11
- #
12
- # @example
13
- # class MergeConsecutiveLists < AbstractMapper::PairRule
14
- # def optimize?
15
- # left.is_a?(List) & right.is_a?(List)
16
- # end
17
- #
18
- # def optimize
19
- # List.new { left.entries + right.entries }
20
- # end
21
- # end
22
- #
23
- # @abstract
24
- #
25
- # @api public
26
- #
27
- class PairRule < Rule
28
-
29
- # @private
30
- def self.composer
31
- :compact
32
- end
33
- private_class_method :composer
34
-
35
- # @!attribute [r] node
36
- #
37
- # @return [AbstractMapper::Node] The left node in the pair to be optimized
38
- #
39
- attr_reader :left
40
-
41
- # @!attribute [r] node
42
- #
43
- # @return [AbstractMapper::Node] The right node in the pair to be optimized
44
- #
45
- attr_reader :right
46
-
47
- # @!scope class
48
- # @!method new(node)
49
- # Creates a rule applied to the sole node
50
- #
51
- # @param [AbstractMapper::Node] node
52
- #
53
- # @return [AbstractMapper::Rule]
54
-
55
- # @private
56
- def initialize(left, right)
57
- @left = left
58
- @right = right
59
- super
60
- end
61
-
62
- end # class PairRule
63
-
64
- end # class AbstractMapper
@@ -1,73 +0,0 @@
1
- # encoding: utf-8
2
-
3
- class AbstractMapper
4
-
5
- # Base class for nodes optimization rules
6
- #
7
- # @abstract
8
- #
9
- # @api private
10
- #
11
- class Rule
12
-
13
- # @private
14
- def self.composer
15
- :identity
16
- end
17
- private_class_method :composer
18
-
19
- # The transformation function that applies the rule to the array of nodes
20
- #
21
- # @return [Transproc::Function]
22
- #
23
- def self.transproc
24
- Functions[composer, proc { |*nodes| new(*nodes).call }]
25
- end
26
-
27
- # @!attribute [r] nodes
28
- #
29
- # @return [Array<AbstractMapper::Node>]
30
- # Either one or two nodes to be optimized
31
- #
32
- attr_reader :nodes
33
-
34
- # @!scope class
35
- # @!method new(*nodes)
36
- # Creates the rule for a sole node, or a pair of consecutive nodes
37
- #
38
- # @param [Array<AbstractMapper::Node>] nodes
39
- #
40
- # @return [AbstractMapper::Rule]
41
-
42
- # @private
43
- def initialize(*nodes)
44
- @nodes = nodes
45
- IceNine.deep_freeze(self)
46
- end
47
-
48
- # Checks if optimization is needed for the node(s)
49
- #
50
- # @return [Boolean]
51
- #
52
- def optimize?
53
- end
54
-
55
- # Returns the optimized node(s)
56
- #
57
- # @return [Object]
58
- #
59
- def optimize
60
- nodes
61
- end
62
-
63
- # Returns the result of the rule applied to the initialized [#nodes]
64
- #
65
- # @return [Array<AbstractMapper::Node>]
66
- #
67
- def call
68
- optimize? ? [optimize].flatten.compact : nodes
69
- end
70
-
71
- end # class Rule
72
-
73
- end # class AbstractMapper
@@ -1,56 +0,0 @@
1
- # encoding: utf-8
2
-
3
- class AbstractMapper
4
-
5
- # The abstract class that describes the rule to be applied to sole nodes.
6
- #
7
- # The subclass should implement two instance methods:
8
- #
9
- # * `#optimize?` to check if the optimization should be applied to the node
10
- # * `#optimize` that should return the optimized node
11
- #
12
- # @example
13
- # class RemoveEmptyList < AbstractMapper::SoleRule
14
- # def optimize?
15
- # node.is_a?(List) && node.empty?
16
- # end
17
- #
18
- # def optimize
19
- # end
20
- # end
21
- #
22
- # @abstract
23
- #
24
- # @api public
25
- #
26
- class SoleRule < Rule
27
-
28
- # @private
29
- def self.composer
30
- :filter
31
- end
32
- private_class_method :composer
33
-
34
- # @!attribute [r] node
35
- #
36
- # @return [AbstractMapper::Node] The node to be optimized
37
- #
38
- attr_reader :node
39
-
40
- # @!scope class
41
- # @!method new(node)
42
- # Creates a rule applied to the sole node
43
- #
44
- # @param [AbstractMapper::Node] node
45
- #
46
- # @return [AbstractMapper::Rule]
47
-
48
- # @private
49
- def initialize(node)
50
- @node = node
51
- super
52
- end
53
-
54
- end # class SoleRule
55
-
56
- end # class AbstractMapper