easydsl 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +6 -0
- data/lib/easydsl.rb +1 -1
- data/lib/easydsl/node.rb +43 -20
- data/lib/easydsl/node_builder.rb +10 -4
- data/lib/easydsl/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 15e90cbb3aa3b31d9b1431a5a03150fcce3ee8fe
|
4
|
+
data.tar.gz: 22764b495dcf37ed851751466b834776d946bf9d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 018ac1248ba9677c22587f3e8d97a74b747c92d8361e6c9a1581615d119e7dee001856ffee64e846bf7c3520250feb51f4fd8407ab73093ccc65aa18f7889e6b
|
7
|
+
data.tar.gz: 5f7fc73e69c593f80308b108eccb254e6bb96fd359ea760149f4ca0747de3822db0c9f1e834de7c56fe140d5e525fd55baa31e4408fc29e748c3e98e4ee1c2c1
|
data/.rubocop.yml
CHANGED
data/lib/easydsl.rb
CHANGED
@@ -7,6 +7,6 @@ module Easydsl
|
|
7
7
|
raise(ArgumentError, 'A block is mandatory.') unless block_given?
|
8
8
|
tree = NodeBuilder.new('root')
|
9
9
|
tree.instance_exec(&block)
|
10
|
-
Node.new(tree.
|
10
|
+
Node.new(tree.get_name, tree.get_args, 0, nil, tree.get_nodes)
|
11
11
|
end
|
12
12
|
end
|
data/lib/easydsl/node.rb
CHANGED
@@ -3,66 +3,89 @@ require 'easydsl/node_array'
|
|
3
3
|
|
4
4
|
module Easydsl
|
5
5
|
class Node
|
6
|
-
attr_reader :name, :index, :parent, :singleton
|
7
|
-
attr_accessor :args
|
8
|
-
|
9
6
|
def initialize(name, args, index, parent, node_builders = [])
|
10
7
|
@name = name.to_s.chomp('!').to_sym
|
11
8
|
@singleton = name[-1] == '!'
|
12
9
|
@args = args
|
13
10
|
@index = index
|
14
11
|
@parent = parent
|
15
|
-
node_builders
|
16
|
-
add_child(item.name, item.args, i, self, item.nodes)
|
17
|
-
end
|
12
|
+
add_hierarchy(self, node_builders, 0)
|
18
13
|
end
|
19
14
|
|
20
|
-
def
|
15
|
+
def get_nodes
|
21
16
|
@nodes ||= Hash.new { |h, k| h[k] = NodeArray.new }
|
22
17
|
end
|
23
18
|
|
24
|
-
def
|
19
|
+
def get_all_nodes
|
25
20
|
all = []
|
26
21
|
nodes.each { |k, _v| all += nodes[k] }
|
27
|
-
all.sort! { |a, b| a.
|
22
|
+
all.sort! { |a, b| a.get_index <=> b.get_index }
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_name
|
26
|
+
@name
|
27
|
+
end
|
28
|
+
|
29
|
+
def get_args
|
30
|
+
@args
|
31
|
+
end
|
32
|
+
|
33
|
+
def set_args(value)
|
34
|
+
@args = value
|
28
35
|
end
|
29
36
|
|
30
|
-
def
|
31
|
-
|
37
|
+
def get_max_index
|
38
|
+
get_all_nodes.map(&:get_index).max || 0
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_index
|
42
|
+
@index
|
43
|
+
end
|
44
|
+
|
45
|
+
def get_parent
|
46
|
+
@parent
|
32
47
|
end
|
33
48
|
|
34
49
|
def define(&block)
|
35
50
|
add_block(&block)
|
36
51
|
end
|
37
52
|
|
53
|
+
def is_singleton?
|
54
|
+
@singleton
|
55
|
+
end
|
56
|
+
|
38
57
|
protected
|
39
58
|
|
59
|
+
def nodes
|
60
|
+
get_nodes
|
61
|
+
end
|
62
|
+
|
40
63
|
def add_hierarchy(to, tree, base_index)
|
41
64
|
tree.each_with_index do |item, index|
|
42
|
-
to.add_child(item.
|
65
|
+
to.add_child(item.get_name, item.get_args, base_index + index, to, item.get_nodes)
|
43
66
|
end
|
44
67
|
end
|
45
68
|
|
46
69
|
def add_block(&block)
|
47
70
|
tree = NodeBuilder.new('tree')
|
48
71
|
tree.instance_exec(&block)
|
49
|
-
add_hierarchy(self, tree.
|
72
|
+
add_hierarchy(self, tree.get_nodes, get_max_index)
|
50
73
|
end
|
51
74
|
|
52
75
|
def add_child(name, args, index, parent, node_builders = [])
|
53
76
|
node = handle_singleton(name, args, node_builders)
|
54
77
|
return node unless node.nil?
|
55
78
|
node = Node.new(name, args, index, parent, node_builders)
|
56
|
-
nodes[node.
|
79
|
+
nodes[node.get_name] << node
|
57
80
|
node
|
58
81
|
end
|
59
82
|
|
60
83
|
def handle_singleton(name, args, node_builders = [])
|
61
84
|
return nil unless nodes.key?(name)
|
62
85
|
node = nodes[name].first
|
63
|
-
return nil if node.nil? || node.
|
64
|
-
node.args
|
65
|
-
add_hierarchy(node, node_builders, node.
|
86
|
+
return nil if node.nil? || node.is_singleton? == false
|
87
|
+
node.set_args(args)
|
88
|
+
add_hierarchy(node, node_builders, node.get_max_index)
|
66
89
|
nodes[name].first
|
67
90
|
end
|
68
91
|
|
@@ -79,7 +102,7 @@ module Easydsl
|
|
79
102
|
child = if collection.count > 0
|
80
103
|
collection.first
|
81
104
|
else
|
82
|
-
add_child(method_symbol, args,
|
105
|
+
add_child(method_symbol, args, get_max_index, self)
|
83
106
|
end
|
84
107
|
child.add_block(&block)
|
85
108
|
end
|
@@ -100,9 +123,9 @@ module Easydsl
|
|
100
123
|
def handle_assignment(method_symbol, *args)
|
101
124
|
collection = nodes[method_symbol]
|
102
125
|
if collection.count > 0
|
103
|
-
collection.first.args
|
126
|
+
collection.first.set_args(args)
|
104
127
|
else
|
105
|
-
add_child(method_symbol, args,
|
128
|
+
add_child(method_symbol, args, get_max_index, self)
|
106
129
|
end
|
107
130
|
end
|
108
131
|
|
data/lib/easydsl/node_builder.rb
CHANGED
@@ -1,11 +1,17 @@
|
|
1
1
|
module Easydsl
|
2
2
|
class NodeBuilder
|
3
|
-
|
4
|
-
|
5
|
-
def nodes
|
3
|
+
def get_nodes
|
6
4
|
@nodes ||= []
|
7
5
|
end
|
8
6
|
|
7
|
+
def get_name
|
8
|
+
@name
|
9
|
+
end
|
10
|
+
|
11
|
+
def get_args
|
12
|
+
@args
|
13
|
+
end
|
14
|
+
|
9
15
|
def initialize(name, *args)
|
10
16
|
@name = name
|
11
17
|
@args = args
|
@@ -13,7 +19,7 @@ module Easydsl
|
|
13
19
|
|
14
20
|
def method_missing(method_symbol, *args, &block)
|
15
21
|
child = NodeBuilder.new(method_symbol, *args)
|
16
|
-
|
22
|
+
get_nodes.push(child)
|
17
23
|
child.instance_exec(&block) if block_given?
|
18
24
|
end
|
19
25
|
end
|
data/lib/easydsl/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easydsl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mirko Mignini
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|