qor_dsl 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -30,7 +30,7 @@ Qor Dsl - DSL made easy!
30
30
  include Qor::Dsl
31
31
  default_configs [ENV['BUNDLE_GEMFILE'], 'Gemfile']
32
32
 
33
- node :source
33
+ node :source, :default_value => 'http://rubygems.org'
34
34
  node :gem
35
35
 
36
36
  node :group do
data/example/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source 'http://rubygems.org'
1
+ #source 'http://rubygems.org'
2
2
 
3
3
  gem 'rails', '3.2.8'
4
4
  gem 'unicorn'
data/example/gemfile.rb CHANGED
@@ -6,7 +6,7 @@ class Gemfile
6
6
  include Qor::Dsl
7
7
  default_configs [ENV['BUNDLE_GEMFILE'], File.join(File.dirname(__FILE__), 'Gemfile')]
8
8
 
9
- node :source
9
+ node :source, :default_value => 'http://rubygems.org'
10
10
  node :gem
11
11
 
12
12
  node :group do
@@ -18,6 +18,9 @@ class Gemfile
18
18
  end
19
19
  end
20
20
 
21
+ # Default Value
22
+ Gemfile.first(:source)
23
+
21
24
  # Methods for query
22
25
  Gemfile.first(:gem)
23
26
  Gemfile.first(:gem, 'unicorn')
data/lib/qor_dsl/node.rb CHANGED
@@ -1,29 +1,43 @@
1
1
  module Qor
2
2
  module Dsl
3
3
  class Node
4
- attr_accessor :name, :config, :parent, :children, :data, :options, :block, :all_nodes
4
+ attr_accessor :name, :config, :parent, :children, :data, :options, :block, :all_nodes, :dummy
5
5
 
6
6
  def initialize(name=nil, options={})
7
7
  self.name = name
8
8
  self.add_config(options[:config] || Qor::Dsl::Config.new('ROOT', self))
9
+ self.dummy = options[:dummy]
9
10
  end
10
11
 
12
+ ## Node Config
11
13
  def config_name
12
14
  config.__name
13
15
  end
14
16
 
15
- def inspect_name
16
- "{#{config_name}: #{name || 'nil'}}"
17
+ def config_options
18
+ config.__options || {} rescue {}
17
19
  end
18
20
 
19
- def root?
20
- root == self
21
+ def child_config(type)
22
+ config.__children[type] || nil
23
+ end
24
+
25
+ def child_config_options(type)
26
+ child_config(type).__options || {} rescue {}
27
+ end
28
+
29
+ def dummy?
30
+ dummy
21
31
  end
22
32
 
23
33
  def is_node?(cname=nil, sname=nil)
24
34
  (cname.nil? || (config_name.to_s == cname.to_s)) && (sname.nil? || (name.to_s == sname.to_s))
25
35
  end
26
36
 
37
+ def root?
38
+ root == self
39
+ end
40
+
27
41
  def root
28
42
  parent ? parent.root : self
29
43
  end
@@ -36,11 +50,17 @@ module Qor
36
50
  return @options if @options.is_a?(Hash)
37
51
  return data[-1] if data.is_a?(Array) && data[-1].is_a?(Hash)
38
52
  return data if data.is_a?(Hash)
53
+ return config_options[:default_options] || {} if dummy?
39
54
  {}
40
55
  end
41
56
 
42
57
  def value
43
- (config.__children.size > 0 || block.nil?) ? (options[:value] || name) : block.call
58
+ ((config.__children.size > 0 || block.nil?) ? (options[:value] || name) : block.call) ||
59
+ (dummy? ? config_options[:default_value] : nil)
60
+ end
61
+
62
+ def block
63
+ @block || (dummy? ? config_options[:default_block] : nil)
44
64
  end
45
65
 
46
66
  def add_config(config)
@@ -58,10 +78,6 @@ module Qor
58
78
  @children
59
79
  end
60
80
 
61
- def config_options_for_child(type)
62
- config.__children[type].__options || {} rescue {}
63
- end
64
-
65
81
  def add_child(child)
66
82
  child.parent = self
67
83
  children << child
@@ -76,13 +92,16 @@ module Qor
76
92
  end
77
93
 
78
94
  def find(type=nil, name=nil, nodes=children, &block)
79
- selected_children = nodes.select do |child|
95
+ results = nodes.select do |child|
80
96
  child.is_node?(type, name) && (block.nil? ? true : block.call(child))
81
97
  end
82
98
 
83
- return selected_children[0] if !name.nil? && selected_children.length == 1
84
- return parent.find(type, name) if (selected_children.length == 0) && config_options_for_child(type)[:inherit]
85
- selected_children
99
+ 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
86
105
  end
87
106
 
88
107
  def first(type=nil, name=nil, &block)
@@ -90,6 +109,11 @@ module Qor
90
109
  selected_children.is_a?(Array) ? selected_children[0] : selected_children
91
110
  end
92
111
 
112
+ ## Inspect
113
+ def inspect_name
114
+ "{#{config_name}: #{name || 'nil'}}"
115
+ end
116
+
93
117
  def to_s
94
118
  obj_options = {
95
119
  'name' => name,
@@ -101,6 +125,15 @@ module Qor
101
125
  }
102
126
  Qor::Dsl.inspect_object(self, obj_options)
103
127
  end
128
+
129
+ private
130
+ def process_find_results(results, type)
131
+ if results.length == 0 &&
132
+ %w(default_options default_value default_block).any? {|x| child_config_options(type)[x.to_sym] }
133
+ results = [Node.new(nil, :config => child_config(type), :dummy => true)]
134
+ end
135
+ results
136
+ end
104
137
  end
105
138
  end
106
139
  end
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.0"
14
+ gem.version = "0.2.1"
15
15
  end
data/test/configure.rb CHANGED
@@ -18,6 +18,8 @@ module Layout
18
18
  end
19
19
 
20
20
  node :layout do
21
+ node :description, :default_value => "TODO"
22
+ node :description_block, :default_block => proc {|x| 'FIXME' }
21
23
  node :gadgets
22
24
  end
23
25
 
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 = 100.times.map do |t|
6
+ threads = 20.times.map do |t|
7
7
  Thread.new do
8
8
  yield
9
9
  end
@@ -103,4 +103,13 @@ describe Layout do
103
103
  lambda { Layout::Configuration.load("non_existing_file.rb", :force => true) }.must_raise Qor::Dsl::ConfigurationNotFound
104
104
  end
105
105
  end
106
+
107
+ it "default value should works" do
108
+ 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
113
+ end
114
+ end
106
115
  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.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -48,7 +48,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
48
48
  version: '0'
49
49
  segments:
50
50
  - 0
51
- hash: 3416388606378692493
51
+ hash: 2719584306054087370
52
52
  required_rubygems_version: !ruby/object:Gem::Requirement
53
53
  none: false
54
54
  requirements:
@@ -57,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
57
  version: '0'
58
58
  segments:
59
59
  - 0
60
- hash: 3416388606378692493
60
+ hash: 2719584306054087370
61
61
  requirements: []
62
62
  rubyforge_project:
63
63
  rubygems_version: 1.8.24