dslh 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ed45c1e092e5109296acdff1cbdf5f04480d54ea
4
- data.tar.gz: c01f03de3c202a12f34311ebd38355976547849f
3
+ metadata.gz: 21ed893265981c141c3c0d34d7c2b1f80fa72ac1
4
+ data.tar.gz: 41e7a5be1a5d6cc4cf0840c9aef97b1bef0ba0b8
5
5
  SHA512:
6
- metadata.gz: 7a4334b6e9a8a5893090b0c94e1cb0bd2f96ada901d6bfa4781b65b1f8a50b013b614516a3c63043f7639e140444987b17f4f1db98abadc229a17807f2ba8e14
7
- data.tar.gz: 12f0f82b30ccbd566b568d4cec226273d6ed96e2e6cc0ee7c4c90b18956e9b7b3d9cf2ab4eb8f97f46c37e9d256b6e430bc717200e0172424effa5fe497f50c0
6
+ metadata.gz: c73b4951338446b91966eeb8c0228b72d06dd6d9582acea5abf550d01d648e2b1660ddc1a979686b67bd6fee90bfc32eea0f4a0a472bf679790f89741fb2b233
7
+ data.tar.gz: 03b3a3a15244f67d8ef0ed5eb74c00af859185325749fe1f1f0ede1bf91030e7566d135d940a92e478bd9a1d0234202ccf9b887b4684b12bfc8cc2fed4ff028e
data/README.md CHANGED
@@ -226,7 +226,6 @@ mapping:
226
226
  required: yes
227
227
  "email":
228
228
  type: str
229
- EOS
230
229
  EOS
231
230
 
232
231
  begin
data/lib/dslh/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Dslh
2
- VERSION = '0.4.0'
2
+ VERSION = '0.4.1'
3
3
  end
data/lib/dslh.rb CHANGED
@@ -27,11 +27,12 @@ class Dslh
27
27
  :initial_depth,
28
28
  :key_conv,
29
29
  :lineno,
30
+ :root_identify,
31
+ :schema,
30
32
  :scope_hook,
31
33
  :scope_vars,
32
34
  :time_inspecter,
33
35
  :use_braces_instead_of_do_end,
34
- :schema,
35
36
  :value_conv,
36
37
  ]
37
38
 
@@ -146,13 +147,13 @@ class Dslh
146
147
  def deval(hash)
147
148
  buf = StringIO.new
148
149
  depth = @options[:initial_depth] || 0
149
- deval0(hash, depth, buf)
150
+ deval0(hash, depth, buf, true)
150
151
  buf.string
151
152
  end
152
153
 
153
154
  private
154
155
 
155
- def deval0(hash, depth, buf)
156
+ def deval0(hash, depth, buf, root = false)
156
157
  indent = (INDENT_SPACES * depth)
157
158
  key_conv = @options[:key_conv]
158
159
  value_conv = @options[:value_conv]
@@ -180,8 +181,15 @@ class Dslh
180
181
 
181
182
  buf.puts(indent + key_value)
182
183
  else
183
- buf.print(indent + key)
184
- value_proc(value, depth, buf, true, key)
184
+ if root and @options[:root_identify] and value.kind_of?(Hash)
185
+ value.each do |k, v|
186
+ buf.print(indent + key + ' ' + k.inspect)
187
+ value_proc(v, depth, buf, true, key)
188
+ end
189
+ else
190
+ buf.print(indent + key)
191
+ value_proc(value, depth, buf, true, key)
192
+ end
185
193
  end
186
194
  end
187
195
  end
@@ -330,9 +338,12 @@ class Dslh
330
338
  value_conv = @__options__[:value_conv]
331
339
  nested_hash = block ? ScopeBlock.nest(binding, 'block', method_name) : nil
332
340
  method_name = key_conv.call(method_name) if key_conv
341
+ exist_value = @__hash__[method_name]
333
342
 
334
- if not @__options__[:allow_duplicate] and @__hash__.has_key?(method_name) and not (block and block.arity == -1)
335
- raise "duplicate key #{method_name.inspect}"
343
+ if not @__options__[:allow_duplicate] and exist_value and not (block and block.arity == -1)
344
+ if args.length != 1 or not nested_hash or not exist_value.kind_of?(Hash)
345
+ raise "duplicate key #{method_name.inspect}"
346
+ end
336
347
  end
337
348
 
338
349
  push_to_hash = proc do |v|
@@ -350,7 +361,9 @@ class Dslh
350
361
  args = args.map {|i| value_conv.call(i) } if value_conv
351
362
  value = args.length > 1 ? args : args[0]
352
363
 
353
- if nested_hash
364
+ if args.length == 1 and exist_value and nested_hash
365
+ exist_value[value] = nested_hash
366
+ elsif nested_hash
354
367
  push_to_hash.call(value => nested_hash)
355
368
  else
356
369
  push_to_hash.call(value)
data/spec/dslh_spec.rb CHANGED
@@ -3025,4 +3025,67 @@ mapping:
3025
3025
  end
3026
3026
  end
3027
3027
  end
3028
+
3029
+ context 'idetify using root key' do
3030
+ it 'eval' do
3031
+ h = Dslh.eval do
3032
+ employees "foo" do
3033
+ code 101
3034
+ email "foo@winebarrel.com"
3035
+ end
3036
+ employees "bar" do
3037
+ code 102
3038
+ email "bar@winebarrel.com"
3039
+ end
3040
+ employees2 "foo2" do
3041
+ code 201
3042
+ email "foo@winebarrel.com"
3043
+ end
3044
+ employees2 "bar2" do
3045
+ code 202
3046
+ email "bar@winebarrel.com"
3047
+ end
3048
+ end
3049
+
3050
+ expect(h).to eq(
3051
+ {"employees"=>
3052
+ {"foo"=>{"code"=>101, "email"=>"foo@winebarrel.com"},
3053
+ "bar"=>{"code"=>102, "email"=>"bar@winebarrel.com"}},
3054
+ "employees2"=>
3055
+ {"foo2"=>{"code"=>201, "email"=>"foo@winebarrel.com"},
3056
+ "bar2"=>{"code"=>202, "email"=>"bar@winebarrel.com"}}}
3057
+ )
3058
+ end
3059
+
3060
+ it 'deval' do
3061
+ h = {"employees"=>
3062
+ {"foo"=>{"code"=>101, "email"=>"foo@winebarrel.com"},
3063
+ "bar"=>{"code"=>102, "email"=>"bar@winebarrel.com"}},
3064
+ "employees2"=>
3065
+ {"foo2"=>{"code"=>201, "email"=>"foo@winebarrel.com"},
3066
+ "bar2"=>{"code"=>202, "email"=>"bar@winebarrel.com"}}}
3067
+
3068
+
3069
+ dsl = Dslh.deval(h, :root_identify => true)
3070
+
3071
+ expect(dsl).to eq <<-EOS
3072
+ employees "foo" do
3073
+ code 101
3074
+ email "foo@winebarrel.com"
3075
+ end
3076
+ employees "bar" do
3077
+ code 102
3078
+ email "bar@winebarrel.com"
3079
+ end
3080
+ employees2 "foo2" do
3081
+ code 201
3082
+ email "foo@winebarrel.com"
3083
+ end
3084
+ employees2 "bar2" do
3085
+ code 202
3086
+ email "bar@winebarrel.com"
3087
+ end
3088
+ EOS
3089
+ end
3090
+ end
3028
3091
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dslh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Sugawara
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-30 00:00:00.000000000 Z
11
+ date: 2016-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  version: '0'
108
108
  requirements: []
109
109
  rubyforge_project:
110
- rubygems_version: 2.5.2
110
+ rubygems_version: 2.5.1
111
111
  signing_key:
112
112
  specification_version: 4
113
113
  summary: It define Hash as a DSL.