qor_dsl 0.0.5 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +78 -2
- data/example/Gemfile +13 -0
- data/example/gemfile.rb +50 -0
- data/lib/qor_dsl/class_method.rb +5 -1
- data/lib/qor_dsl/config.rb +16 -1
- data/lib/qor_dsl/node.rb +32 -8
- data/qor_dsl.gemspec +4 -4
- data/test/layout.rb +7 -0
- data/test/layout_test.rb +12 -0
- metadata +8 -5
data/README.md
CHANGED
@@ -1,4 +1,80 @@
|
|
1
|
-
|
1
|
+
Qor Dsl - DSL made easy!
|
2
2
|
=======
|
3
3
|
|
4
|
-
|
4
|
+
## Description
|
5
|
+
|
6
|
+
Why DSL? Easy to read, Easy to write, Easy to maintain!
|
7
|
+
Why Qor DSL? Make it even easier! Write your DSLs in 10 minutes!
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
1, Adding it as dependency to your Gemspec or Gemfile:
|
12
|
+
|
13
|
+
# your_awesome_gem.gemspec
|
14
|
+
Gem::Specification.new do |gem|
|
15
|
+
gem.add_dependency "qor_dsl"
|
16
|
+
end
|
17
|
+
|
18
|
+
# Gemfile
|
19
|
+
gem "qor_dsl"
|
20
|
+
|
21
|
+
2, Defining DSL (Sample configuration support DSLs like [Gemfile](http://gembundler.com))
|
22
|
+
|
23
|
+
# gemfile.rb
|
24
|
+
class Gemfile
|
25
|
+
include Qor::Dsl
|
26
|
+
default_configs [ENV['BUNDLE_GEMFILE'], 'Gemfile']
|
27
|
+
|
28
|
+
node :source
|
29
|
+
node :gem
|
30
|
+
|
31
|
+
node :group do
|
32
|
+
node :gem
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
3, Defining config (Sample Gemfile file used for above config)
|
37
|
+
|
38
|
+
# Gemfile
|
39
|
+
source 'http://rubygems.org'
|
40
|
+
|
41
|
+
gem 'rails', '3.2.8'
|
42
|
+
gem 'unicorn'
|
43
|
+
gem 'devise', :git => "git://github.com/jinzhu/devise.git", :ref => "b94ee9da98b16e4c8fbdc91af8605669d01b17e6"
|
44
|
+
|
45
|
+
group :development do
|
46
|
+
gem 'pry-rails'
|
47
|
+
end
|
48
|
+
|
49
|
+
group :test do
|
50
|
+
gem 'rspec'
|
51
|
+
end
|
52
|
+
|
53
|
+
4, Querying config (Please checkout the source code and play with examples under the /example directory for more details)
|
54
|
+
|
55
|
+
# Find by type
|
56
|
+
Gemfile.find(:gem)
|
57
|
+
|
58
|
+
# Find by type and name also chain query
|
59
|
+
Gemfile.find(:group, 'development').find(:gem)
|
60
|
+
|
61
|
+
# Get all gems
|
62
|
+
Gemfile.deep_find(:gem)
|
63
|
+
|
64
|
+
# Find by block
|
65
|
+
Gemfile.deep_find(:gem) do |n|
|
66
|
+
# Find all gems used in development environment
|
67
|
+
parent = n.parent
|
68
|
+
parent.root? || ((parent.config_name == :group) && parent.name == :development)
|
69
|
+
end
|
70
|
+
|
71
|
+
# Qor DSL is designed to be as flexible as possible while helping you to create your DSLs.
|
72
|
+
# Please check source code for all possibilities!
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
|
76
|
+
1. Fork it
|
77
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
78
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
79
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
80
|
+
5. Create new Pull Request
|
data/example/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
|
3
|
+
gem 'rails', '3.2.8'
|
4
|
+
gem 'unicorn'
|
5
|
+
gem 'devise', :git => "git://github.com/jinzhu/devise.git", :ref => "b94ee9da98b16e4c8fbdc91af8605669d01b17e6"
|
6
|
+
|
7
|
+
group :development do
|
8
|
+
gem 'pry-rails'
|
9
|
+
end
|
10
|
+
|
11
|
+
group :test do
|
12
|
+
gem 'rspec'
|
13
|
+
end
|
data/example/gemfile.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'qor_dsl'
|
4
|
+
|
5
|
+
class Gemfile
|
6
|
+
include Qor::Dsl
|
7
|
+
default_configs [ENV['BUNDLE_GEMFILE'], File.join(File.dirname(__FILE__), 'Gemfile')]
|
8
|
+
|
9
|
+
node :source
|
10
|
+
node :gem
|
11
|
+
|
12
|
+
node :group do
|
13
|
+
node :gem
|
14
|
+
end
|
15
|
+
|
16
|
+
node :example do
|
17
|
+
node :gem, :inherit => true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Methods for query
|
22
|
+
Gemfile.first(:gem)
|
23
|
+
Gemfile.first(:gem, 'unicorn')
|
24
|
+
|
25
|
+
Gemfile.find(:gem)
|
26
|
+
Gemfile.find(:gem, 'rails')
|
27
|
+
Gemfile.find(:gem) do |n|
|
28
|
+
!!n.options[:git].nil?
|
29
|
+
end
|
30
|
+
|
31
|
+
Gemfile.find(:group, 'development').find(:gem)
|
32
|
+
|
33
|
+
Gemfile.deep_find(:gem)
|
34
|
+
Gemfile.deep_find(:gem) do |n|
|
35
|
+
# Find all gems used in development environment
|
36
|
+
parent = n.parent
|
37
|
+
parent.root? || ((parent.config_name == :group) && parent.name == :development)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Methods for node
|
41
|
+
node = Gemfile.find(:gem, 'devise')
|
42
|
+
node.name
|
43
|
+
node.value
|
44
|
+
node.options
|
45
|
+
node.data
|
46
|
+
node.config
|
47
|
+
node.parent
|
48
|
+
node.parents
|
49
|
+
node.root
|
50
|
+
node.root?
|
data/lib/qor_dsl/class_method.rb
CHANGED
@@ -19,7 +19,7 @@ module Qor
|
|
19
19
|
|
20
20
|
def default_config
|
21
21
|
if @default_configs.is_a?(Array)
|
22
|
-
@default_configs.select {|x| File.exist?(x) }[0]
|
22
|
+
@default_configs.select {|x| File.exist?(x.to_s) }[0]
|
23
23
|
else
|
24
24
|
@default_configs
|
25
25
|
end
|
@@ -44,6 +44,10 @@ module Qor
|
|
44
44
|
root.find(*arguments, &block)
|
45
45
|
end
|
46
46
|
|
47
|
+
def deep_find(*arguments, &block)
|
48
|
+
root.deep_find(*arguments, &block)
|
49
|
+
end
|
50
|
+
|
47
51
|
def first(*arguments, &block)
|
48
52
|
root.first(*arguments, &block)
|
49
53
|
end
|
data/lib/qor_dsl/config.rb
CHANGED
@@ -32,13 +32,28 @@ module Qor
|
|
32
32
|
node.add_config(config)
|
33
33
|
node.data = *data
|
34
34
|
node.block = blk
|
35
|
-
node.config.instance_eval(&blk) if block_given? && (config.__children.size > 0)
|
36
35
|
__node.add_child(node)
|
36
|
+
node.config.instance_eval(&blk) if block_given? && (config.__children.size > 0)
|
37
37
|
end
|
38
38
|
DOC
|
39
39
|
|
40
40
|
self.instance_eval method_defination
|
41
41
|
end
|
42
|
+
|
43
|
+
def to_s
|
44
|
+
result = [
|
45
|
+
['name', __name],
|
46
|
+
['parent', __parent && __parent.__name],
|
47
|
+
['__children', __children.keys],
|
48
|
+
['options', __options],
|
49
|
+
['block', __block]
|
50
|
+
].inject({}) do |s, value|
|
51
|
+
s[value[0]] = value[1] if value[1] && value[1].to_s.length > 0
|
52
|
+
s
|
53
|
+
end.inspect
|
54
|
+
|
55
|
+
"#<Qor::Dsl::Config::0x#{object_id.to_s(16)} #{result}>"
|
56
|
+
end
|
42
57
|
end
|
43
58
|
end
|
44
59
|
end
|
data/lib/qor_dsl/node.rb
CHANGED
@@ -1,15 +1,31 @@
|
|
1
1
|
module Qor
|
2
2
|
module Dsl
|
3
3
|
class Node
|
4
|
-
attr_accessor :name, :config, :parent, :children, :data, :options, :block
|
4
|
+
attr_accessor :name, :config, :parent, :children, :data, :options, :block, :all_nodes
|
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
9
|
end
|
10
10
|
|
11
|
+
def config_name
|
12
|
+
config.__name
|
13
|
+
end
|
14
|
+
|
11
15
|
def inspect_name
|
12
|
-
"{#{
|
16
|
+
"{#{config_name}: #{name || 'nil'}}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def root?
|
20
|
+
root == self
|
21
|
+
end
|
22
|
+
|
23
|
+
def root
|
24
|
+
parent ? parent.root : self
|
25
|
+
end
|
26
|
+
|
27
|
+
def parents
|
28
|
+
parent ? [parent, parent.parents].flatten : []
|
13
29
|
end
|
14
30
|
|
15
31
|
def options
|
@@ -39,17 +55,25 @@ module Qor
|
|
39
55
|
end
|
40
56
|
|
41
57
|
def config_options_for_child(type)
|
42
|
-
config.__children[type].__options || {}
|
58
|
+
config.__children[type].__options || {} rescue {}
|
43
59
|
end
|
44
60
|
|
45
61
|
def add_child(child)
|
46
62
|
child.parent = self
|
47
63
|
children << child
|
64
|
+
root.all_nodes ||= []
|
65
|
+
root.all_nodes << child
|
66
|
+
end
|
67
|
+
|
68
|
+
def deep_find(type=nil, name=nil, &block)
|
69
|
+
nodes = root.all_nodes
|
70
|
+
nodes = nodes.select {|n| n.parents.include?(self) } unless root?
|
71
|
+
find(type, name, nodes, &block)
|
48
72
|
end
|
49
73
|
|
50
|
-
def find(type=nil, name=nil, &block)
|
51
|
-
selected_children =
|
52
|
-
(type.nil? ? true : (child.
|
74
|
+
def find(type=nil, name=nil, nodes=children, &block)
|
75
|
+
selected_children = nodes.select do |child|
|
76
|
+
(type.nil? ? true : (child.config_name.to_s == type.to_s)) &&
|
53
77
|
(name.nil? ? true : (child.name.to_s == name.to_s)) &&
|
54
78
|
(block.nil? ? true : block.call(child))
|
55
79
|
end
|
@@ -64,11 +88,11 @@ module Qor
|
|
64
88
|
selected_children.is_a?(Array) ? selected_children[0] : selected_children
|
65
89
|
end
|
66
90
|
|
67
|
-
def
|
91
|
+
def to_s
|
68
92
|
result = [
|
69
93
|
['name', name],
|
70
94
|
['parent', parent && parent.inspect_name],
|
71
|
-
['config',
|
95
|
+
['config', config_name],
|
72
96
|
['children', children.map(&:inspect_name)],
|
73
97
|
['data', data],
|
74
98
|
['block', block]
|
data/qor_dsl.gemspec
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
Gem::Specification.new do |gem|
|
3
3
|
gem.authors = ["Jinzhu"]
|
4
4
|
gem.email = ["wosmvp@gmail.com"]
|
5
|
-
gem.
|
6
|
-
gem.
|
7
|
-
gem.homepage = ""
|
5
|
+
gem.summary = %q{DSL made easy!}
|
6
|
+
gem.description = %q{Qor DSL is designed to be as flexible as possible while helping you to create your DSLs.}
|
7
|
+
gem.homepage = "https://github.com/qor/qor_dsl"
|
8
8
|
|
9
9
|
gem.files = `git ls-files`.split($\)
|
10
10
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
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.0
|
14
|
+
gem.version = "0.1.0"
|
15
15
|
end
|
data/test/layout.rb
CHANGED
data/test/layout_test.rb
CHANGED
@@ -10,15 +10,24 @@ describe Layout do
|
|
10
10
|
it "layout config testing" do
|
11
11
|
# Find by type
|
12
12
|
Layout::Configuration.find(:gadget).length.must_equal 2
|
13
|
+
Layout::Configuration.find(:template).length.must_equal 2
|
14
|
+
Layout::Configuration.deep_find(:template).length.must_equal 3
|
15
|
+
# :settings, :meta, :meta, :meta, :meta, :context, :template
|
16
|
+
Layout::Configuration.find(:gadget, :quick_buy).deep_find().length.must_equal 7
|
13
17
|
|
14
18
|
# Find by name
|
15
19
|
Layout::Configuration.find(:gadget, 'quick_buy').name.must_equal :quick_buy
|
20
|
+
Layout::Configuration.deep_find(:desc, "From Google").value.must_equal "From Google"
|
16
21
|
|
17
22
|
# Find by block
|
18
23
|
Layout::Configuration.first(:template) do |n|
|
19
24
|
n.options[:since] > "12:50"
|
20
25
|
end.value.must_equal 'Hello World2'
|
21
26
|
|
27
|
+
Layout::Configuration.deep_find(:desc) do |n|
|
28
|
+
n.parents.include?(Layout::Configuration.find(:action, :google))
|
29
|
+
end[0].value.must_equal 'From Google'
|
30
|
+
|
22
31
|
# Inherit
|
23
32
|
Layout::Configuration.find(:gadget, :product_link).find(:template)[0].value.must_equal "Hello World"
|
24
33
|
|
@@ -28,6 +37,9 @@ describe Layout do
|
|
28
37
|
# Options
|
29
38
|
Layout::Configuration.find(:template)[1].options.must_equal({:since => "13:00", :to => "18:00"})
|
30
39
|
|
40
|
+
# Parents
|
41
|
+
Layout::Configuration.find(:gadget, :quick_buy).first(:template).parents.count.must_equal 2
|
42
|
+
|
31
43
|
# More is coming... (multi, alias_node)
|
32
44
|
end
|
33
45
|
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.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,9 +9,10 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-20 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
|
-
description: Qor DSL
|
14
|
+
description: Qor DSL is designed to be as flexible as possible while helping you to
|
15
|
+
create your DSLs.
|
15
16
|
email:
|
16
17
|
- wosmvp@gmail.com
|
17
18
|
executables: []
|
@@ -23,6 +24,8 @@ files:
|
|
23
24
|
- Gemfile.lock
|
24
25
|
- README.md
|
25
26
|
- Rakefile
|
27
|
+
- example/Gemfile
|
28
|
+
- example/gemfile.rb
|
26
29
|
- lib/qor_dsl.rb
|
27
30
|
- lib/qor_dsl/class_method.rb
|
28
31
|
- lib/qor_dsl/config.rb
|
@@ -31,7 +34,7 @@ files:
|
|
31
34
|
- test/configure.rb
|
32
35
|
- test/layout.rb
|
33
36
|
- test/layout_test.rb
|
34
|
-
homepage:
|
37
|
+
homepage: https://github.com/qor/qor_dsl
|
35
38
|
licenses: []
|
36
39
|
post_install_message:
|
37
40
|
rdoc_options: []
|
@@ -54,7 +57,7 @@ rubyforge_project:
|
|
54
57
|
rubygems_version: 1.8.24
|
55
58
|
signing_key:
|
56
59
|
specification_version: 3
|
57
|
-
summary:
|
60
|
+
summary: DSL made easy!
|
58
61
|
test_files:
|
59
62
|
- test/configure.rb
|
60
63
|
- test/layout.rb
|