Class: XlsFunction::FormatString::Evaluators::TimeEvaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/xls_function/format_string/evaluators/time_evaluator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, cache, minute_mode: false, flags: {}, &after_effect) ⇒ TimeEvaluator

Returns a new instance of TimeEvaluator.



7
8
9
10
11
12
13
# File 'lib/xls_function/format_string/evaluators/time_evaluator.rb', line 7

def initialize(source, cache, minute_mode: false, flags: {}, &after_effect)
  @source = source.to_s
  @cache = cache
  @minute_mode = minute_mode
  @flags = flags
  @after_effect = after_effect
end

Instance Attribute Details

#after_effectObject (readonly)

Returns the value of attribute after_effect.



5
6
7
# File 'lib/xls_function/format_string/evaluators/time_evaluator.rb', line 5

def after_effect
  @after_effect
end

#cacheObject (readonly)

Returns the value of attribute cache.



5
6
7
# File 'lib/xls_function/format_string/evaluators/time_evaluator.rb', line 5

def cache
  @cache
end

#flagsObject (readonly)

Returns the value of attribute flags.



5
6
7
# File 'lib/xls_function/format_string/evaluators/time_evaluator.rb', line 5

def flags
  @flags
end

#minute_modeObject (readonly)

Returns the value of attribute minute_mode.



5
6
7
# File 'lib/xls_function/format_string/evaluators/time_evaluator.rb', line 5

def minute_mode
  @minute_mode
end

#sourceObject (readonly)

Returns the value of attribute source.



5
6
7
# File 'lib/xls_function/format_string/evaluators/time_evaluator.rb', line 5

def source
  @source
end

Instance Method Details

#ampm_modeObject



28
29
30
# File 'lib/xls_function/format_string/evaluators/time_evaluator.rb', line 28

def ampm_mode
  flags[:ampm]
end

#call(input) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/xls_function/format_string/evaluators/time_evaluator.rb', line 15

def call(input)
  time = ::XlsFunction::Converters::TimeConverter.convert_with_cache(input, cache)
  format = convert_format
  result = convert_to_string(time, format)
  return result unless after_effect

  after_effect.call(self, result)
end

#convert_formatObject



24
25
26
# File 'lib/xls_function/format_string/evaluators/time_evaluator.rb', line 24

def convert_format
  XlsFunction::Converters::TimeConverter.convert_format(modified_source, ampm_mode: ampm_mode)
end

#convert_to_string(time, format) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/xls_function/format_string/evaluators/time_evaluator.rb', line 36

def convert_to_string(time, format)
  if format.start_with?('%J')
    time.to_date.strftime(format)
  else
    time.strftime(format)
  end
end

#day(value) ⇒ Object



52
53
54
55
56
# File 'lib/xls_function/format_string/evaluators/time_evaluator.rb', line 52

def day(value)
  return value[1..] if source == 'd' && value.start_with?('0')

  value
end

#gannenObject



72
73
74
# File 'lib/xls_function/format_string/evaluators/time_evaluator.rb', line 72

def gannen
  flags[:gannen]
end

#gengo(value) ⇒ Object



76
77
78
79
80
81
# File 'lib/xls_function/format_string/evaluators/time_evaluator.rb', line 76

def gengo(value)
  return ::XlsFunction::Converters::TimeConverter.convert_wareki_to_alphabet(value) if source == 'g'
  return value[0] if source == 'gg'

  value
end

#hour(value) ⇒ Object



83
84
85
86
87
# File 'lib/xls_function/format_string/evaluators/time_evaluator.rb', line 83

def hour(value)
  return value[1..] if source == 'h' && value.length == 2 && value.start_with?('0')

  value
end

#minute(value) ⇒ Object



89
90
91
92
93
# File 'lib/xls_function/format_string/evaluators/time_evaluator.rb', line 89

def minute(value)
  return value[1..] if source == 'm' && value.length == 2 && value.start_with?('0')

  value
end

#modified_sourceObject



32
33
34
# File 'lib/xls_function/format_string/evaluators/time_evaluator.rb', line 32

def modified_source
  minute_mode ? source.upcase : source.downcase
end

#month(value) ⇒ Object

↓ after_effects: treat formats not covered by strftime



45
46
47
48
49
50
# File 'lib/xls_function/format_string/evaluators/time_evaluator.rb', line 45

def month(value)
  return value[1..] if source == 'm' && value.start_with?('0')
  return value[0] if source == 'mmmmm'

  value
end

#second(value) ⇒ Object



95
96
97
98
99
# File 'lib/xls_function/format_string/evaluators/time_evaluator.rb', line 95

def second(value)
  return value[1..] if source == 's' && value.length == 2 && value.start_with?('0')

  value
end

#wareki(value) ⇒ Object



65
66
67
68
69
70
# File 'lib/xls_function/format_string/evaluators/time_evaluator.rb', line 65

def wareki(value)
  return '' if gannen && value == '1'
  return value.rjust(2, '0') if %w[ee r].include?(source)

  value
end

#weekday(value) ⇒ Object



58
59
60
61
62
63
# File 'lib/xls_function/format_string/evaluators/time_evaluator.rb', line 58

def weekday(value)
  return ::XlsFunction::Converters::TimeConverter.convert_weekday_jp(value.to_i) if source == 'aaa'
  return "#{::XlsFunction::Converters::TimeConverter.convert_weekday_jp(value.to_i)}曜日" if source == 'aaaa'

  value
end