dslh 0.1.5 → 0.1.6

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.
Files changed (5) hide show
  1. checksums.yaml +5 -13
  2. data/lib/dslh/version.rb +1 -1
  3. data/lib/dslh.rb +66 -18
  4. data/spec/dslh_spec.rb +172 -0
  5. metadata +11 -10
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- Zjg0MDVhNGE3MTM3YzdkZmE2ZjhhZDFiZWNkYzQzNDdlMzEzMzEzZg==
5
- data.tar.gz: !binary |-
6
- ODU1OWU1MmFiMDdjMjg3Mzc1OWY2MWRjMTE3NzE2Y2IwMzZhNTdlOQ==
2
+ SHA1:
3
+ metadata.gz: 448c7a38e72cbc66a338013c5b594d15501675b5
4
+ data.tar.gz: 500c4decaa985b8682c123c8b2d58f0e82566d6a
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- Nzg3MTEzMjU3NzI1MGEyOGE1YTk3MWRmZTY1NDYzMGQ0NjAwNDQwNGM0OTc1
10
- N2E5ZjViOTc1YzUyMmM5ZGNlZmQ1ODkyMGNhNTlmM2IyNTU3NTA3OTJmMzU4
11
- NjM4ZjRlYjU4ZDMwOTVlMDY0NTYyNTllYWVmMTNlN2I0OTFiMDI=
12
- data.tar.gz: !binary |-
13
- MWUxNzFmMjk3Nzg2ZTcwYzZmZjljY2U1Yjk1OWRkZTJiMjk5NjhmZGU4YWE1
14
- NjFlMDk2YmE4NDA5OTNkYWNhOWE5NDIyZDRkNzBjYzMwZTMyMmYzNjJhMDAy
15
- NDNkMjIxNTg2YWQyNDUyNDNhOGU3NzBmMTNlMzExZTY3OGUzOWE=
6
+ metadata.gz: 61bdc03040f883968193fc63a7fbb4c9d7d1e9f6b1d26af00478b65266d659ab382687488940cf0d870f62d7da31323c499d704c35a9d6f0d5510d9efcc18c4e
7
+ data.tar.gz: 1137178d9f9b748198787b5747b63745cc09dba6baeee17836e56bb0f087dfa927e63d1894084cdcb57c8905d2ef2bbfa5f0aabde88ae7dbceeec9583af13f16
data/lib/dslh/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Dslh
2
- VERSION = '0.1.5'
2
+ VERSION = '0.1.6'
3
3
  end
data/lib/dslh.rb CHANGED
@@ -1,27 +1,40 @@
1
1
  require 'dslh/version'
2
+ require 'stringio'
2
3
 
3
4
  class Dslh
4
- def self.eval(expr_or_options = nil, options = nil, &block)
5
- if options and not options.kind_of?(Hash)
6
- raise TypeError, "wrong argument type #{options.class} (expected Hash)"
7
- end
5
+ INDENT_SPACES = ' '
8
6
 
9
- expr = nil
10
- options ||= {}
7
+ class << self
8
+ def eval(expr_or_options = nil, options = nil, &block)
9
+ if options and not options.kind_of?(Hash)
10
+ raise TypeError, "wrong argument type #{options.class} (expected Hash)"
11
+ end
11
12
 
12
- if expr_or_options
13
- case expr_or_options
14
- when String
15
- expr = expr_or_options
16
- when Hash
17
- options.update(expr_or_options)
18
- else
19
- raise TypeError, "wrong argument type #{expr_or_options.class} (expected String or Hash)"
13
+ expr = nil
14
+ options ||= {}
15
+
16
+ if expr_or_options
17
+ case expr_or_options
18
+ when String
19
+ expr = expr_or_options
20
+ when Hash
21
+ options.update(expr_or_options)
22
+ else
23
+ raise TypeError, "wrong argument type #{expr_or_options.class} (expected String or Hash)"
24
+ end
20
25
  end
26
+
27
+ self.new(options).eval(expr, &block)
21
28
  end
22
29
 
23
- self.new(options).eval(expr, &block)
24
- end
30
+ def deval(hash, options = {}, &block)
31
+ if [hash, options].all? {|i| not i.kind_of?(Hash) }
32
+ raise TypeError, "wrong argument type #{options.class} (expected Hash)"
33
+ end
34
+
35
+ self.new(options).deval(hash, &block)
36
+ end
37
+ end # of class methods
25
38
 
26
39
  def initialize(options = {})
27
40
  @options = options.dup
@@ -49,6 +62,41 @@ class Dslh
49
62
  return retval
50
63
  end
51
64
 
65
+ def deval(hash)
66
+ buf = StringIO.new
67
+ deval0(hash, 0, buf)
68
+ buf.string
69
+ end
70
+
71
+ private
72
+
73
+ def deval0(hash, depth, buf)
74
+ hash.each do |key, value|
75
+ key_conv = @options[:key_conv] || @options[:conv]
76
+ value_conv = @options[:value_conv] || @options[:conv]
77
+
78
+ key = key_conv.call(key) if key_conv
79
+ indent = (INDENT_SPACES * depth)
80
+
81
+ buf.print(indent + key)
82
+
83
+ case value
84
+ when Hash
85
+ buf.puts(' do')
86
+ deval0(value, depth + 1, buf)
87
+ buf.puts(indent + 'end')
88
+ when Array
89
+ buf.puts ' ' + value.map {|v|
90
+ v = value_conv.call(v) if value_conv
91
+ v.inspect
92
+ }.join(', ')
93
+ else
94
+ value = value_conv.call(value) if value_conv
95
+ buf.puts ' ' + value.inspect
96
+ end
97
+ end
98
+ end
99
+
52
100
  class Scope
53
101
  def method_missing(method_name, *args, &block)
54
102
  key_conv = @__options__[:key_conv] || @__options__[:conv]
@@ -82,6 +130,6 @@ class Dslh
82
130
 
83
131
  return @__hash__
84
132
  end
85
- end # of Scope
86
- end
133
+ end
134
+ end # of Scope
87
135
  end
data/spec/dslh_spec.rb CHANGED
@@ -323,4 +323,176 @@ describe Dslh do
323
323
 
324
324
  expect(h).to eq({:key1=>123, :key2=>{:key21=>123, :key22=>{:key221=>123}}})
325
325
  end
326
+
327
+ it 'should convert hash to dsl' do
328
+ h = {"glossary"=>
329
+ {"title"=>"example glossary",
330
+ "GlossDiv"=>
331
+ {"title"=>"S",
332
+ "GlossList"=>
333
+ {"GlossEntry"=>
334
+ {"ID"=>"SGML",
335
+ "SortAs"=>"SGML",
336
+ "GlossTerm"=>"Standard Generalized Markup Language",
337
+ "Acronym"=>"SGML",
338
+ "Abbrev"=>"ISO 8879:1986",
339
+ "GlossDef"=>
340
+ {"para"=>
341
+ "A meta-markup language, used to create markup languages such as DocBook.",
342
+ "GlossSeeAlso"=>["GML", "XML"]},
343
+ "GlossSee"=>"markup"}}}}}
344
+
345
+ dsl = Dslh.deval(h)
346
+ expect(dsl).to eq(<<-EOS)
347
+ glossary do
348
+ title "example glossary"
349
+ GlossDiv do
350
+ title "S"
351
+ GlossList do
352
+ GlossEntry do
353
+ ID "SGML"
354
+ SortAs "SGML"
355
+ GlossTerm "Standard Generalized Markup Language"
356
+ Acronym "SGML"
357
+ Abbrev "ISO 8879:1986"
358
+ GlossDef do
359
+ para "A meta-markup language, used to create markup languages such as DocBook."
360
+ GlossSeeAlso "GML", "XML"
361
+ end
362
+ GlossSee "markup"
363
+ end
364
+ end
365
+ end
366
+ end
367
+ EOS
368
+ end
369
+
370
+ it 'should convert hash to dsl with conv' do
371
+ h = {"glossary"=>
372
+ {"title"=>"example glossary",
373
+ "GlossDiv"=>
374
+ {"title"=>"S",
375
+ "GlossList"=>
376
+ {"GlossEntry"=>
377
+ {"ID"=>"SGML",
378
+ "SortAs"=>"SGML",
379
+ "GlossTerm"=>"Standard Generalized Markup Language",
380
+ "Acronym"=>"SGML",
381
+ "Abbrev"=>"ISO 8879:1986",
382
+ "GlossDef"=>
383
+ {"para"=>
384
+ "A meta-markup language, used to create markup languages such as DocBook.",
385
+ "GlossSeeAlso"=>["GML", "XML"]},
386
+ "GlossSee"=>"markup"}}}}}
387
+
388
+ dsl = Dslh.deval(h, :conv => proc {|i| i.to_s.upcase })
389
+ expect(dsl).to eq(<<-EOS)
390
+ GLOSSARY do
391
+ TITLE "EXAMPLE GLOSSARY"
392
+ GLOSSDIV do
393
+ TITLE "S"
394
+ GLOSSLIST do
395
+ GLOSSENTRY do
396
+ ID "SGML"
397
+ SORTAS "SGML"
398
+ GLOSSTERM "STANDARD GENERALIZED MARKUP LANGUAGE"
399
+ ACRONYM "SGML"
400
+ ABBREV "ISO 8879:1986"
401
+ GLOSSDEF do
402
+ PARA "A META-MARKUP LANGUAGE, USED TO CREATE MARKUP LANGUAGES SUCH AS DOCBOOK."
403
+ GLOSSSEEALSO "GML", "XML"
404
+ end
405
+ GLOSSSEE "MARKUP"
406
+ end
407
+ end
408
+ end
409
+ end
410
+ EOS
411
+ end
412
+
413
+ it 'should convert hash to dsl with key_conv' do
414
+ h = {"glossary"=>
415
+ {"title"=>"example glossary",
416
+ "GlossDiv"=>
417
+ {"title"=>"S",
418
+ "GlossList"=>
419
+ {"GlossEntry"=>
420
+ {"ID"=>"SGML",
421
+ "SortAs"=>"SGML",
422
+ "GlossTerm"=>"Standard Generalized Markup Language",
423
+ "Acronym"=>"SGML",
424
+ "Abbrev"=>"ISO 8879:1986",
425
+ "GlossDef"=>
426
+ {"para"=>
427
+ "A meta-markup language, used to create markup languages such as DocBook.",
428
+ "GlossSeeAlso"=>["GML", "XML"]},
429
+ "GlossSee"=>"markup"}}}}}
430
+
431
+ dsl = Dslh.deval(h, :key_conv => proc {|i| i.to_s.upcase })
432
+ expect(dsl).to eq(<<-EOS)
433
+ GLOSSARY do
434
+ TITLE "example glossary"
435
+ GLOSSDIV do
436
+ TITLE "S"
437
+ GLOSSLIST do
438
+ GLOSSENTRY do
439
+ ID "SGML"
440
+ SORTAS "SGML"
441
+ GLOSSTERM "Standard Generalized Markup Language"
442
+ ACRONYM "SGML"
443
+ ABBREV "ISO 8879:1986"
444
+ GLOSSDEF do
445
+ PARA "A meta-markup language, used to create markup languages such as DocBook."
446
+ GLOSSSEEALSO "GML", "XML"
447
+ end
448
+ GLOSSSEE "markup"
449
+ end
450
+ end
451
+ end
452
+ end
453
+ EOS
454
+ end
455
+
456
+ it 'should convert hash to dsl with value_conv' do
457
+ h = {"glossary"=>
458
+ {"title"=>"example glossary",
459
+ "GlossDiv"=>
460
+ {"title"=>"S",
461
+ "GlossList"=>
462
+ {"GlossEntry"=>
463
+ {"ID"=>"SGML",
464
+ "SortAs"=>"SGML",
465
+ "GlossTerm"=>"Standard Generalized Markup Language",
466
+ "Acronym"=>"SGML",
467
+ "Abbrev"=>"ISO 8879:1986",
468
+ "GlossDef"=>
469
+ {"para"=>
470
+ "A meta-markup language, used to create markup languages such as DocBook.",
471
+ "GlossSeeAlso"=>["GML", "XML"]},
472
+ "GlossSee"=>"markup"}}}}}
473
+
474
+ dsl = Dslh.deval(h, :value_conv => proc {|i| i.to_s.upcase })
475
+ expect(dsl).to eq(<<-EOS)
476
+ glossary do
477
+ title "EXAMPLE GLOSSARY"
478
+ GlossDiv do
479
+ title "S"
480
+ GlossList do
481
+ GlossEntry do
482
+ ID "SGML"
483
+ SortAs "SGML"
484
+ GlossTerm "STANDARD GENERALIZED MARKUP LANGUAGE"
485
+ Acronym "SGML"
486
+ Abbrev "ISO 8879:1986"
487
+ GlossDef do
488
+ para "A META-MARKUP LANGUAGE, USED TO CREATE MARKUP LANGUAGES SUCH AS DOCBOOK."
489
+ GlossSeeAlso "GML", "XML"
490
+ end
491
+ GlossSee "MARKUP"
492
+ end
493
+ end
494
+ end
495
+ end
496
+ EOS
497
+ end
326
498
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dslh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Sugawara
@@ -14,42 +14,42 @@ dependencies:
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ! '>='
31
+ - - '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ! '>='
38
+ - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ! '>='
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: 2.11.0
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ! '>='
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: 2.11.0
55
55
  description: It define Hash as a DSL.
@@ -80,20 +80,21 @@ require_paths:
80
80
  - lib
81
81
  required_ruby_version: !ruby/object:Gem::Requirement
82
82
  requirements:
83
- - - ! '>='
83
+ - - '>='
84
84
  - !ruby/object:Gem::Version
85
85
  version: '0'
86
86
  required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  requirements:
88
- - - ! '>='
88
+ - - '>='
89
89
  - !ruby/object:Gem::Version
90
90
  version: '0'
91
91
  requirements: []
92
92
  rubyforge_project:
93
- rubygems_version: 2.1.11
93
+ rubygems_version: 2.0.14
94
94
  signing_key:
95
95
  specification_version: 4
96
96
  summary: It define Hash as a DSL.
97
97
  test_files:
98
98
  - spec/dslh_spec.rb
99
99
  - spec/spec_helper.rb
100
+ has_rdoc: