Class: Patty::TimeSignature

Inherits:
Object
  • Object
show all
Defined in:
lib/patty/time_signature.rb

Constant Summary collapse

SECONDS =
['00', '30']
MINUTES =
(0..59).map { |m| "%02d" % m }
TMINUTES =
['0', '1', '2', '3', '4', '5']
HOURS =
(0..23).map { |h| "%02d" % h }
MONTHS =
(1..12).map { |m| "%02d" % m }
MEASURE_LENGTHS =
{
  :second  => 19, # 2012-04-10 12:30:32
  :minute  => 16, # 2012-04-10 12:30
  :tminute => 15, # 2012-04-10 12:3
  :hour    => 13, # 2012-04-10 12
  :day     => 10, # 2012-04-10
  :month   => 7,  # 2012-04
  :year    => 4   # 2012
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(signature = '') ⇒ TimeSignature

Returns a new instance of TimeSignature.



33
34
35
36
37
38
# File 'lib/patty/time_signature.rb', line 33

def initialize(signature = '')
  if signature.empty?
    raise ArgumentError, 'signature can not be empty'
  end
  @signature = signature
end

Instance Attribute Details

#signatureObject (readonly)

Returns the value of attribute signature.



31
32
33
# File 'lib/patty/time_signature.rb', line 31

def signature
  @signature
end

Class Method Details

.from_datetime(datetime = lambda{ DateTime.now }) ⇒ Object



23
24
25
26
27
28
# File 'lib/patty/time_signature.rb', line 23

def from_datetime(datetime = lambda{ DateTime.now })
  if datetime.is_a?(Proc)
    datetime = datetime.call
  end
  self.new(datetime.strftime("%Y-%m-%d %H:%M:%S")).align
end

Instance Method Details

#==(other) ⇒ Object



40
41
42
# File 'lib/patty/time_signature.rb', line 40

def ==(other)
  signature == other.signature
end

#alignObject



60
61
62
# File 'lib/patty/time_signature.rb', line 60

def align
  self.class.new "%04d-%02d-%02d %02d:%02d:%02d" % [year, month, day, hour, minute, aligned_second]
end

#aligned_secondObject



96
97
98
99
100
101
102
# File 'lib/patty/time_signature.rb', line 96

def aligned_second
  if kind == :second
    second / 30 == 0 ? '00' : '30'
  else
    second
  end
end

#childrenObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/patty/time_signature.rb', line 112

def children
  case kind
    when :minute
      SECONDS.map{ |s| self.class.new "#{signature}:#{s}" }
    when :tminute
      (0..9).map{ |m| self.class.new "#{signature}#{m}" }
    when :hour
      TMINUTES.map{ |t| self.class.new "#{signature}:#{t}" }
    when :day
      HOURS.map{ |h| self.class.new "#{signature} #{h}" }
    when :month
      (1..days_count).map{ |d| self.class.new "#{signature}-" + '%02d' % d }
    when :year
      MONTHS.map{ |m| self.class.new "#{signature}-#{m}" }
    else
      []
  end
end

#crop(to = 13) ⇒ Object



68
69
70
# File 'lib/patty/time_signature.rb', line 68

def crop(to = 13)
  signature.size > to ? signature[0, to] : ''
end

#cut(from = 0, length = 2) ⇒ Object



64
65
66
# File 'lib/patty/time_signature.rb', line 64

def cut(from = 0, length = 2)
  signature.size > from ? signature[from, length] : ''
end

#dayObject



80
81
82
# File 'lib/patty/time_signature.rb', line 80

def day
  cut(8).to_i
end

#days_countObject



104
105
106
# File 'lib/patty/time_signature.rb', line 104

def days_count
  Date.new(year, month, -1).day
end

#hourObject



84
85
86
# File 'lib/patty/time_signature.rb', line 84

def hour
  cut(11).to_i
end

#kindObject



48
49
50
# File 'lib/patty/time_signature.rb', line 48

def kind
  MEASURE_LENGTHS.select{ |k, v| v == signature.size }.keys[0]
end

#measure(title = :tminute) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/patty/time_signature.rb', line 52

def measure(title = :tminute)
  unless MEASURE_LENGTHS.keys.include? title
    raise ArgumentError, 'bad measure title'
  end

  self.class.new crop(MEASURE_LENGTHS[title])
end

#minuteObject



88
89
90
# File 'lib/patty/time_signature.rb', line 88

def minute
  cut(14).to_i
end

#monthObject



76
77
78
# File 'lib/patty/time_signature.rb', line 76

def month
  cut(5).to_i
end

#parentsObject



108
109
110
# File 'lib/patty/time_signature.rb', line 108

def parents
  [crop(16), crop(15), crop(13), crop(10), crop(7), crop(4)].delete_if{ |p| p.empty? }.map{ |p| self.class.new p }
end

#secondObject



92
93
94
# File 'lib/patty/time_signature.rb', line 92

def second
  cut(17).to_i
end

#to_datetimeObject



131
132
133
# File 'lib/patty/time_signature.rb', line 131

def to_datetime
  DateTime.new year, month, day, hour, minute
end

#to_sObject



44
45
46
# File 'lib/patty/time_signature.rb', line 44

def to_s
  signature
end

#yearObject



72
73
74
# File 'lib/patty/time_signature.rb', line 72

def year
  cut(0, 4).to_i
end