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
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: abstract_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kozin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-06 00:00:00.000000000 Z
11
+ date: 2015-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: attributes_dsl
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: ice_nine
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -92,24 +106,25 @@ files:
92
106
  - config/metrics/simplecov.yml
93
107
  - config/metrics/yardstick.yml
94
108
  - lib/abstract_mapper.rb
95
- - lib/abstract_mapper/attributes.rb
96
- - lib/abstract_mapper/branch.rb
109
+ - lib/abstract_mapper/ast.rb
110
+ - lib/abstract_mapper/ast/branch.rb
111
+ - lib/abstract_mapper/ast/node.rb
97
112
  - lib/abstract_mapper/builder.rb
98
- - lib/abstract_mapper/command.rb
99
113
  - lib/abstract_mapper/commands.rb
114
+ - lib/abstract_mapper/commands/base.rb
100
115
  - lib/abstract_mapper/dsl.rb
116
+ - lib/abstract_mapper/errors.rb
101
117
  - lib/abstract_mapper/errors/unknown_command.rb
102
118
  - lib/abstract_mapper/errors/wrong_node.rb
103
119
  - lib/abstract_mapper/errors/wrong_rule.rb
104
120
  - lib/abstract_mapper/functions.rb
105
- - lib/abstract_mapper/node.rb
106
121
  - lib/abstract_mapper/optimizer.rb
107
- - lib/abstract_mapper/pair_rule.rb
108
122
  - lib/abstract_mapper/rspec.rb
109
- - lib/abstract_mapper/rule.rb
110
123
  - lib/abstract_mapper/rules.rb
124
+ - lib/abstract_mapper/rules/base.rb
125
+ - lib/abstract_mapper/rules/pair.rb
126
+ - lib/abstract_mapper/rules/sole.rb
111
127
  - lib/abstract_mapper/settings.rb
112
- - lib/abstract_mapper/sole_rule.rb
113
128
  - lib/abstract_mapper/version.rb
114
129
  - lib/rspec/doubles.rb
115
130
  - lib/rspec/nodes.rb
@@ -118,9 +133,10 @@ files:
118
133
  - spec/integration/mapper_definition_spec.rb
119
134
  - spec/integration/rspec_examples_spec.rb
120
135
  - spec/spec_helper.rb
121
- - spec/unit/abstract_mapper/branch_spec.rb
136
+ - spec/unit/abstract_mapper/ast/branch_spec.rb
137
+ - spec/unit/abstract_mapper/ast/node_spec.rb
122
138
  - spec/unit/abstract_mapper/builder_spec.rb
123
- - spec/unit/abstract_mapper/command_spec.rb
139
+ - spec/unit/abstract_mapper/commands/base_spec.rb
124
140
  - spec/unit/abstract_mapper/commands_spec.rb
125
141
  - spec/unit/abstract_mapper/dsl_spec.rb
126
142
  - spec/unit/abstract_mapper/errors/unknown_command_spec.rb
@@ -131,13 +147,12 @@ files:
131
147
  - spec/unit/abstract_mapper/functions/identity_spec.rb
132
148
  - spec/unit/abstract_mapper/functions/restrict_spec.rb
133
149
  - spec/unit/abstract_mapper/functions/subclass_spec.rb
134
- - spec/unit/abstract_mapper/node_spec.rb
135
150
  - spec/unit/abstract_mapper/optimizer_spec.rb
136
- - spec/unit/abstract_mapper/pair_rule_spec.rb
137
- - spec/unit/abstract_mapper/rule_spec.rb
151
+ - spec/unit/abstract_mapper/rules/base_spec.rb
152
+ - spec/unit/abstract_mapper/rules/pair_spec.rb
153
+ - spec/unit/abstract_mapper/rules/sole_spec.rb
138
154
  - spec/unit/abstract_mapper/rules_spec.rb
139
155
  - spec/unit/abstract_mapper/settings_spec.rb
140
- - spec/unit/abstract_mapper/sole_rule_spec.rb
141
156
  - spec/unit/abstract_mapper_spec.rb
142
157
  homepage: https://github.com/nepalez/abstract_mapper
143
158
  licenses:
@@ -159,7 +174,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
174
  version: '0'
160
175
  requirements: []
161
176
  rubyforge_project:
162
- rubygems_version: 2.4.6
177
+ rubygems_version: 2.4.8
163
178
  signing_key:
164
179
  specification_version: 4
165
180
  summary: AST for ruby mappers
@@ -169,9 +184,6 @@ test_files:
169
184
  - spec/integration/faceter.rb
170
185
  - spec/integration/rspec_examples_spec.rb
171
186
  - spec/unit/abstract_mapper/dsl_spec.rb
172
- - spec/unit/abstract_mapper/rule_spec.rb
173
- - spec/unit/abstract_mapper/sole_rule_spec.rb
174
- - spec/unit/abstract_mapper/pair_rule_spec.rb
175
187
  - spec/unit/abstract_mapper/functions/compact_spec.rb
176
188
  - spec/unit/abstract_mapper/functions/filter_spec.rb
177
189
  - spec/unit/abstract_mapper/functions/restrict_spec.rb
@@ -182,11 +194,14 @@ test_files:
182
194
  - spec/unit/abstract_mapper/errors/wrong_node_spec.rb
183
195
  - spec/unit/abstract_mapper/errors/unknown_command_spec.rb
184
196
  - spec/unit/abstract_mapper/errors/wrong_rule_spec.rb
197
+ - spec/unit/abstract_mapper/rules/pair_spec.rb
198
+ - spec/unit/abstract_mapper/rules/sole_spec.rb
199
+ - spec/unit/abstract_mapper/rules/base_spec.rb
185
200
  - spec/unit/abstract_mapper/commands_spec.rb
186
- - spec/unit/abstract_mapper/node_spec.rb
201
+ - spec/unit/abstract_mapper/commands/base_spec.rb
187
202
  - spec/unit/abstract_mapper/settings_spec.rb
188
- - spec/unit/abstract_mapper/branch_spec.rb
203
+ - spec/unit/abstract_mapper/ast/node_spec.rb
204
+ - spec/unit/abstract_mapper/ast/branch_spec.rb
189
205
  - spec/unit/abstract_mapper/optimizer_spec.rb
190
- - spec/unit/abstract_mapper/command_spec.rb
191
206
  - spec/unit/abstract_mapper_spec.rb
192
207
  has_rdoc:
@@ -1,65 +0,0 @@
1
- # encoding: utf-8
2
-
3
- class AbstractMapper
4
-
5
- # Defines attributes along with the DSL for setting them
6
- #
7
- module Attributes
8
-
9
- # @private
10
- def self.included(klass)
11
- klass.__send__ :extend, DSL
12
- end
13
-
14
- # @!attribute [r] attributes
15
- #
16
- # @return [Hash] The initialized attributes
17
- #
18
- attr_reader :attributes
19
-
20
- # Initializes the instance with the hash of attributes
21
- #
22
- # @param [Hash] attributes
23
- #
24
- def initialize(attributes)
25
- @attributes = Functions[:restrict, self.class.attributes][attributes]
26
- end
27
-
28
- # Attributes DSL
29
- #
30
- module DSL
31
-
32
- # Makes a attributes inheritable
33
- #
34
- # @private
35
- #
36
- def inherited(klass)
37
- attributes.each { |key, value| klass.attribute key, default: value }
38
- end
39
-
40
- # Default attributes for the node
41
- #
42
- # @return [Hash]
43
- #
44
- def attributes
45
- @attributes ||= {}
46
- end
47
-
48
- # Declares the attribute
49
- #
50
- # @param [#to_sym] name
51
- # @param [Hash] options
52
- # @option options [Object] :default
53
- #
54
- # @return [undefined]
55
- #
56
- def attribute(name, options = {})
57
- attributes[name.to_sym] = options[:default]
58
- define_method(name) { attributes[name.to_sym] }
59
- end
60
-
61
- end # module DSL
62
-
63
- end # module Attributes
64
-
65
- end # class AbstractMapper
@@ -1,120 +0,0 @@
1
- # encoding: utf-8
2
-
3
- class AbstractMapper
4
-
5
- # A special type of the composed node, that describes transformation,
6
- # applied to some level of nested input.
7
- #
8
- # Unlike the simple node, describing a transformation of data, the
9
- # branch carries a collection of subnodes along with methods to [#update]
10
- # itself with the same attributes and different subnodes.
11
- #
12
- # Tne branch only stores subnodes and composes transformations.
13
- # Its has no access to DSL and knows neither how to build a tree
14
- # (see [AbstractMapper::Builder]), nor how to optimize it later
15
- # (see [AbstractMapper::Optimizer]).
16
- #
17
- # @api public
18
- #
19
- class Branch < Node
20
-
21
- include Enumerable
22
-
23
- # @!scope class
24
- # @!method new(*attributes, &block)
25
- # Creates a new branch
26
- #
27
- # @param [Object, Array<Object>] attributes
28
- # The list of type-specific attributes of the branch
29
- # @param [Proc] block
30
- # The block that returns an array of subnodes for the branch
31
- #
32
- # @return [Branch::Node]
33
-
34
- # @private
35
- def initialize(attributes = {})
36
- @subnodes = block_given? ? yield : []
37
- super(attributes, &nil)
38
- end
39
-
40
- # Returns a new branch of the same type, with the same attributes,
41
- # but with a different collection of subnodes, transmitted by the block.
42
- #
43
- # @example
44
- # branch = Branch.new(:foo)
45
- # # => <Branch(:foo) []>
46
- # branch.update { Node.new(:bar) }
47
- # # => <Branch(:foo) [<Node(:bar)>]>
48
- #
49
- # @param [Proc] block
50
- # The block that should return an array of subnodes for the branch
51
- #
52
- # @return [AbstractMapper::Branch]
53
- #
54
- # @yield block
55
- #
56
- def update
57
- self.class.new(attributes) { yield }
58
- end
59
-
60
- # @!method each
61
- # Returns the enumerator for subnodes
62
- #
63
- # @return [Enumerator]
64
- #
65
- def each(&block)
66
- @subnodes.each(&block)
67
- end
68
-
69
- # Returns a new branch with the other node added to its subnodes
70
- #
71
- # @param [AbstractMapper::Node] other
72
- #
73
- # @return [AbstractMapper::Branch]
74
- #
75
- def <<(other)
76
- update { entries << other }
77
- end
78
-
79
- # The composition of transformations from all subnodes of the branch
80
- #
81
- # To be reloaded by the subclasses to apply the composition to
82
- # a corresponding level of nested data.
83
- #
84
- # @return [Transproc::Function]
85
- #
86
- # @abstract
87
- #
88
- def transproc
89
- map(&:transproc).inject(:>>)
90
- end
91
-
92
- # Adds subnodes to the default description of the branch
93
- #
94
- # @return [String]
95
- #
96
- def to_s
97
- "#{super} [#{map(&:inspect).join(", ")}]"
98
- end
99
-
100
- # Checks equality of branches by type, attributes and subnodes
101
- #
102
- # @param [Other] other
103
- #
104
- # @return [Boolean]
105
- #
106
- def eql?(other)
107
- super && entries.eql?(other.entries)
108
- end
109
-
110
- private
111
-
112
- # Substitutes the name of the class by the special name "Root"
113
- # to describe the root node of AST.
114
- def __name__
115
- instance_of?(Branch) ? "Root" : super
116
- end
117
-
118
- end # class Branch
119
-
120
- end # class AbstractMapper
@@ -1,68 +0,0 @@
1
- # encoding: utf-8
2
-
3
- class AbstractMapper
4
-
5
- # Describes the command of the mapper
6
- #
7
- # Every command has a name, a node that is added to the AST and the function,
8
- # that converts command attributes into the node's.
9
- #
10
- # Method `#call` builds a correspodning node for the AST.
11
- #
12
- # @api private
13
- #
14
- class Command
15
-
16
- # @!attribute [r] name
17
- #
18
- # @return [Symbol] The name of the DSL command
19
- #
20
- attr_reader :name
21
-
22
- # @!attribute [r] klass
23
- #
24
- # @return [Class] The class of the node to be created
25
- #
26
- attr_reader :klass
27
-
28
- # @!attribute [r] converter
29
- #
30
- # @return [#call] The converter of the command's arguments into the node's
31
- #
32
- attr_reader :converter
33
-
34
- # @!scope class
35
- # @!method new(name, klass, converter)
36
- # Creates the named command for node klass and arguments converter
37
- #
38
- # @param [#to_sym] name The name of the DSL command
39
- # @param [Class] klass The klass of the node to be created
40
- # @param [#call] converter The function that coerces attributes
41
- #
42
- # @return [AbstractMapper::Command]
43
-
44
- # @private
45
- def initialize(name, klass, converter = nil)
46
- @name = name.to_sym
47
- @klass = klass
48
- @branch = Functions[:subclass?, Branch][klass]
49
- @converter = converter || proc { |args = {}| args }
50
- IceNine.deep_freeze(self)
51
- end
52
-
53
- # Builds the AST node
54
- #
55
- # @param [Object, Array] args
56
- # The argument of the command that should be converted to node attributes
57
- # @param [Proc] block
58
- #
59
- # @return [AbstractMapper::Node]
60
- #
61
- def call(*args, &block)
62
- block = nil if @branch
63
- klass.new(converter.call(*args), &block)
64
- end
65
-
66
- end # class Command
67
-
68
- end # class AbstractMapper
@@ -1,87 +0,0 @@
1
- # encoding: utf-8
2
-
3
- class AbstractMapper
4
-
5
- # An immutable node of the abstract syntax tree (AST), that describes
6
- # either some "end-up" transformation, or a level of nested input data.
7
- #
8
- # Every node is expected to accept attributes and (possibly) block, and
9
- # implement `#transproc` that implements the `#call` method to
10
- # transform input data to the output.
11
- #
12
- # Nodes describe only the structure of AST, they know
13
- # neither how to build the tree with DSL (see [AbstractMapper::Builder]),
14
- # nor how to optimize it later (see [AbstractMapper::Optimizer]).
15
- #
16
- # @api public
17
- #
18
- class Node
19
-
20
- include Attributes # adds attributes and their DSL
21
- include Comparable
22
-
23
- # @!attribute [r] block
24
- #
25
- # @return [Proc] The block given to initializer
26
- #
27
- attr_reader :block
28
-
29
- # @private
30
- def initialize(_ = {}, &block)
31
- super
32
- @block = block
33
- IceNine.deep_freeze(self)
34
- end
35
-
36
- # @!method transproc
37
- # The transformation function for the branch
38
- #
39
- # @return [Transproc::Function]
40
- #
41
- # @abstract
42
- #
43
- def transproc
44
- Functions[:identity]
45
- end
46
-
47
- # Returns a human-readable string representating the node
48
- #
49
- # @return [String]
50
- #
51
- def inspect
52
- "<#{self}>"
53
- end
54
-
55
- # Converts the node into string with its name and attributes
56
- #
57
- # @return [String]
58
- #
59
- def to_s
60
- "#{__name__}#{__attributes__}"
61
- end
62
-
63
- # Compares the node to another one by type and attributes
64
- #
65
- # @param [Object] other
66
- #
67
- # @return [Boolean]
68
- #
69
- def ==(other)
70
- other.instance_of?(self.class) && attributes.eql?(other.attributes)
71
- end
72
- alias_method :eql?, :==
73
-
74
- private
75
-
76
- def __name__
77
- self.class.name.split("::").last
78
- end
79
-
80
- def __attributes__
81
- return if attributes.empty?
82
- "(#{attributes.map { |k, v| "#{k}: #{v.inspect}" }.join(", ")})"
83
- end
84
-
85
- end # class Node
86
-
87
- end # class AbstractMapper