tokiyomi 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -26,9 +26,24 @@ Tokiyomi.parse('10日前') # => 2012-12-31T12:34:56
26
26
  ### Supported formats
27
27
 
28
28
  * 年, 月, 日, 時間, 分, 秒 + [前後]
29
- * eg: 10日前
29
+
30
+ ```
31
+ Tokiyomi.parse('10日前') # => 2012-12-31T12:34:56
32
+ Tokiyomi.parse('3時間後') # => 2013-01-10T15:34:56
33
+ ```
34
+
30
35
  * hour and minutes are also able to specified.
31
- * eg: 2ヵ月前の20:00
36
+
37
+ ```
38
+ Tokiyomi.parse('2ヵ月前の20:00') # => 2012-11-10T20:00:00
39
+ Tokiyomi.parse('1週間後の0:00') # => 2013-01-17T00:00:00
40
+ ```
41
+
42
+ * 今日, 明日, 明後日, 昨日, 一昨日
43
+
44
+ ```
45
+ Tokiyomi.parse('明日') # => 2013-01-11T12:34:56
46
+ ```
32
47
 
33
48
  ## Contributing
34
49
 
data/Rakefile CHANGED
@@ -1 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |t|
5
+ t.pattern = "spec/**/*_spec.rb"
6
+ end
7
+
8
+ task :default => [:spec]
data/lib/tokiyomi.rb CHANGED
@@ -1,10 +1,15 @@
1
1
  require 'tokiyomi/version'
2
2
  require 'tokiyomi/relative_time'
3
+ require 'tokiyomi/named_time'
3
4
 
4
5
  module Tokiyomi
5
6
  extend self
6
7
 
7
8
  def parse(str)
8
- RelativeTime.new(str.encode('UTF-8')).calculate(Time.now)
9
+ TimeBase.parser_for(str.encode('UTF-8')).calculate(Time.now)
10
+ end
11
+
12
+ def readable?(str)
13
+ TimeBase.readable?(str)
9
14
  end
10
15
  end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ require 'tokiyomi/time_base'
3
+
4
+ module Tokiyomi
5
+ class NamedTime < TimeBase
6
+ Definition = Struct.new(:duration, :direction)
7
+
8
+ NAMED_TIMES = {
9
+ 'おととい' => Definition.new(2, :ago),
10
+ '一昨日' => Definition.new(2, :ago),
11
+ '昨日' => Definition.new(1, :ago),
12
+ '今日' => Definition.new(0, :ago),
13
+ '明日' => Definition.new(1, :since),
14
+ '明後日' => Definition.new(2, :since),
15
+ 'あさって' => Definition.new(2, :since),
16
+ }.freeze
17
+
18
+ def self.readable?(str)
19
+ str.in?(NAMED_TIMES.keys)
20
+ end
21
+
22
+ def initialize(str)
23
+ definition = NAMED_TIMES[str]
24
+ @duration = definition.duration
25
+ @unit = :days
26
+ @direction = definition.direction
27
+ end
28
+ end
29
+ end
@@ -1,8 +1,8 @@
1
1
  # coding: utf-8
2
- require 'active_support/core_ext'
2
+ require 'tokiyomi/time_base'
3
3
 
4
4
  module Tokiyomi
5
- class RelativeTime
5
+ class RelativeTime < TimeBase
6
6
  UNITS = {
7
7
  '年' => :years,
8
8
  '月' => :months,
@@ -18,16 +18,19 @@ module Tokiyomi
18
18
  str =~ RELATIVE_TIME
19
19
  end
20
20
 
21
- attr_reader :duration, :unit, :direction, :hour_min
22
-
23
21
  def initialize(str)
24
22
  raise ArgumentError, "can't understand `#{str}'" unless self.class.readable?(str)
25
23
 
26
- init_from_dynamic_value(str)
24
+ duration, unit, direction, *hour_min= str.scan(RELATIVE_TIME).first
25
+
26
+ @duration = duration.to_i
27
+ @unit = UNITS[unit]
28
+ @direction = direction == '前' ? :ago : :since
29
+ @hour_min = hour_min.all?(&:nil?) ? nil : Hash[[:hour, :min].zip(hour_min.map(&:to_i))]
27
30
  end
28
31
 
29
32
  def calculate(base)
30
- datetime = duration.send(unit).send(direction, base)
33
+ datetime = super
31
34
 
32
35
  hour_min_fixable? ? datetime.change(hour_min) : datetime
33
36
  end
@@ -37,14 +40,5 @@ module Tokiyomi
37
40
  def hour_min_fixable?
38
41
  hour_min && unit.in?([:years, :months, :days])
39
42
  end
40
-
41
- def init_from_dynamic_value(str)
42
- duration, unit, direction, *hour_min= str.scan(RELATIVE_TIME).first
43
-
44
- @duration = duration.to_i
45
- @unit = UNITS[unit]
46
- @direction = direction == '前' ? :ago : :since
47
- @hour_min = hour_min.all?(&:nil?) ? nil : Hash[[:hour, :min].zip(hour_min.map(&:to_i))]
48
- end
49
43
  end
50
44
  end
@@ -0,0 +1,27 @@
1
+ require 'active_support/core_ext'
2
+
3
+ module Tokiyomi
4
+ class TimeBase
5
+ attr_reader :duration, :unit, :direction, :hour_min
6
+
7
+ class << self
8
+ def readable?(str)
9
+ !!handlable_subclass(str)
10
+ end
11
+
12
+ def parser_for(str)
13
+ handlable_subclass(str).new(str)
14
+ end
15
+
16
+ private
17
+
18
+ def handlable_subclass(str)
19
+ subclasses.detect {|klass| klass.readable?(str) }
20
+ end
21
+ end
22
+
23
+ def calculate(base)
24
+ duration.send(unit).send(direction, base)
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module Tokiyomi
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,16 @@
1
+ def RSpec.root
2
+ @spec_root ||= Pathname.new(File.dirname(__FILE__))
3
+ end
4
+
5
+ $: << File.expand_path('../lib', File.dirname(__FILE__))
6
+
7
+ RSpec.configure do |config|
8
+ config.mock_with :rspec
9
+ end
10
+
11
+ RSpec::Matchers.define :be_calculated_to do |expect|
12
+ match do |relative_time|
13
+ relative_time.calculate(now).should == Time.parse(expect)
14
+ end
15
+ end
16
+ require 'time'
@@ -0,0 +1,16 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+ require 'tokiyomi/named_time'
4
+
5
+ describe Tokiyomi::NamedTime do
6
+ let(:now) { Time.local(2013, 1, 10, 12, 34, 56) }
7
+
8
+ def named_time(str)
9
+ Tokiyomi::NamedTime.new(str)
10
+ end
11
+
12
+ specify { expect(named_time('昨日')).to be_calculated_to('2013/01/09 12:34:56') }
13
+ specify { expect(named_time('おととい')).to be_calculated_to('2013/01/08 12:34:56') }
14
+ specify { expect(named_time('今日')).to be_calculated_to('2013/01/10 12:34:56') }
15
+ specify { expect(named_time('明後日')).to be_calculated_to('2013/01/12 12:34:56') }
16
+ end
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require 'time'
2
+ require 'spec_helper'
3
3
  require 'tokiyomi/relative_time'
4
4
 
5
5
  describe Tokiyomi::RelativeTime do
@@ -9,12 +9,6 @@ describe Tokiyomi::RelativeTime do
9
9
  Tokiyomi::RelativeTime.new(str)
10
10
  end
11
11
 
12
- RSpec::Matchers.define :be_calculated_to do |expect|
13
- match do |relative_time|
14
- relative_time.calculate(now).should == Time.parse(expect)
15
- end
16
- end
17
-
18
12
  specify { expect(rel_time('3日前')).to be_calculated_to('2013/01/07 12:34:56') }
19
13
  specify { expect(rel_time('10分後')).to be_calculated_to('2013/01/10 12:44:56') }
20
14
 
@@ -3,4 +3,11 @@ require 'tokiyomi'
3
3
 
4
4
  describe Tokiyomi do
5
5
  specify { Tokiyomi.parse('10日前').should be_kind_of(Time) }
6
+
7
+ describe '.readable?' do
8
+ specify { Tokiyomi.readable?('10日前').should be_true }
9
+ specify { Tokiyomi.readable?('日後').should be_false }
10
+ specify { Tokiyomi.readable?('おっととい').should be_false }
11
+ specify { Tokiyomi.readable?('おととい').should be_true }
12
+ end
6
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tokiyomi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-08 00:00:00.000000000 Z
12
+ date: 2013-01-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -72,8 +72,12 @@ files:
72
72
  - README.md
73
73
  - Rakefile
74
74
  - lib/tokiyomi.rb
75
+ - lib/tokiyomi/named_time.rb
75
76
  - lib/tokiyomi/relative_time.rb
77
+ - lib/tokiyomi/time_base.rb
76
78
  - lib/tokiyomi/version.rb
79
+ - spec/spec_helper.rb
80
+ - spec/tokiyomi_named_time_spec.rb
77
81
  - spec/tokiyomi_relative_time_spec.rb
78
82
  - spec/tokiyomi_spec.rb
79
83
  - tokiyomi.gemspec
@@ -102,5 +106,7 @@ signing_key:
102
106
  specification_version: 3
103
107
  summary: Japanese relative date/time string parser.
104
108
  test_files:
109
+ - spec/spec_helper.rb
110
+ - spec/tokiyomi_named_time_spec.rb
105
111
  - spec/tokiyomi_relative_time_spec.rb
106
112
  - spec/tokiyomi_spec.rb