patty 0.0.7 → 0.0.8

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.
@@ -39,7 +39,7 @@ module Patty
39
39
 
40
40
  def validate_signature(signature = nil)
41
41
  if signature.nil? || !signature.is_a?(Patty::TimeSignature)
42
- raise "key validation fail for #{signature}"
42
+ raise "key validation fail for #{signature} class #{signature.class}"
43
43
  end
44
44
  end
45
45
 
data/lib/patty/base.rb CHANGED
@@ -17,13 +17,17 @@ module Patty
17
17
  'events'
18
18
  end
19
19
 
20
+ def measure
21
+ :tminute
22
+ end
23
+
20
24
  def build_signature(datetime_string = '')
21
- Patty::TimeSignature.new(datetime_string[0, 16]).align
25
+ Patty::TimeSignature.new(datetime_string[0, 19]).align.measure measure
22
26
  end
23
27
 
24
28
  def emit(signature = nil, value = nil, marker = nil)
25
29
  if signature.nil?
26
- signature = Patty::TimeSignature.from_datetime.align
30
+ signature = Patty::TimeSignature.from_datetime
27
31
  end
28
32
 
29
33
  current_value = fetch signature, marker
@@ -3,16 +3,28 @@ require 'date'
3
3
  module Patty
4
4
  class TimeSignature
5
5
 
6
- MINUTES = ['00', '10', '20', '30', '40', '50']
7
- HOURS = (0..23).map { |h| "%02d" % h }
8
- MONTHS = (1..12).map { |m| "%02d" % m }
6
+ SECONDS = ['00', '30']
7
+ MINUTES = (0..59).map { |m| "%02d" % m }
8
+ TMINUTES = ['0', '1', '2', '3', '4', '5']
9
+ HOURS = (0..23).map { |h| "%02d" % h }
10
+ MONTHS = (1..12).map { |m| "%02d" % m }
11
+
12
+ MEASURE_LENGTHS = {
13
+ :second => 19, # 2012-04-10 12:30:32
14
+ :minute => 16, # 2012-04-10 12:30
15
+ :tminute => 15, # 2012-04-10 12:3
16
+ :hour => 13, # 2012-04-10 12
17
+ :day => 10, # 2012-04-10
18
+ :month => 7, # 2012-04
19
+ :year => 4 # 2012
20
+ }
9
21
 
10
22
  class << self
11
23
  def from_datetime(datetime = lambda{ DateTime.now })
12
24
  if datetime.is_a?(Proc)
13
25
  datetime = datetime.call
14
26
  end
15
- self.new(datetime.strftime("%Y-%m-%d %H:%M")).align
27
+ self.new(datetime.strftime("%Y-%m-%d %H:%M:%S")).align
16
28
  end
17
29
  end
18
30
 
@@ -34,24 +46,19 @@ module Patty
34
46
  end
35
47
 
36
48
  def kind
37
- case signature.size
38
- when 16
39
- :minute
40
- when 13
41
- :hour
42
- when 10
43
- :day
44
- when 7
45
- :month
46
- when 4
47
- :year
48
- else
49
- raise 'broken signature'
49
+ MEASURE_LENGTHS.select{ |k, v| v == signature.size }.keys[0]
50
+ end
51
+
52
+ def measure(title = :tminute)
53
+ unless MEASURE_LENGTHS.keys.include? title
54
+ raise ArgumentError, 'bad measure title'
50
55
  end
56
+
57
+ self.class.new crop(MEASURE_LENGTHS[title])
51
58
  end
52
59
 
53
60
  def align
54
- self.class.new "%04d-%02d-%02d %02d:%02d" % [year, month, day, hour, aligned_minute]
61
+ self.class.new "%04d-%02d-%02d %02d:%02d:%02d" % [year, month, day, hour, minute, aligned_second]
55
62
  end
56
63
 
57
64
  def cut(from = 0, length = 2)
@@ -59,7 +66,7 @@ module Patty
59
66
  end
60
67
 
61
68
  def crop(to = 13)
62
- signature.size > to ? signature[0, to] : signature
69
+ signature.size > to ? signature[0, to] : ''
63
70
  end
64
71
 
65
72
  def year
@@ -82,11 +89,15 @@ module Patty
82
89
  cut(14).to_i
83
90
  end
84
91
 
85
- def aligned_minute
86
- if kind == :minute
87
- "%-1d0" % (minute / 10)
92
+ def second
93
+ cut(17).to_i
94
+ end
95
+
96
+ def aligned_second
97
+ if kind == :second
98
+ second / 30 == 0 ? '00' : '30'
88
99
  else
89
- minute
100
+ second
90
101
  end
91
102
  end
92
103
 
@@ -95,13 +106,17 @@ module Patty
95
106
  end
96
107
 
97
108
  def parents
98
- [crop(13), crop(10), crop(7), crop(4)].delete_if{ |p| p.empty? }.map{ |p| self.class.new p }
109
+ [crop(16), crop(15), crop(13), crop(10), crop(7), crop(4)].delete_if{ |p| p.empty? }.map{ |p| self.class.new p }
99
110
  end
100
111
 
101
112
  def children
102
113
  case kind
114
+ when :minute
115
+ SECONDS.map{ |s| self.class.new "#{signature}:#{s}" }
116
+ when :tminute
117
+ (0..9).map{ |m| self.class.new "#{signature}#{m}" }
103
118
  when :hour
104
- MINUTES.map{ |m| self.class.new "#{signature}:#{m}" }
119
+ TMINUTES.map{ |t| self.class.new "#{signature}:#{t}" }
105
120
  when :day
106
121
  HOURS.map{ |h| self.class.new "#{signature} #{h}" }
107
122
  when :month
data/lib/patty/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Patty
2
2
 
3
- VERSION = '0.0.7'
3
+ VERSION = '0.0.8'
4
4
 
5
5
  end
@@ -4,15 +4,15 @@ require 'patty/time_signature'
4
4
  class TimeSignatureTest < Test::Unit::TestCase
5
5
 
6
6
  def setup
7
- @time_signature = Patty::TimeSignature.new('2005-10-10 18:55')
7
+ @time_signature = Patty::TimeSignature.new('2005-10-10 18:55:35')
8
8
  end
9
9
 
10
10
  def test_to_s
11
- assert_equal '2005-10-10 18:55', @time_signature.to_s
11
+ assert_equal '2005-10-10 18:55:35', @time_signature.to_s
12
12
  end
13
13
 
14
14
  def test_kind
15
- assert_equal :minute, @time_signature.kind
15
+ assert_equal :second, @time_signature.kind
16
16
  end
17
17
 
18
18
  def test_cut
@@ -22,11 +22,13 @@ class TimeSignatureTest < Test::Unit::TestCase
22
22
 
23
23
  def test_crop
24
24
  assert_equal '2005-10-10', @time_signature.crop(10)
25
- assert_equal '2005-10-10 18:55', @time_signature.crop(100)
25
+ assert_equal '', @time_signature.crop(100)
26
26
  end
27
27
 
28
28
  def test_parents
29
29
  expected_parents = [
30
+ Patty::TimeSignature.new('2005-10-10 18:55'),
31
+ Patty::TimeSignature.new('2005-10-10 18:5'),
30
32
  Patty::TimeSignature.new('2005-10-10 18'),
31
33
  Patty::TimeSignature.new('2005-10-10'),
32
34
  Patty::TimeSignature.new('2005-10'),
@@ -35,10 +37,15 @@ class TimeSignatureTest < Test::Unit::TestCase
35
37
  assert_equal expected_parents, @time_signature.parents
36
38
  end
37
39
 
38
- def test_children_for_minute
40
+ def test_children_for_second
39
41
  assert_equal 0, @time_signature.children.size
40
42
  end
41
43
 
44
+ def test_children_for_minute
45
+ @time_signature = Patty::TimeSignature.new('2005-10-10 18:55')
46
+ assert_equal 2, @time_signature.children.size
47
+ end
48
+
42
49
  def test_children_for_hour
43
50
  @time_signature = Patty::TimeSignature.new('2005-10-10 18')
44
51
  assert_equal 6, @time_signature.children.size
@@ -60,8 +67,8 @@ class TimeSignatureTest < Test::Unit::TestCase
60
67
  end
61
68
 
62
69
  def test_from_datetime
63
- @time_signature = Patty::TimeSignature.from_datetime DateTime.new(2012, 02, 29, 12, 35)
64
- assert_equal '2012-02-29 12:30', @time_signature.signature
70
+ @time_signature = Patty::TimeSignature.from_datetime DateTime.new(2012, 02, 29, 12, 35, 12)
71
+ assert_equal '2012-02-29 12:35:00', @time_signature.signature
65
72
  end
66
73
 
67
74
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: patty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-09 00:00:00.000000000Z
12
+ date: 2012-04-11 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: yajl-ruby
16
- requirement: &84304380 !ruby/object:Gem::Requirement
16
+ requirement: &83964590 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *84304380
24
+ version_requirements: *83964590
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: riak-client
27
- requirement: &84304170 !ruby/object:Gem::Requirement
27
+ requirement: &83964380 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *84304170
35
+ version_requirements: *83964380
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sinatra
38
- requirement: &84303960 !ruby/object:Gem::Requirement
38
+ requirement: &83964170 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *84303960
46
+ version_requirements: *83964170
47
47
  description: Server part of Patty statistics server
48
48
  email: alexander.lomakin@gmail.com
49
49
  executables: []