lipa 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/NEWS.md CHANGED
@@ -1,9 +1,59 @@
1
+ 2011-10-27 Release-0.3.0
2
+ -----------------------
3
+ - Added supporting references to other object as attribute ```Node#ref```
4
+
5
+ ```Ruby
6
+ node :node_1
7
+ node :node_2 do
8
+ param_1 ref("../node_1")
9
+ end
10
+ ```
11
+
12
+ - Added access to object by path in Unix style for ```Node#[]```
13
+
14
+ ```Ruby
15
+ node["dir_1/dir_2/searched_obj"]
16
+ node["searched_obj"]
17
+ node["./searched_obj"]
18
+ node["../dir_2/searched_obj"]
19
+ ```
20
+
21
+ - Added ```Node#run``` for wraping code
22
+
23
+ ``` Ruby
24
+ node :some_node do
25
+ some_param run{ rand(10) }
26
+ end
27
+ ```
28
+ - Added load description of trees from external files ```Tree#load_from```:
29
+
30
+ ```Ruby
31
+ Lipa::Tree.new "lipa" do
32
+ load_from File.dirname(__FILE__) + "/data/part_of_tree.rb"
33
+ end
34
+ ```
35
+
36
+ - Kind for node is default:
37
+
38
+ ```Ruby
39
+ t = Lipa::Tree.new("1") do
40
+ kind :some_kind
41
+
42
+ some_kind :obj_1
43
+ end
44
+ ```
45
+ - Added ```tree``` attribute for node
46
+ - Deleted deprecated classes ```Lipa::Leaf``` and ```Lipa::Branch```
47
+ - Deleted deprecated methods ```Node#leafs``` and ```Node#branch```
48
+
1
49
  2011-10-20 Release-0.2.2
50
+ -------------------------
2
51
 
3
52
  - Fixed issue [#1](https://github.com/flipback/lipa/issues/1) in template functional.
4
53
  Lambda expressions is not supporting. Use `Proc.new {}` for added calculation in your trees
5
54
 
6
55
  2011-10-19 Release-0.2.1
56
+ ------------------------
7
57
 
8
58
  - Fixed bug in Lipa::Bunch class. Now is working:
9
59
 
data/README.md CHANGED
@@ -3,7 +3,7 @@ Lipa - DSL for description treelike structures in Ruby
3
3
 
4
4
  Features
5
5
  ------------------------------------------------------
6
- - Dynamic creating treelike structures for Ruby
6
+ - Creating treelike structures for Ruby in DSL style
7
7
  - Flexible syntax
8
8
  - Supporting templates and scope initialization
9
9
  - Supporting Proc object as attributes
@@ -14,31 +14,49 @@ Installation
14
14
 
15
15
  Example
16
16
  ------------------------------------------------------
17
- ```Ruby
18
- require 'lipa'
19
17
 
20
- tree = Lipa::Tree.new :tree do
21
- kind :red, :for => :node do
22
- color "red"
18
+ require 'lipa'
19
+ un = Lipa::Tree.new :universe do
20
+ kind :planet_system do
21
+ num_planet run{
22
+ count = 0
23
+ children.values.each do |planet|
24
+ count += 1 if planet.kind == :planet
25
+ end
26
+ count
27
+ }
28
+ end
29
+
30
+ kind :planet do
31
+ has_live false
32
+ has_water false
33
+ number 0
34
+ end
35
+
36
+ planet_system :sun_system do
37
+ planet :mercury do
38
+ number 1
39
+ radius 46_001_210
40
+ end
41
+
42
+ planet :venus do
43
+ number 2
44
+ radius 107_476_259
45
+ end
46
+
47
+ planet :earth do
48
+ number 3
49
+ radius 147_098_074
50
+ has_live true
51
+ has_water true
52
+
53
+ node :moon, :radius => 363_104
54
+ end
55
+ end
23
56
  end
24
57
 
25
- node :branch do
26
- with :color => "green", do
27
- node :leaf_green
28
- node :leaf_yelow, :color => "yelow"
29
- end
30
- end
31
-
32
- red :red_leaf
33
- end
34
-
35
- #Access
36
- puts Lipa::Tree["tree://branch/leaf_green"].color
37
- #or
38
- puts tree["branch/leaf_yelow"].color
39
- #or
40
- puts tree.red_leaf.color
41
- ```
58
+ un.sun_system.num_planet #=> 3
59
+ un.sun_system.earth.radius #=> 147098074
42
60
 
43
61
  Reference
44
62
  ----------------------------------
data/Rakefile CHANGED
@@ -17,4 +17,7 @@ RSpec::Core::RakeTask.new(:spec) do |spec|
17
17
  spec.pattern = FileList['spec/**/*_spec.rb']
18
18
  end
19
19
 
20
+ require 'yard'
21
+ YARD::Rake::YardocTask.new
22
+
20
23
  task :default => :spec
@@ -2,7 +2,7 @@ $:.unshift File.join(File.dirname(__FILE__),'../lib')
2
2
  require 'lipa'
3
3
 
4
4
  tree = Lipa::Tree.new :tree do
5
- kind :red, :for => :node do
5
+ kind :red do
6
6
  color "red"
7
7
  end
8
8
 
@@ -0,0 +1,77 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'../lib')
2
+ require "lipa"
3
+
4
+ store = Lipa::Tree.new :store do
5
+ kind :category do
6
+ unit_count run{
7
+ count = 0
8
+ children.values.each do |child|
9
+ case child.kind
10
+ when :unit
11
+ count += child.count
12
+ when :category
13
+ count += child.unit_count
14
+ end
15
+ end
16
+
17
+ count
18
+ }
19
+
20
+ total_cost run{
21
+ cost = 0
22
+ children.values.each do |child|
23
+ case child.kind
24
+ when :unit
25
+ cost += child.cost * child.count
26
+ when :category
27
+ cost += child.total_cost
28
+ end
29
+ end
30
+
31
+ cost
32
+ }
33
+
34
+ end
35
+
36
+ kind :unit do
37
+ count 0
38
+ cost 0.0
39
+ end
40
+
41
+ category :electorinics do
42
+ category :video do
43
+ with :label => "Sony" do
44
+ unit :tv_1 do
45
+ count 231
46
+ part_number "123-567-a"
47
+ cost 100.0
48
+ end
49
+
50
+ unit :tv_2 do
51
+ count 27
52
+ part_number "123-567-b"
53
+ cost 199.0
54
+ end
55
+ end
56
+
57
+ unit :tv_3 do
58
+ count 98
59
+ part_number "123-567-c"
60
+ cost 299.0
61
+ label "Samsung"
62
+ end
63
+ end
64
+
65
+ category :audio do
66
+ unit :player_1 do
67
+ count 193
68
+ part_number "123-567-d"
69
+ cost 99.0
70
+ label "Apple"
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ puts store.electorinics.video.unit_count
77
+ puts store.electorinics.video.total_cost
@@ -2,11 +2,17 @@ $:.unshift File.join(File.dirname(__FILE__),'../lib')
2
2
  require 'lipa'
3
3
 
4
4
  un = Lipa::Tree.new :universe do
5
- kind :planet_system, :for => :node do
6
- num_planet 0
5
+ kind :planet_system do
6
+ num_planet run{
7
+ count = 0
8
+ children.values.each do |planet|
9
+ count += 1 if planet.kind == :planet
10
+ end
11
+ count
12
+ }
7
13
  end
8
14
 
9
- kind :planet, :for => :node do
15
+ kind :planet do
10
16
  has_live false
11
17
  has_water false
12
18
  number 0
@@ -39,4 +45,4 @@ puts un.sun_system.earth.number
39
45
  puts un.sun_system.earth.radius
40
46
  puts un.sun_system.earth.has_live
41
47
  puts un.sun_system.earth.has_water
42
- puts un.sun_system.earth.moon.radius
48
+ puts un.sun_system.num_planet
@@ -25,8 +25,6 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
25
 
26
26
  require "lipa/version"
27
27
  require "lipa/node"
28
- require "lipa/leaf"
29
- require "lipa/branch"
30
28
  require "lipa/bunch"
31
29
  require "lipa/kind"
32
30
  require "lipa/tree"
@@ -27,31 +27,31 @@ module Lipa
27
27
  # Implemenation of kind(template) for description
28
28
  #
29
29
  # @example
30
- #
31
30
  # tree = Lipa::Tree.new :tree do
32
31
  # kind :some_kind do
33
32
  # param1 "some_param"
34
33
  # end
35
34
  #
36
- # leaf :some_instance, :kind => :some_kind
35
+ # some_kind :some_instance
37
36
  # end
38
37
  # tree["some_instance"].param_1 #=> "some_param"
39
38
  #
40
- # alias #kind is #template
41
- class Kind < Node
42
- def method_missing(name, *args, &block)
43
- unless Node.add_node(name, self, *args, &block)
44
- # only assigment
45
- if args.size == 1
46
- name = name.to_s
47
- name["="] = "" if name["="]
48
- @attrs[name.to_sym] = args[0]
49
- else
50
- super
51
- end
39
+ # @see Tree#kind
40
+ class Kind
41
+ attr_reader :attrs, :name, :for, :block
42
+ def initialize(name, attrs = {}, &block)
43
+ # OPTIMIZE: Make it shorter
44
+ if attrs[:for]
45
+ @for = attr[:for]
46
+ attrs[:for] = nil
47
+ else
48
+ @for = :node
52
49
  end
53
- end
54
50
 
51
+ @attrs = attrs
52
+ @name = name.to_sym
53
+ @block = block if block_given?
54
+ end
55
55
  end
56
56
  end
57
57
 
@@ -27,34 +27,26 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
27
  module Lipa
28
28
  # Base object for all nested object of tree
29
29
  # It supports initialization
30
- # attributes by constant, variable or Proc object
30
+ # attributes by constant, variable or code
31
31
  #
32
32
  # @example
33
33
  # tree = Lipa::Tree.new :tree do
34
34
  # node :object, :param_1 => 4 do
35
35
  # param_2 "some_param"
36
- # param_3 lambda{1+param_3}
36
+ # param_3 run{1+param_3}
37
37
  # end
38
38
  # end
39
- # tree["object"].param_1 #=> 4
40
- # tree["object"].param_2 #=> "some_param"
41
- # tree["object"].param_3 #=> 5
39
+ # tree.object.param_1 #=> 4
40
+ # tree.object.param_2 #=> "some_param"
41
+ # tree.object.param_3 #=> 5
42
42
  class Node
43
43
  attr_accessor :attrs
44
44
  @@init_methods = {:node => self}
45
- @@kinds = {}
46
45
 
47
46
  def initialize(name, attrs = {}, &block)
48
- @attrs = {}
49
-
50
- #init attrs from template
51
- if attrs[:kind]
52
- kind = @@kinds[attrs[:kind].to_sym]
53
- @attrs.merge! kind.attrs if kind
54
- end
55
-
56
- @attrs.merge! attrs
47
+ @attrs = attrs
57
48
  @attrs[:name] = name.to_s
49
+ @attrs[:children] ||= {}
58
50
  instance_eval &block if block_given?
59
51
  end
60
52
 
@@ -66,6 +58,7 @@ module Lipa
66
58
  return child if child
67
59
 
68
60
  val = @attrs[name]
61
+ super unless val
69
62
  if val.class == Proc
70
63
  instance_eval &(val)
71
64
  else
@@ -81,15 +74,28 @@ module Lipa
81
74
  end
82
75
  end
83
76
 
84
- # Accessor for node by path
77
+ # Accessor for node by path in Unix style
85
78
  # @param [String] path nodes
86
79
  # @return [Node] node
87
80
  #
88
81
  # @example
89
- # tree["dir_1/dir_2/searched_obj"]
82
+ # dir_2["dir_1/dir_2/searched_obj"]
83
+ # dir_2["searched_obj"]
84
+ # dir_2["./searched_obj"]
85
+ # dir_2["../dir_2/searched_obj"]
90
86
  def [](path)
91
87
  split_path = path.split("/")
92
- obj = @attrs[:children][split_path[0].to_sym]
88
+ obj = case split_path[0]
89
+ when ""
90
+ @attrs[:tree]
91
+ when ".."
92
+ @attrs[:parent]
93
+ when "."
94
+ self
95
+ else
96
+ @attrs[:children][split_path[0].to_sym]
97
+ end
98
+
93
99
  if obj
94
100
  if split_path.size > 1
95
101
  obj[split_path[1..-1].join("/")]
@@ -104,19 +110,62 @@ module Lipa
104
110
  # @example
105
111
  # tree = Lipa::Tree.new :tree do
106
112
  # with :param_1 => "some_param" do
107
- # leaf :obj_1
108
- # leaf :obj_2
113
+ # node :obj_1
114
+ # node :obj_2
109
115
  # end
110
116
  # end
111
117
  #
112
- # tree["obj_1"].param_1 #=> "some_param"
113
- # tree["obj_2"].param_1 #=> "some_param"
118
+ # tree.obj_1.param_1 #=> "some_param"
119
+ # tree.obj_2.param_1 #=> "some_param"
114
120
  def with(attrs = {}, &block)
115
121
  if block_given?
116
122
  Lipa::Bunch.new(self, attrs, &block)
117
123
  end
118
124
  end
119
125
 
126
+ # Wraping code in attribute
127
+ #
128
+ # @param block of code
129
+ #
130
+ # @example
131
+ # Tree.new :tree do
132
+ # param_1 10
133
+ # param_2 run{ param_1 * 10 }
134
+ # end
135
+ def run(&block)
136
+ block
137
+ end
138
+
139
+ # Reference to othe object
140
+ # @param path in Unix style
141
+ #
142
+ # @example
143
+ #
144
+ # node :node_1
145
+ # node :node_2 do
146
+ # param_1 ref("../node_1")
147
+ # end
148
+ def ref(path)
149
+ Proc.new { self[path] }
150
+ end
151
+
152
+ # Accesor for methods for initialization
153
+ # node objects
154
+ #
155
+ # @param names of initial methods
156
+ #
157
+ # @example
158
+ # class Folder < Lipa::Node
159
+ # init_methods :folder
160
+ # end
161
+ #
162
+ # fls = Lipa::Tree.new :folders do
163
+ # folder :folder_1 do
164
+ # param_1 "some_param
165
+ # end
166
+ # end
167
+ #
168
+ # fls.folder_1.class #=> Folder
120
169
  def self.init_methods(*names)
121
170
  if names.size > 0
122
171
  names.each do |name|
@@ -134,42 +183,47 @@ module Lipa
134
183
  # @param args of node
135
184
  # @param block for node
136
185
  def self.add_node(name, parent, *args, &block)
137
- parent.attrs[:children] ||= {}
138
-
186
+ # OPTIMIZE: This code looks bad
139
187
  # Init from kind
140
- kind = @@kinds[name]
188
+ attrs = {}
189
+ args[1] ||= {}
190
+ attrs.merge!(args[1])
191
+
192
+ kind = parent.attrs[:tree].kinds[name]
141
193
  if kind and kind.for
142
- init_class = @@init_methods[kind.for]
143
- args[1] ||= {}
144
- args[1][:kind] = kind.name
194
+ init_class = @@init_methods[kind.for]
195
+ attrs[:kind] = kind.name
196
+ attrs.merge!(kind.attrs) do |key,v1,v2|
197
+ v1
198
+ end
145
199
  else
146
200
  #from init methods
147
201
  init_class = @@init_methods[name]
148
202
  end
149
203
 
150
204
  if init_class
151
- args[1] ||= {}
152
- args[1][:parent] = parent
205
+ attrs[:parent] = parent
206
+ attrs[:tree] = parent.attrs[:tree]
153
207
  child_name = args[0].to_sym
154
208
 
155
209
  existen_child = parent.attrs[:children][child_name]
156
- args[1] = existen_child.attrs.merge(args[1]) if existen_child
157
-
158
- parent.attrs[:children][child_name] = init_class.send(:new, *args, &block )
210
+ attrs = existen_child.attrs.merge(args[1]) if existen_child
211
+
212
+ args[1].merge!(attrs) do |key,v1,v2|
213
+ v1
214
+ end
215
+
216
+ if kind
217
+ parent.attrs[:children][child_name] = init_class.send(:new, *args.clone, &kind.block)
218
+ parent.attrs[:children][child_name].attrs.merge!(attrs)
219
+ parent.attrs[:children][child_name].instance_exec(&block) if block_given?
220
+ else
221
+ parent.attrs[:children][child_name] = init_class.send(:new, *args, &block )
222
+ end
223
+ true
159
224
  else
160
225
  nil
161
226
  end
162
227
  end
163
-
164
- #Deprecated methods
165
- def bunch(attrs = {}, &block)
166
- warn "#{__FILE__}:#{__LINE__} Deprecated method. Please use Lipa::Node. It is removing in 0.3.0 version"
167
- with(attrs, &block)
168
- end
169
-
170
- def leafs
171
- warn "#{__FILE__}:#{__LINE__} Deprecated method. Please use Lipa::Node. It is removing in 0.3.0 version"
172
- attrs[:children]
173
- end
174
228
  end
175
229
  end
@@ -28,17 +28,20 @@ module Lipa
28
28
  # @example
29
29
  #
30
30
  # tree = Lipa::Tree.new :tree do
31
- # leaf :object do
31
+ # node :object do
32
32
  # param_1 "some_param"
33
- # param_2 lambda{1+2}
33
+ # param_2 run{param_1 + "!!!!"}
34
34
  # end
35
35
  # end
36
36
  #
37
- # tree["object"].param_1 #=> "some_param"
38
- # tree["object"].param_2 #=> 3
37
+ # tree.object.param_1 #=> "some_param"
38
+ # tree.object.param_2 #=> "some_param!!!!"
39
39
  class Tree < Node
40
+ attr_reader :kinds
40
41
  @@trees = {}
41
42
  def initialize(name, attrs = {}, &block_given)
43
+ @kinds = {}
44
+ attrs[:tree] = self
42
45
  super
43
46
  @@trees.merge! name.to_s => self
44
47
  end
@@ -51,10 +54,10 @@ module Lipa
51
54
  # param1 "some_param"
52
55
  # end
53
56
  #
54
- # leaf :some_instance, :kind => :some_kind
57
+ # some_kind :some_instance
55
58
  def kind(name, attrs = {}, &block)
56
- @@kinds ||= {}
57
- @@kinds[name.to_sym] = Lipa::Kind.new(name, attrs, &block)
59
+ attrs = { :tree => self }
60
+ @kinds[name.to_sym] = Lipa::Kind.new(name, attrs, &block)
58
61
  end
59
62
 
60
63
  alias_method :template, :kind
@@ -70,5 +73,19 @@ module Lipa
70
73
  tree, path = uri.split("://")
71
74
  @@trees[tree][path] if @@trees[tree]
72
75
  end
76
+
77
+ # Load description tree from file
78
+ #
79
+ # @param path to file
80
+ #
81
+ # @example
82
+ # Lipa::Tree.new "lipa" do
83
+ # load_from File.dirname(__FILE__) + "/data/part_of_tree.rb"
84
+ # end
85
+ def load_from(path)
86
+ File.open(path,'r') do |f|
87
+ instance_eval f.read
88
+ end
89
+ end
73
90
  end
74
91
  end
@@ -24,5 +24,5 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
24
  =end
25
25
 
26
26
  module Lipa
27
- VERSION = "0.2.2"
27
+ VERSION = "0.3.0"
28
28
  end
@@ -0,0 +1,37 @@
1
+ require 'lipa'
2
+
3
+ describe "access to object by path in Unix style" do
4
+ before :all do
5
+ @tree = Lipa::Tree.new "lipa" do
6
+ node :obj_1 do
7
+ node :obj_2 do
8
+ node :obj_3 do
9
+ node :obj_4
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ @node = @tree.obj_1.obj_2
16
+ end
17
+
18
+ it 'should have access by relative path' do
19
+ @node["obj_3/obj_4"].should eql(@tree.obj_1.obj_2.obj_3.obj_4)
20
+ end
21
+
22
+ it 'should have access by parent path with "./"' do
23
+ @node["./obj_3/obj_4"].should eql(@tree.obj_1.obj_2.obj_3.obj_4)
24
+ end
25
+
26
+ it 'should have access to parent by "../"' do
27
+ @node[".."].should eql(@tree.obj_1)
28
+ end
29
+
30
+ it 'should have access by "../obj_2/obj_3"' do
31
+ @node["../obj_2/obj_3"].should eql(@tree.obj_1.obj_2.obj_3)
32
+ end
33
+
34
+ it 'should have access by absolute path' do
35
+ @node["/obj_1/obj_2/obj_3"].should eql(@tree.obj_1.obj_2.obj_3)
36
+ end
37
+ end
@@ -1,14 +1,26 @@
1
- require File.dirname(__FILE__) + "/spec_helper"
1
+ require 'lipa'
2
2
 
3
3
  describe Lipa::Bunch do
4
+ before :all do
5
+ @tree = Lipa::Tree.new "lipa" do
6
+ with :attr_1 => 100, :attr_2 => "attr_2" do
7
+ node :obj_4
8
+ node :obj_5
9
+ node :obj_6, :attr_1 => 200, :attr_2 => ""
10
+ end
11
+ end
12
+
13
+ @node = @tree["group_1/obj_1"]
14
+ end
15
+
4
16
  it 'should create some object with any attributes' do
5
- tree["obj_4"].attr_1.should eql(100)
6
- tree["obj_5"].attr_1.should eql(100)
7
- tree["obj_6"].attr_1.should eql(200)
17
+ @tree["obj_4"].attr_1.should eql(100)
18
+ @tree["obj_5"].attr_1.should eql(100)
19
+ @tree["obj_6"].attr_1.should eql(200)
8
20
 
9
- tree["obj_4"].attr_2.should eql("attr_2")
10
- tree["obj_5"].attr_2.should eql("attr_2")
11
- tree["obj_6"].attr_2.should eql("")
21
+ @tree["obj_4"].attr_2.should eql("attr_2")
22
+ @tree["obj_5"].attr_2.should eql("attr_2")
23
+ @tree["obj_6"].attr_2.should eql("")
12
24
  end
13
25
 
14
26
  it 'should have "with" initial method' do
@@ -1,25 +1,57 @@
1
- require File.dirname(__FILE__) + "/spec_helper"
1
+ require 'lipa'
2
2
 
3
3
  describe Lipa::Kind do
4
+ before :all do
5
+ @tree = Lipa::Tree.new :tree do
6
+ # Set #1
7
+ kind :object, :for => :node
8
+ kind :group, :for => :node do
9
+ object :obj_x do
10
+ attr_0 "from_kind"
11
+ attr_1 "from_kind"
12
+ attr_3 run{ attr_2 * 2}
13
+ end
14
+ end
15
+
16
+ group :group_1 do
17
+ object :obj_x do
18
+ attr_1 "from_instance"
19
+ attr_2 2
20
+ end
21
+ end
22
+ # Set #2
23
+ kind :folder
24
+ kind :file do
25
+ size 1024
26
+ ext "jpg"
27
+ end
28
+
29
+ folder :folder_1 do
30
+ file :some_file, :ext => "txt"
31
+ end
32
+ end
33
+ end
34
+
4
35
  it 'should create brunch from template' do
5
- tree.group_3.obj_x.attr_0.should eql("from_kind")
36
+ @tree.group_1.obj_x.attr_0.should eql("from_kind")
6
37
  end
7
38
 
8
39
  it 'should support local changing in instances' do
9
- tree.group_3.obj_x.attr_1.should eql("from_instance")
40
+ @tree.group_1.obj_x.attr_1.should eql("from_instance")
41
+ end
42
+
43
+ it 'should have lazy calculation' do
44
+ @tree.group_1.obj_x.attr_2.should eql(2)
45
+ @tree.group_1.obj_x.attr_3.should eql(4)
10
46
  end
11
47
 
12
48
  it 'should create init method with name template' do
13
- tree.folder_1.some_file.size.should eql(1024)
14
- tree.folder_1.some_file.ext.should eql("txt")
49
+ @tree.folder_1.some_file.size.should eql(1024)
50
+ @tree.folder_1.some_file.ext.should eql("txt")
15
51
  end
16
52
 
17
53
  it 'should create node with your name' do
18
- tree.folder_1.name.should eql("folder_1")
54
+ @tree.folder_1.name.should eql("folder_1")
19
55
  end
20
56
 
21
- it 'should have lazy calculation' do
22
- tree.group_3.obj_x.attr_2.should eql(2)
23
- tree.group_3.obj_x.attr_3.should eql(4)
24
- end
25
57
  end
@@ -1,16 +1,56 @@
1
- require File.dirname(__FILE__) + "/spec_helper"
1
+ require 'lipa'
2
2
 
3
3
  describe Lipa::Node do
4
- before :each do
5
- @node = tree["group_1/obj_1"]
4
+ before :all do
5
+ @tree = Lipa::Tree.new "lipa" do
6
+ node :group_1 do
7
+ any_attr "any attr"
8
+
9
+ node :obj_1, :attr_1 => 5 do
10
+ attr_2 3
11
+ attr_3 run{attr_1 + attr_2}
12
+ attr_4 ref("/other_object")
13
+
14
+ node :obj_2
15
+ node :obj_3
16
+ end
17
+ end
18
+
19
+ node :other_object
20
+
21
+ kind :some_kind do
22
+ param_1 "something"
23
+ end
24
+
25
+ some_kind :obj_x do
26
+ some_kind :obj_y1 do
27
+ some_kind :obj_z1
28
+ some_kind :obj_z2
29
+ end
30
+
31
+ some_kind :obj_y2 do
32
+ some_kind :obj_z3
33
+ end
34
+ end
35
+ end
36
+
37
+ @node = @tree["group_1/obj_1"]
6
38
  end
7
39
 
8
40
  it 'should have name' do
9
41
  @node.name.should eql("obj_1")
10
42
  end
11
43
 
44
+ it 'should have tree' do
45
+ @node.tree.should eql(@tree)
46
+ end
47
+
48
+ it 'should kind' do
49
+ @tree["obj_x"].kind.should eql(:some_kind)
50
+ end
51
+
12
52
  it 'should have parent' do
13
- @node.parent.should eql(tree["group_1"])
53
+ @node.parent.should eql(@tree["group_1"])
14
54
  end
15
55
 
16
56
  it 'should have descripted attr_1 eql 5' do
@@ -39,15 +79,24 @@ describe Lipa::Node do
39
79
  end
40
80
 
41
81
  it 'should have children' do
42
- @node.children[:obj_2].should eql(tree["group_1/obj_1/obj_2"])
43
- @node.children[:obj_3].should eql(tree["group_1/obj_1/obj_3"])
82
+ @node.children.values.should eql([@tree["group_1/obj_1/obj_2"], @tree["group_1/obj_1/obj_3"]])
44
83
  end
45
84
 
46
85
  it 'should have [] for access entry by path' do
47
- @node["obj_3"].should eql(tree["group_1/obj_1/obj_3"])
86
+ @node["obj_3"].should eql(@tree["group_1/obj_1/obj_3"])
48
87
  end
49
88
 
50
89
  it 'should access for children by .' do
51
- @node.obj_3.should eql(tree["group_1/obj_1/obj_3"])
90
+ @node.obj_3.should eql(@tree["group_1/obj_1/obj_3"])
91
+ end
92
+
93
+ it 'should not have bug in tree of kind objects' do
94
+ @tree.obj_x.obj_y1.children.keys.should eql([:obj_z1, :obj_z2])
95
+ @tree.obj_x.children.keys.should eql([:obj_y1, :obj_y2])
96
+ @tree.obj_x.obj_y2.obj_z3.children.keys.should eql([])
97
+ end
98
+
99
+ it 'should have access other object by reference' do
100
+ @node.attr_4.should eql(@tree.other_object)
52
101
  end
53
102
  end
@@ -1,8 +1,30 @@
1
- require File.dirname(__FILE__) + "/spec_helper"
1
+ require 'lipa'
2
2
 
3
3
  describe Lipa::Tree do
4
+ before :all do
5
+ @tree = Lipa::Tree.new "lipa" do
6
+ node :group_1 do
7
+ any_attr "any attr"
8
+
9
+ node :obj_1, :attr_1 => 5 do
10
+ attr_2 3
11
+ attr_3 run{attr_1 + attr_2}
12
+
13
+ node :obj_2
14
+ node :obj_3
15
+ end
16
+ end
17
+
18
+ load_from File.dirname(__FILE__) + "/data/part_of_tree.rb"
19
+ end
20
+ end
21
+
4
22
  it "should have access any object in trees" do
5
- Lipa::Tree["lipa://group_1/obj_1"].should eql(tree["group_1/obj_1"])
23
+ Lipa::Tree["lipa://group_1/obj_1"].should eql(@tree["group_1/obj_1"])
24
+ end
25
+
26
+ it 'should load description from files' do
27
+ @tree.external_node.msg.should eql("Hello!")
6
28
  end
7
29
  end
8
30
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lipa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-20 00:00:00.000000000Z
12
+ date: 2011-10-27 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &11366740 !ruby/object:Gem::Requirement
16
+ requirement: &12833120 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *11366740
24
+ version_requirements: *12833120
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &11366060 !ruby/object:Gem::Requirement
27
+ requirement: &12795600 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *11366060
35
+ version_requirements: *12795600
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &11362040 !ruby/object:Gem::Requirement
38
+ requirement: &12794560 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,51 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *11362040
46
+ version_requirements: *12794560
47
+ - !ruby/object:Gem::Dependency
48
+ name: yard
49
+ requirement: &12793300 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *12793300
58
+ - !ruby/object:Gem::Dependency
59
+ name: rdiscount
60
+ requirement: &12792500 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *12792500
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: &12791100 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *12791100
80
+ - !ruby/object:Gem::Dependency
81
+ name: notes
82
+ requirement: &12789280 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *12789280
47
91
  description:
48
92
  email: atimin@gmail.com
49
93
  executables: []
@@ -56,17 +100,16 @@ files:
56
100
  - lib/lipa/version.rb
57
101
  - lib/lipa/kind.rb
58
102
  - lib/lipa/node.rb
59
- - lib/lipa/leaf.rb
60
103
  - lib/lipa/bunch.rb
61
104
  - lib/lipa/tree.rb
62
- - lib/lipa/branch.rb
63
105
  - spec/tree_spec.rb
64
106
  - spec/node_spec.rb
65
107
  - spec/kind_spec.rb
66
- - spec/spec_helper.rb
67
108
  - spec/bunch_spec.rb
109
+ - spec/access_by_path_spec.rb
68
110
  - examples/simple_tree.rb
69
111
  - examples/universe.rb
112
+ - examples/store.rb
70
113
  - Rakefile
71
114
  - README.md
72
115
  - NEWS.md
@@ -1,36 +0,0 @@
1
- =begin
2
- Lipa - DSL for description treelike structures in Ruby
3
-
4
- Copyright (c) 2011 Aleksey Timin
5
-
6
- Permission is hereby granted, free of charge, to any person obtaining
7
- a copy of this software and associated documentation files (the
8
- 'Software'), to deal in the Software without restriction, including
9
- without limitation the rights to use, copy, modify, merge, publish,
10
- distribute, sublicense, and/or sell copies of the Software, and to
11
- permit persons to whom the Software is furnished to do so, subject to
12
- the following conditions:
13
-
14
- The above copyright notice and this permission notice shall be
15
- included in all copies or substantial portions of the Software.
16
-
17
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
18
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
- =end
25
-
26
- module Lipa
27
- # Deprecte class
28
- # Don't use it
29
- class Branch < Node
30
- init_methods :branch, :dir, :group
31
- def initialize(name, attrs = {}, &block)
32
- warn "#{__FILE__}:#{__LINE__} Deprecated class. Please use Lipa::Node. It is removing in 0.3.0 version"
33
- super
34
- end
35
- end
36
- end
@@ -1,37 +0,0 @@
1
- =begin
2
- Lipa - DSL for description treelike structures in Ruby
3
-
4
- Copyright (c) 2011 Aleksey Timin
5
-
6
- Permission is hereby granted, free of charge, to any person obtaining
7
- a copy of this software and associated documentation files (the
8
- 'Software'), to deal in the Software without restriction, including
9
- without limitation the rights to use, copy, modify, merge, publish,
10
- distribute, sublicense, and/or sell copies of the Software, and to
11
- permit persons to whom the Software is furnished to do so, subject to
12
- the following conditions:
13
-
14
- The above copyright notice and this permission notice shall be
15
- included in all copies or substantial portions of the Software.
16
-
17
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
18
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
- =end
25
-
26
-
27
- module Lipa
28
- # Deprecte class
29
- # Don't use it
30
- class Leaf < Node
31
- init_methods :leaf, :object
32
- def initialize(name, attrs = {}, &block)
33
- warn "#{__FILE__}:#{__LINE__} Deprecated class. Please use Lipa::Node. It is removing in 0.3.0 version"
34
- super
35
- end
36
- end
37
- end
@@ -1,52 +0,0 @@
1
- require "lipa"
2
-
3
- TREE ||= Lipa::Tree.new "lipa" do
4
- node :group_1 do
5
- any_attr "any attr"
6
-
7
- node :obj_1, :attr_1 => 5 do
8
- attr_2 3
9
- attr_3 Proc.new{attr_1 + attr_2}
10
-
11
- node :obj_2
12
- node :obj_3
13
- end
14
- end
15
-
16
- with :attr_1 => 100, :attr_2 => "attr_2" do
17
- node :obj_4
18
- node :obj_5
19
- node :obj_6, :attr_1 => 200, :attr_2 => ""
20
- end
21
-
22
- # Template
23
- kind :kind_group do
24
- node :obj_x do
25
- attr_0 "from_kind"
26
- attr_1 "from_kind"
27
- attr_2 0
28
- attr_3 Proc.new{ attr_2 * 2}
29
- end
30
- end
31
-
32
- node :group_3, :kind => :kind_group do
33
- node :obj_x do
34
- attr_1 "from_instance"
35
- attr_2 2
36
- end
37
- end
38
- #or
39
- kind :folder, :for => :node
40
- kind :file, :for => :node do
41
- size 1024
42
- ext "jpg"
43
- end
44
-
45
- folder :folder_1 do
46
- file :some_file, :ext => "txt"
47
- end
48
- end
49
-
50
- def tree
51
- TREE
52
- end