dslh 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NzE1YzAxOTU1N2I1NTE1NGZmMjhlZGZiZWViM2QzNTMzZGNiNWZkNg==
5
+ data.tar.gz: !binary |-
6
+ ODdlYWE3NGZlZjQ3NDVkYjBjYzQyZGFjMzdiMGYwMDdmMGJhZDY0OQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZjUzMzgwNzAxZGVhNzIxYjEyOWI4ZmUwNzdmZmEyOWMyMWE0ZmU3OGQxMzI4
10
+ Y2EzMGMxMjc5MmQwOWFjNTdmZGZkM2ZmMzZkMmRkNmQxNTI3NDkzNzRiOTY5
11
+ YjRiNDJhNGI0ODdhNjc1OGNjNGYwNjlhODExMjg2ODBkZTFiMmI=
12
+ data.tar.gz: !binary |-
13
+ Y2I2NTcxMTdkZmY3NmMyM2RiMDViM2YzNDFhYTM5YTIyYmNjMzczMTY4M2Ew
14
+ NzVlN2M2Njk3OWYzYmJhZDg2N2ZjMWQ3NDlkN2U5MDc2MGQzMWIxZWNmNzVi
15
+ YWM1YjZlNzRlMTcxYzY1MzFmNDEwMjYyYmEzNmIxYWM4NDQxZTc=
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require spec_helper
2
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dslh.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Genki Sugawara
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,80 @@
1
+ # Dslh
2
+
3
+ It define Hash as a DSL.
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/dslh.png)](http://badge.fury.io/rb/dslh)
6
+ [![Build Status](https://drone.io/bitbucket.org/winebarrel/dslh/status.png)](https://drone.io/bitbucket.org/winebarrel/dslh/latest)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'dslh'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install dslh
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ require 'dslh'
26
+ require 'pp'
27
+
28
+ h = Dslh.eval do
29
+ glossary do
30
+ title "example glossary"
31
+ GlossDiv do
32
+ title "S"
33
+ GlossList do
34
+ GlossEntry do
35
+ ID "SGML"
36
+ SortAs "SGML"
37
+ GlossTerm "Standard Generalized Markup Language"
38
+ Acronym "SGML"
39
+ Abbrev "ISO 8879:1986"
40
+ GlossDef do
41
+ para "A meta-markup language, used to create markup languages such as DocBook."
42
+ GlossSeeAlso "GML", "XML"
43
+ end
44
+ GlossSee "markup"
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ pp h
52
+ ```
53
+
54
+ ```ruby
55
+ # h =>
56
+ {:glossary=>
57
+ {:title=>"example glossary",
58
+ :GlossDiv=>
59
+ {:title=>"S",
60
+ :GlossList=>
61
+ {:GlossEntry=>
62
+ {:ID=>"SGML",
63
+ :SortAs=>"SGML",
64
+ :GlossTerm=>"Standard Generalized Markup Language",
65
+ :Acronym=>"SGML",
66
+ :Abbrev=>"ISO 8879:1986",
67
+ :GlossDef=>
68
+ {:para=>
69
+ "A meta-markup language, used to create markup languages such as DocBook.",
70
+ :GlossSeeAlso=>["GML", "XML"]},
71
+ :GlossSee=>"markup"}}}}}
72
+ ```
73
+
74
+ ## Contributing
75
+
76
+ 1. Fork it ( http://github.com/<my-github-username>/dslh/fork )
77
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
78
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
79
+ 4. Push to the branch (`git push origin my-new-feature`)
80
+ 5. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dslh/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'dslh'
8
+ spec.version = Dslh::VERSION
9
+ spec.authors = ['Genki Sugawara']
10
+ spec.email = ['sugawara@cookpad.com']
11
+ spec.summary = %q{It define Hash as a DSL.}
12
+ spec.description = %q{It define Hash as a DSL.}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec', '>= 2.11.0'
24
+ end
@@ -0,0 +1,40 @@
1
+ require 'dslh/version'
2
+
3
+ class Dslh
4
+ def self.eval(options = {}, &block)
5
+ self.new(options).eval(&block)
6
+ end
7
+
8
+ def initialize(options = {})
9
+ @options = options
10
+ @hash = {}
11
+ end
12
+
13
+ def eval(&block)
14
+ self.instance_eval(&block)
15
+ return @hash
16
+ end
17
+
18
+ def method_missing(method_name, *args, &block)
19
+ key_conv = @options[:key_conv] || @options[:conv]
20
+ value_conv = @options[:value_conv] || @options[:conv]
21
+
22
+ nested_hash = block ? self.class.eval(@options, &block) : nil
23
+ method_name = key_conv.call(method_name) if key_conv
24
+
25
+ if args.empty?
26
+ @hash[method_name] = nested_hash
27
+ else
28
+ value = args.length > 1 ? args : args[0]
29
+ value = value_conv.call(value) if value_conv
30
+
31
+ if nested_hash
32
+ @hash[method_name] = {
33
+ value => nested_hash
34
+ }
35
+ else
36
+ @hash[method_name] = value
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ class Dslh
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,197 @@
1
+ describe Dslh do
2
+ it 'should be empty hash' do
3
+ h = Dslh.eval {}
4
+ expect(h).to eq({})
5
+ end
6
+
7
+ it 'should be hash' do
8
+ h = Dslh.eval do
9
+ key1 'value'
10
+ key2 100
11
+ end
12
+
13
+ expect(h).to eq({
14
+ :key1 => 'value',
15
+ :key2 => 100,
16
+ })
17
+ end
18
+
19
+ it 'should be nested hash' do
20
+ h = Dslh.eval do
21
+ key1 'value'
22
+ key2 100
23
+
24
+ key3 do
25
+ key31 "value31" do
26
+ key311 100
27
+ key312 '200'
28
+ end
29
+
30
+ key32 do
31
+ key321 "value321" do
32
+ key3211 'XXX'
33
+ key3212 :XXX
34
+ end
35
+ key322 300
36
+ end
37
+ end
38
+ end
39
+
40
+ expect(h).to eq(
41
+ {:key1=>"value",
42
+ :key2=>100,
43
+ :key3=>
44
+ {:key31=>{"value31"=>{:key311=>100, :key312=>"200"}},
45
+ :key32=>
46
+ {:key321=>{"value321"=>{:key3211=>"XXX", :key3212=>:XXX}}, :key322=>300}}}
47
+ )
48
+ end
49
+
50
+ it 'can pass hash argument' do
51
+ h = Dslh.eval do
52
+ key1 'value'
53
+ key2 100
54
+
55
+ key3(
56
+ 100 => 200,
57
+ 'XXX' => :XXX
58
+ )
59
+
60
+ key4 do
61
+ key41(
62
+ '300' => '400',
63
+ :FOO => :BAR
64
+ )
65
+ key42 100
66
+ end
67
+ end
68
+
69
+ expect(h).to eq(
70
+ {:key1=>"value",
71
+ :key2=>100,
72
+ :key3=>{100=>200, "XXX"=>:XXX},
73
+ :key4=>{:key41=>{"300"=>"400", :FOO=>:BAR}, :key42=>100}}
74
+ )
75
+ end
76
+
77
+ it 'should convert hash key/value' do
78
+ h = Dslh.eval :conv => proc {|i| i.to_s } do
79
+ key1 'value'
80
+ key2 100
81
+
82
+ key3 do
83
+ key31 "value31" do
84
+ key311 100
85
+ key312 '200'
86
+ end
87
+
88
+ key32 do
89
+ key321 "value321" do
90
+ key3211 'XXX'
91
+ key3212 :XXX
92
+ end
93
+ key322 300
94
+ end
95
+ end
96
+ end
97
+
98
+ expect(h).to eq(
99
+ {"key1"=>"value",
100
+ "key2"=>"100",
101
+ "key3"=>
102
+ {"key31"=>{"value31"=>{"key311"=>"100", "key312"=>"200"}},
103
+ "key32"=>
104
+ {"key321"=>{"value321"=>{"key3211"=>"XXX", "key3212"=>"XXX"}},
105
+ "key322"=>"300"}}}
106
+ )
107
+ end
108
+
109
+ it 'should convert hash key' do
110
+ h = Dslh.eval :key_conv => proc {|i| i.to_s } do
111
+ key1 'value'
112
+ key2 100
113
+
114
+ key3 do
115
+ key31 "value31" do
116
+ key311 100
117
+ key312 '200'
118
+ end
119
+
120
+ key32 do
121
+ key321 "value321" do
122
+ key3211 'XXX'
123
+ key3212 :XXX
124
+ end
125
+ key322 300
126
+ end
127
+ end
128
+ end
129
+
130
+ expect(h).to eq(
131
+ {"key1"=>"value",
132
+ "key2"=>100,
133
+ "key3"=>
134
+ {"key31"=>{"value31"=>{"key311"=>100, "key312"=>"200"}},
135
+ "key32"=>
136
+ {"key321"=>{"value321"=>{"key3211"=>"XXX", "key3212"=>:XXX}},
137
+ "key322"=>300}}}
138
+ )
139
+ end
140
+
141
+ it 'should convert hash value' do
142
+ h = Dslh.eval :value_conv => proc {|i| i.to_s } do
143
+ key1 'value'
144
+ key2 100
145
+
146
+ key3 do
147
+ key31 "value31" do
148
+ key311 100
149
+ key312 '200'
150
+ end
151
+
152
+ key32 do
153
+ key321 "value321" do
154
+ key3211 'XXX'
155
+ key3212 :XXX
156
+ end
157
+ key322 300
158
+ end
159
+ end
160
+ end
161
+
162
+ expect(h).to eq(
163
+ {:key1=>"value",
164
+ :key2=>"100",
165
+ :key3=>
166
+ {:key31=>{"value31"=>{:key311=>"100", :key312=>"200"}},
167
+ :key32=>
168
+ {:key321=>{"value321"=>{:key3211=>"XXX", :key3212=>"XXX"}},
169
+ :key322=>"300"}}}
170
+ )
171
+ end
172
+
173
+ it 'can pass multiple argument' do
174
+ h = Dslh.eval do
175
+ key1 'value', 'value2'
176
+ key2 100, 200
177
+
178
+ key3 do
179
+ key31 :FOO, :BAR
180
+ key32 'ZOO', 'BAZ'
181
+ end
182
+
183
+ key4 'value4', 'value42' do
184
+ key41 100
185
+ key42 '200'
186
+ end
187
+ end
188
+
189
+ expect(h).to eq(
190
+ {:key1=>["value", "value2"],
191
+ :key2=>[100, 200],
192
+ :key3=>{:key31=>[:FOO, :BAR], :key32=>["ZOO", "BAZ"]},
193
+ :key4=>{["value4", "value42"]=>{:key41=>100, :key42=>"200"}}}
194
+ )
195
+ end
196
+
197
+ end
@@ -0,0 +1 @@
1
+ require 'dslh'
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dslh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Genki Sugawara
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 2.11.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 2.11.0
55
+ description: It define Hash as a DSL.
56
+ email:
57
+ - sugawara@cookpad.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - dslh.gemspec
69
+ - lib/dslh.rb
70
+ - lib/dslh/version.rb
71
+ - spec/dslh_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: ''
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.1.11
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: It define Hash as a DSL.
97
+ test_files:
98
+ - spec/dslh_spec.rb
99
+ - spec/spec_helper.rb