qor_dsl 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -69,7 +69,7 @@ Qor Dsl - DSL made easy!
69
69
  Gemfile.find(:gem)
70
70
 
71
71
  # Find by type and name also chain query
72
- Gemfile.find(:group, 'development').find(:gem)
72
+ Gemfile.first(:group, 'development').find(:gem)
73
73
 
74
74
  # Get all gems
75
75
  Gemfile.deep_find(:gem)
data/example/gemfile.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'qor_dsl'
3
+ require File.expand_path(File.join(File.dirname(__FILE__), '../lib/qor_dsl'))
4
4
 
5
5
  class Gemfile
6
6
  include Qor::Dsl
@@ -31,7 +31,7 @@ Gemfile.find(:gem) do |n|
31
31
  !!n.options[:git].nil?
32
32
  end
33
33
 
34
- Gemfile.find(:group, 'development').find(:gem)
34
+ Gemfile.first(:group, 'development').find(:gem)
35
35
 
36
36
  Gemfile.deep_find(:gem)
37
37
  Gemfile.deep_find(:gem) do |n|
@@ -40,7 +40,7 @@ module Qor
40
40
  self.instance_eval method_defination
41
41
  end
42
42
 
43
- def to_s
43
+ def inspect
44
44
  obj_options = {
45
45
  'name' => __name,
46
46
  'parent' => __parent && __parent.__name,
data/lib/qor_dsl/node.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Qor
2
2
  module Dsl
3
3
  class Node
4
- attr_accessor :name, :config, :parent, :children, :data, :options, :block, :all_nodes, :dummy
4
+ attr_accessor :name, :config, :parent, :children, :data, :block, :all_nodes, :dummy
5
5
 
6
6
  def initialize(name=nil, options={})
7
7
  self.name = name
@@ -9,6 +9,23 @@ module Qor
9
9
  self.dummy = options[:dummy]
10
10
  end
11
11
 
12
+ def node(type, options={}, &blk)
13
+ config.node(type, options, &blk)
14
+ end
15
+
16
+ def add_config(config)
17
+ self.config = config
18
+ config.__node = self
19
+ end
20
+
21
+ def add_child(child)
22
+ child.parent = self
23
+ children << child
24
+ root.all_nodes ||= []
25
+ root.all_nodes << child
26
+ end
27
+
28
+
12
29
  ## Node Config
13
30
  def config_name
14
31
  config.__name
@@ -18,71 +35,68 @@ module Qor
18
35
  config.__options || {} rescue {}
19
36
  end
20
37
 
21
- def child_config(type)
22
- config.__children[type] || nil
23
- end
24
38
 
25
- def child_config_options(type)
26
- child_config(type).__options || {} rescue {}
39
+ ## Node
40
+ def parents
41
+ parent ? [parent, parent.parents].flatten : []
27
42
  end
28
43
 
29
- def dummy?
30
- dummy
44
+ def children
45
+ @children ||= []
46
+ @children = @children.flatten.compact
47
+ @children
31
48
  end
32
49
 
33
- def is_node?(cname=nil, sname=nil)
34
- (cname.nil? || (config_name.to_s == cname.to_s)) && (sname.nil? || (name.to_s == sname.to_s))
50
+ def root
51
+ parent ? parent.root : self
35
52
  end
36
53
 
37
54
  def root?
38
55
  root == self
39
56
  end
40
57
 
41
- def root
42
- parent ? parent.root : self
58
+ def dummy?
59
+ !!dummy
43
60
  end
44
61
 
45
- def parents
46
- parent ? [parent, parent.parents].flatten : []
62
+ def is_node?(node_type=nil, name_str=nil)
63
+ (node_type.nil? || (config_name.to_s == node_type.to_s)) && (name_str.nil? || (name.to_s == name_str.to_s))
47
64
  end
48
65
 
49
- def options
50
- return @options if @options.is_a?(Hash)
51
- return data[-1] if data.is_a?(Array) && data[-1].is_a?(Hash)
52
- return data if data.is_a?(Hash)
53
- return config_options[:default_options] || {} if dummy?
54
- {}
66
+
67
+ ## Node Data
68
+ def default_value
69
+ config_options[:default_value]
55
70
  end
56
71
 
57
72
  def value
58
- ((config.__children.size > 0 || block.nil?) ? (options[:value] || name) : block.call) ||
59
- (dummy? ? config_options[:default_value] : nil)
73
+ ((config.__children.size > 0 || block.nil?) ? (options[:value] || name) : block.call) || default_value
60
74
  end
61
75
 
62
- def block
63
- @block || (dummy? ? config_options[:default_block] : nil)
76
+ def default_options
77
+ config_options[:default_options]
64
78
  end
65
79
 
66
- def add_config(config)
67
- self.config = config
68
- config.__node = self
80
+ def options
81
+ return data[-1] if data.is_a?(Array) && data[-1].is_a?(Hash)
82
+ return data if data.is_a?(Hash)
83
+ return name if name.is_a?(Hash)
84
+ return default_options || {}
69
85
  end
70
86
 
71
- def node(type, options={}, &blk)
72
- config.node(type, options, &blk)
87
+ def default_block
88
+ config_options[:default_block]
73
89
  end
74
90
 
75
- def children
76
- @children ||= []
77
- @children = @children.flatten.compact
78
- @children
91
+ def block
92
+ @block || default_block
79
93
  end
80
94
 
81
- def add_child(child)
82
- child.parent = self
83
- children << child
84
- root.all_nodes ||= []
85
- root.all_nodes << child
95
+
96
+ ## Query
97
+ def first(type=nil, name=nil, &block)
98
+ selected_children = find(type, name, &block)
99
+ selected_children.is_a?(Array) ? selected_children[0] : selected_children
86
100
  end
87
101
 
88
102
  def deep_find(type=nil, name=nil, &block)
@@ -97,28 +111,15 @@ module Qor
97
111
  end
98
112
 
99
113
  results = parent.find(type, name, &block) if results.length == 0 && child_config_options(type)[:inherit]
100
- results = process_find_results(results, type)
101
-
102
- return results[0] if !name.nil? && results.is_a?(Array) && results.length == 1
103
-
104
- results
105
- end
106
-
107
- def first(type=nil, name=nil, &block)
108
- selected_children = find(type, name, &block)
109
- selected_children.is_a?(Array) ? selected_children[0] : selected_children
114
+ process_find_results(results, type)
110
115
  end
111
116
 
112
- ## Inspect
113
- def inspect_name
114
- "{#{config_name}: #{name || 'nil'}}"
115
- end
116
117
 
117
- def to_s
118
+ def inspect
118
119
  obj_options = {
119
120
  'name' => name,
120
- 'parent' => parent && parent.inspect_name,
121
121
  'config' => config_name,
122
+ 'parent' => parent && parent.inspect_name,
122
123
  'children' => children.map(&:inspect_name),
123
124
  'data' => data,
124
125
  'block' => block
@@ -126,6 +127,7 @@ module Qor
126
127
  Qor::Dsl.inspect_object(self, obj_options)
127
128
  end
128
129
 
130
+
129
131
  private
130
132
  def process_find_results(results, type)
131
133
  if results.length == 0 &&
@@ -134,6 +136,18 @@ module Qor
134
136
  end
135
137
  results
136
138
  end
139
+
140
+ def child_config(type)
141
+ config.__children[type] || nil
142
+ end
143
+
144
+ def child_config_options(type)
145
+ child_config(type).__options || {} rescue {}
146
+ end
147
+
148
+ def inspect_name
149
+ "{#{config_name}: #{name || 'nil'}}"
150
+ end
137
151
  end
138
152
  end
139
153
  end
data/lib/qor_dsl.rb CHANGED
@@ -11,7 +11,11 @@ module Qor
11
11
 
12
12
  def self.inspect_object(obj, options)
13
13
  options = options.inject({}) do |summary, value|
14
- summary[value[0]] = value[1] if value[1] && value[1].to_s.length > 0
14
+ unless [:nil?, :empty?, :blank?].any? { |method|
15
+ value[1].respond_to?(method) && value[1].send(method)
16
+ }
17
+ summary[value[0]] = value[1]
18
+ end
15
19
  summary
16
20
  end
17
21
 
data/qor_dsl.gemspec CHANGED
@@ -11,5 +11,5 @@ Gem::Specification.new do |gem|
11
11
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
12
12
  gem.name = "qor_dsl"
13
13
  gem.require_paths = ["lib"]
14
- gem.version = "0.2.1"
14
+ gem.version = "0.3.0"
15
15
  end
data/test/layout_test.rb CHANGED
@@ -3,7 +3,7 @@ require File.join(File.dirname(__FILE__), 'configure')
3
3
 
4
4
  describe Layout do
5
5
  def many_times
6
- threads = 20.times.map do |t|
6
+ threads = 100.times.map do |t|
7
7
  Thread.new do
8
8
  yield
9
9
  end
@@ -19,11 +19,11 @@ describe Layout do
19
19
  Layout::Configuration.find(:template).length.must_equal 2
20
20
  Layout::Configuration.deep_find(:template).length.must_equal 3
21
21
  # :settings, :meta, :meta, :meta, :meta, :context, :template
22
- Layout::Configuration.find(:gadget, :quick_buy).deep_find().length.must_equal 7
22
+ Layout::Configuration.first(:gadget, :quick_buy).deep_find().length.must_equal 7
23
23
 
24
24
  # Find by name
25
- Layout::Configuration.find(:gadget, 'quick_buy').name.must_equal :quick_buy
26
- Layout::Configuration.deep_find(:desc, "From Google").value.must_equal "From Google"
25
+ Layout::Configuration.first(:gadget, 'quick_buy').name.must_equal :quick_buy
26
+ Layout::Configuration.deep_find(:desc, "From Google")[0].value.must_equal "From Google"
27
27
 
28
28
  # Find by block
29
29
  Layout::Configuration.first(:template) do |n|
@@ -31,11 +31,11 @@ describe Layout do
31
31
  end.value.must_equal 'Hello World2'
32
32
 
33
33
  Layout::Configuration.deep_find(:desc) do |n|
34
- n.parents.include?(Layout::Configuration.find(:action, :google))
34
+ n.parents.include?(Layout::Configuration.first(:action, :google))
35
35
  end[0].value.must_equal 'From Google'
36
36
 
37
37
  # Inherit
38
- Layout::Configuration.find(:gadget, :product_link).find(:template)[0].value.must_equal "Hello World"
38
+ Layout::Configuration.first(:gadget, :product_link).find(:template)[0].value.must_equal "Hello World"
39
39
 
40
40
  # Store any data
41
41
  Layout::Configuration.first(:template).data.must_equal ["v1", {:since => "09:00", :to => "12:00"}]
@@ -44,12 +44,10 @@ describe Layout do
44
44
  Layout::Configuration.find(:template)[1].options.must_equal({:since => "13:00", :to => "18:00"})
45
45
 
46
46
  # Parents
47
- Layout::Configuration.find(:gadget, :quick_buy).first(:template).parents.count.must_equal 2
47
+ Layout::Configuration.first(:gadget, :quick_buy).first(:template).parents.count.must_equal 2
48
48
 
49
49
  # Value for node
50
- Layout::Configuration.find(:gadget, :quick_buy).value.must_equal :quick_buy
51
-
52
- Layout::Configuration.find(:gadget, :quick_buy).value.must_equal :quick_buy
50
+ Layout::Configuration.first(:gadget, :quick_buy).value.must_equal :quick_buy
53
51
  end
54
52
  # More is coming... (multi, alias_node)
55
53
  end
@@ -57,7 +55,7 @@ describe Layout do
57
55
  it "test node helper" do
58
56
  many_times do
59
57
  Layout::Configuration.load('test/layout.rb', :force => true)
60
- node = Layout::Configuration.find(:gadget, :quick_buy)
58
+ node = Layout::Configuration.first(:gadget, :quick_buy)
61
59
  node.is_node?(:gadget).must_equal true
62
60
  node.is_node?(:gadget, 'quick_buy').must_equal true
63
61
  node.is_node?(:template).must_equal false
@@ -104,12 +102,42 @@ describe Layout do
104
102
  end
105
103
  end
106
104
 
107
- it "default value should works" do
105
+ it "default value" do
108
106
  many_times do
109
- Layout::Configuration.load('test/layout.rb', :force => true)
110
- Layout::Configuration.first(:layout).first(:description).value.must_equal 'TODO'
111
- Layout::Configuration.first(:layout).first(:description_block).value.must_equal 'FIXME'
112
- refute_nil Layout::Configuration.first(:layout).first(:description_block).block
107
+ config = Layout::Configuration.load(nil, :force => true) do
108
+ layout "new" do
109
+ description
110
+ end
111
+ end
112
+ # default value
113
+ config.first(:layout).first(:description).value.must_equal 'TODO'
114
+ config.first(:layout).first(:description).dummy?.must_equal false
115
+
116
+ # default block
117
+ config.first(:layout).first(:description_block).value.must_equal 'FIXME'
118
+ refute_nil config.first(:layout).first(:description_block).block
119
+ end
120
+ end
121
+
122
+ it "find dummy nodes" do
123
+ many_times do
124
+ config = Layout::Configuration.load(nil, :force => true) do
125
+ layout "new"
126
+ end
127
+ config.first(:layout).first(:description).value.must_equal 'TODO'
128
+ config.first(:layout).first(:description).dummy?.must_equal true
129
+ config.first(:layout).find(:description).count.must_equal 1
130
+ end
131
+ end
132
+
133
+ it "name as options" do
134
+ many_times do
135
+ config = Layout::Configuration.load(nil, :force => true) do
136
+ layout "new" do
137
+ description :hello => true
138
+ end
139
+ end
140
+ config.first(:layout).first(:description).options.must_equal({:hello => true})
113
141
  end
114
142
  end
115
143
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qor_dsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-27 00:00:00.000000000 Z
12
+ date: 2012-11-08 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Qor DSL is designed to be as flexible as possible while helping you to
15
15
  create your DSLs.
@@ -46,18 +46,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
46
46
  - - ! '>='
47
47
  - !ruby/object:Gem::Version
48
48
  version: '0'
49
- segments:
50
- - 0
51
- hash: 2719584306054087370
52
49
  required_rubygems_version: !ruby/object:Gem::Requirement
53
50
  none: false
54
51
  requirements:
55
52
  - - ! '>='
56
53
  - !ruby/object:Gem::Version
57
54
  version: '0'
58
- segments:
59
- - 0
60
- hash: 2719584306054087370
61
55
  requirements: []
62
56
  rubyforge_project:
63
57
  rubygems_version: 1.8.24