Class: Embulk::Guess::TimeFormatGuess::GuessMatch

Inherits:
Object
  • Object
show all
Defined in:
lib/embulk/guess/time_format_guess.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delimiters, parts, part_options) ⇒ GuessMatch

Returns a new instance of GuessMatch.



25
26
27
28
29
# File 'lib/embulk/guess/time_format_guess.rb', line 25

def initialize(delimiters, parts, part_options)
  @delimiters = delimiters
  @parts = parts
  @part_options = part_options
end

Instance Attribute Details

#part_optionsObject (readonly)

Returns the value of attribute part_options.



140
141
142
# File 'lib/embulk/guess/time_format_guess.rb', line 140

def part_options
  @part_options
end

#partsObject (readonly)

Returns the value of attribute parts.



139
140
141
# File 'lib/embulk/guess/time_format_guess.rb', line 139

def parts
  @parts
end

Instance Method Details

#array_sequence_find(array, seq) ⇒ Object



163
164
165
166
167
168
# File 'lib/embulk/guess/time_format_guess.rb', line 163

def array_sequence_find(array, seq)
  (array.size - seq.size + 1).times {|i|
    return i if array[i, seq.size] == seq
  }
  return nil
end

#formatObject



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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/embulk/guess/time_format_guess.rb', line 31

def format
  format = ''
  @parts.size.times do |i|
    format << @delimiters[i-1] if i != 0
    option = @part_options[i]

    case @parts[i]
    when :year
      format << '%Y'

    when :month
      case option
      when :zero
        format << '%m'
      when :blank
        #format << '%_m'  # not supported
        format << '%m'
      when :none
        #format << '%-m'  # not supported
        format << '%m'
      else
        format << '%m'
      end

    when :day
      case option
      when :zero
        format << '%d'
      when :blank
        format << '%e'
      when :none
        format << '%d'  # not supported
      else
        format << '%d'
      end

    when :hour
      case option
      when :zero
        format << '%H'
      when :blank
        format << '%k'
      when :none
        format << '%k'  # not supported
      else
        format << '%H'
      end

    when :minute
      # heading options are not supported
      format << '%M'

    when :second
      # heading options are not supported
      format << '%S'

    when :frac
      if option <= 3
        format << '%L'
      #elsif option <= 6
      #  format << '%6N'
      #elsif option <= 6
      #  format << '%6N'
      #elsif option <= 9
      #  format << '%9N'
      #elsif option <= 12
      #  format << '%12N'
      #elsif option <= 15
      #  format << '%15N'
      #elsif option <= 18
      #  format << '%18N'
      #elsif option <= 21
      #  format << '%21N'
      #elsif option <= 24
      #  format << '%24N'
      else
        format << '%N'
      end

    when :zone
      case option
      when :extended
        format << '%:z'
      else
        # :simple, :abb
        # don't use %Z even with :abb: https://github.com/jruby/jruby/issues/3702
        format << '%z'
      end

    else
      raise "Unknown part: #{@parts[i]}"
    end
  end

  return format
end

#merge!(another_in_group) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/embulk/guess/time_format_guess.rb', line 142

def merge!(another_in_group)
  part_options = another_in_group.part_options
  @part_options.size.times do |i|
    @part_options[i] ||= part_options[i]
    if @part_options[i] == nil
      part_options[i]
    elsif part_options[i] == nil
      @part_options[i]
    else
      [@part_options[i], part_options[i]].sort.last
    end
  end

  # if DMY matches, MDY is likely false match of DMY.
  dmy = array_sequence_find(another_in_group.parts, [:day, :month, :year])
  mdy = array_sequence_find(@parts, [:month, :day, :year])
  if mdy && dmy
    @parts[mdy, 3] = [:day, :month, :year]
  end
end

#mergeable_groupObject



128
129
130
131
132
133
134
135
136
137
# File 'lib/embulk/guess/time_format_guess.rb', line 128

def mergeable_group
  # MDY is mergible with DMY
  if i = array_sequence_find(@parts, [:day, :month, :year])
    ps = @parts.dup
    ps[i, 3] = [:month, :day, :year]
    [@delimiters, ps]
  else
    [@delimiters, @parts]
  end
end