5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/xls_function/format_string/transform_rules/times.rb', line 5
def self.included(klass)
klass.class_eval do
rule(hour: simple(:hour)) do |context|
::XlsFunction::FormatString::Evaluators::TimeEvaluator.new(
context[:hour],
context[:caches],
flags: context[:flags],
&:hour
)
end
rule(hour_elapsed: simple(:hour_elapsed)) do |context|
::XlsFunction::FormatString::Evaluators::ElapsedTimeEvaluator.new(
context[:caches],
half: context[:hour_elapsed] == '[h]',
&:hour
)
end
rule(minute: simple(:minute)) do |context|
::XlsFunction::FormatString::Evaluators::TimeEvaluator.new(
context[:minute],
context[:caches],
minute_mode: true,
&:minute
)
end
rule(minute_elapsed: simple(:minute_elapsed)) do |context|
::XlsFunction::FormatString::Evaluators::ElapsedTimeEvaluator.new(
context[:caches],
half: context[:minute_elapsed] == '[m]',
&:minute
)
end
rule(second: simple(:second)) do |context|
::XlsFunction::FormatString::Evaluators::TimeEvaluator.new(
context[:second],
context[:caches],
&:second
)
end
rule(second: simple(:second), millisecond: simple(:millisecond)) do |context|
->(input) do
second_func = ::XlsFunction::FormatString::Evaluators::TimeEvaluator.new(
context[:second],
context[:caches],
&:second
)
millisecond_func = ::XlsFunction::FormatString::Evaluators::TimeEvaluator.new(
context[:millisecond],
context[:caches]
)
"#{second_func.call(input)}.#{millisecond_func.call(input)}"
end
end
rule(second_elapsed: simple(:second_elapsed)) do |context|
::XlsFunction::FormatString::Evaluators::ElapsedTimeEvaluator.new(
context[:caches],
half: context[:second_elapsed] == '[s]',
&:second
)
end
rule(ampm: simple(:ampm)) do |context|
context[:flags][:ampm] = true
->(input) do
time = ::XlsFunction::Converters::TimeConverter.convert_with_cache(input, context[:caches])
am, pm = context[:ampm].to_s.split('/')
time.hour <= 11 ? am : pm
end
end
rule(time: subtree(:time)) { time }
rule(texts: subtree(:texts), time: subtree(:time)) do
->(input) do
"#{texts.map { |expr| expr.call(input) }.join}#{time.call(input)}"
end
end
rule(times: subtree(:time)) do
->(input) { Array(time).flatten.map { |expr| expr.call(input) }.join }
end
end
end
|