dslh 0.3.1 → 0.3.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 069a25cadfe07e5fb94b13999c621805b209934f
4
- data.tar.gz: 61352ba15ae2c64775f6ee6498142cf4343f3004
3
+ metadata.gz: eb022da4335f23ff59901ac5652400831dd13c6f
4
+ data.tar.gz: 1e257ec574e65f1d24409001864aed67be2c8d84
5
5
  SHA512:
6
- metadata.gz: cee130b7d63b7288c7b4db402117562d290fc1d29dbf9a94a494d7f27d7ef992685df441b8510a050b40b3f3f6db66a9766cc40262d3de617b239895c391acbf
7
- data.tar.gz: 2d76120513c6ce2b5fdff0236b1c3e4160d548f8523c30d61ed70c623c9611f640a8983b47639150336da8a0d3632fb590237db5ef4d7cd350eb5b7f2ca73437
6
+ metadata.gz: 90d4d8d730e57bab79f1e438996f6d0e1de4d7c5a5701b0affb0bb9da9ecc056633426d8c204d48f00ea7029ac2ee725c42673761161a6fc5493f558658aee57
7
+ data.tar.gz: 78e73bcd8b3686b8dbfc6d8dff8a0e9d56a0f53d77730d991fef01270bde4b0ac5a7ed0a5c432d631e3115b2d8df1fb1f50dac79ff689d3c13a158c8ca08ac4a
@@ -38,7 +38,10 @@ class Dslh
38
38
  end # of class methods
39
39
 
40
40
  def initialize(options = {})
41
- @options = options.dup
41
+ @options = {
42
+ :time_inspecter => method(:inspect_time)
43
+ }.merge(options)
44
+
42
45
  @options[:key_conv] ||= (@options[:conv] || proc {|i| i.to_s })
43
46
  @options[:value_conv] ||= @options[:conv]
44
47
  end
@@ -176,7 +179,13 @@ class Dslh
176
179
  end
177
180
  else
178
181
  value = value_conv.call(value) if value_conv
179
- value_buf.puts(' ' + value.inspect)
182
+
183
+ if @options[:time_inspecter] and value.kind_of?(Time)
184
+ value = @options[:time_inspecter].call(value)
185
+ value_buf.puts(' ' + value)
186
+ else
187
+ value_buf.puts(' ' + value.inspect)
188
+ end
180
189
  end
181
190
 
182
191
  return nested
@@ -193,6 +202,14 @@ class Dslh
193
202
  keys.any? {|k| exclude_key.call(k) }
194
203
  end
195
204
 
205
+ def inspect_time(time)
206
+ if Time.respond_to?(:parse)
207
+ "Time.parse(#{time.to_s.inspect})"
208
+ else
209
+ "Time.at(#{time.tv_sec}, #{time.tv_usec})"
210
+ end
211
+ end
212
+
196
213
  class Scope
197
214
  def _(key = nil, &block)
198
215
  nested_hash = ScopeBlock.nest(binding, 'block')
@@ -1,3 +1,3 @@
1
1
  class Dslh
2
- VERSION = '0.3.1'
2
+ VERSION = '0.3.2'
3
3
  end
@@ -2531,6 +2531,18 @@ end
2531
2531
  )
2532
2532
  end
2533
2533
 
2534
+ it 'include time"' do
2535
+ h = Dslh.eval do
2536
+ key1 'value'
2537
+ key2 Time.parse('2016/05/21 00:00 UTC')
2538
+ end
2539
+
2540
+ expect(h).to eq(
2541
+ {"key1"=>"value",
2542
+ "key2"=>Time.parse('2016/05/21 00:00 UTC')}
2543
+ )
2544
+ end
2545
+
2534
2546
  it 'should convert hash to dsl (symbol key)' do
2535
2547
  h = {:glossary=>
2536
2548
  {:title=>"example glossary",
@@ -2570,6 +2582,54 @@ glossary do
2570
2582
  end
2571
2583
  end
2572
2584
  end
2585
+ end
2586
+ EOS
2587
+ end
2588
+
2589
+ it 'should convert hash to dsl (inclute Time)' do
2590
+ h = {:glossary=>
2591
+ {:title=>"example glossary",
2592
+ :date=>Time.parse('2016/05/21 00:00 UTC')}}
2593
+
2594
+ parse_method = Time.method(:parse)
2595
+ time_cc = (class << Time; self; end)
2596
+ time_cc.send(:undef_method, :parse)
2597
+
2598
+ dsl = Dslh.deval(h)
2599
+ expect(dsl).to eq(<<-EOS)
2600
+ glossary do
2601
+ title "example glossary"
2602
+ date Time.at(1463788800, 0)
2603
+ end
2604
+ EOS
2605
+
2606
+ time_cc.send(:define_method, :parse, &parse_method)
2607
+ end
2608
+
2609
+ it 'should convert hash to dsl (inclute Time / use Time#parse)' do
2610
+ h = {:glossary=>
2611
+ {:title=>"example glossary",
2612
+ :date=>Time.parse('2016/05/21 00:00 UTC')}}
2613
+
2614
+ dsl = Dslh.deval(h)
2615
+ expect(dsl).to eq(<<-EOS)
2616
+ glossary do
2617
+ title "example glossary"
2618
+ date Time.parse("2016-05-21 00:00:00 UTC")
2619
+ end
2620
+ EOS
2621
+ end
2622
+
2623
+ it 'should convert hash to dsl (inclute Time / pass time_inspecter)' do
2624
+ h = {:glossary=>
2625
+ {:title=>"example glossary",
2626
+ :date=>Time.parse('2016/05/21 00:00 UTC')}}
2627
+
2628
+ dsl = Dslh.deval(h, :time_inspecter => proc {|i| i.to_s.inspect })
2629
+ expect(dsl).to eq(<<-EOS)
2630
+ glossary do
2631
+ title "example glossary"
2632
+ date "2016-05-21 00:00:00 UTC"
2573
2633
  end
2574
2634
  EOS
2575
2635
  end
@@ -1,2 +1,3 @@
1
1
  require 'dslh'
2
2
  require 'json'
3
+ require 'time'
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.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Sugawara