Class: Stupidedi::Versions::FunctionalGroups::FiftyTen::ElementTypes::TimeVal::NonEmpty

Inherits:
Stupidedi::Versions::FunctionalGroups::FiftyTen::ElementTypes::TimeVal show all
Defined in:
lib/stupidedi/versions/functional_groups/005010/element_types/time_val.rb

Overview

Non-empty time value. Unlike common models of time, this encapsulates only an hour, minute, and second (with fractional seconds) but not a date. Shouldn't be directly instantiated -- instead, use the value constructor.

Instance Attribute Summary collapse

Attributes inherited from Stupidedi::Values::SimpleElementVal

#position, #usage

Instance Method Summary collapse

Methods inherited from Stupidedi::Versions::FunctionalGroups::FiftyTen::ElementTypes::TimeVal

empty, #time?, #too_long?, value

Methods inherited from Stupidedi::Values::SimpleElementVal

#allowed?, #component?, #date?, #id?, #leaf?, #numeric?, #simple?, #string?, #time?

Methods inherited from Stupidedi::Values::AbstractElementVal

#element?, #size

Methods inherited from Stupidedi::Values::AbstractVal

#blank?, #characters, #component?, #composite?, #definition, #element?, #functional_group?, #interchange?, #invalid?, #loop?, #present?, #repeated?, #segment?, #separator?, #simple?, #size, #table?, #transaction_set?, #transmission?

Methods included from Color

ansi, #ansi

Constructor Details

#initialize(hour, minute, second, usage, position) ⇒ NonEmpty

Returns a new instance of NonEmpty.


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/time_val.rb', line 162

def initialize(hour, minute, second, usage, position)
  @hour, @minute, @second = hour, minute, second

  valid   = (  hour.nil? or hour.between?(0, 24))
  valid &&= (minute.nil? or not hour.nil?)
  valid &&= (minute.nil? or minute.between?(0, 60))
  valid &&= (second.nil? or not minute.nil?)
  valid &&= (second.nil? or second.between?(0, 60))

  super(usage, position)

  unless valid
    raise Exceptions::InvalidElementError,
      "invalid time #{inspect}"
  end
end

Instance Attribute Details

#hourInteger (readonly)

Returns:

  • (Integer)

154
155
156
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/time_val.rb', line 154

def hour
  @hour
end

#minuteInteger (readonly)

Returns:

  • (Integer)

157
158
159
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/time_val.rb', line 157

def minute
  @minute
end

#secondInteger, BigDecimal (readonly)

Returns:

  • (Integer, BigDecimal)

160
161
162
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/time_val.rb', line 160

def second
  @second
end

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (Boolean)

261
262
263
264
265
266
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/time_val.rb', line 261

def ==(other)
  eql?(other) or
   (other.hour   == @hour   and
    other.minute == @minute and
    other.second == @second)
end

#copy(changes = {}) ⇒ NonEmpty

Returns:


180
181
182
183
184
185
186
187
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/time_val.rb', line 180

def copy(changes = {})
  NonEmpty.new \
    changes.fetch(:hour, @hour),
    changes.fetch(:minute, @minute),
    changes.fetch(:second, @second),
    changes.fetch(:usage, usage),
    changes.fetch(:position, position)
end

#empty?Boolean

Returns:

  • (Boolean)

193
194
195
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/time_val.rb', line 193

def empty?
  false
end

#inspectString

Returns:

  • (String)

212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/time_val.rb', line 212

def inspect
  id = definition.bind do |d|
    "[#{'% 5s' % d.id}: #{d.name}]".bind do |s|
      if usage.forbidden?
        ansi.forbidden(s)
      elsif usage.required?
        ansi.required(s)
      else
        ansi.optional(s)
      end
    end
  end

  hh =   @hour.try{|h| "%02d" % h }  || "hh"
  mm = @minute.try{|m| "%02d" % m }  || "mm"
  ss = @second.try{|s| s.to_s("F") } || "ss"

  ansi.element("TM.value#{id}") << "(#{hh}:#{mm}:#{ss})"
end

#to_s(hh = "hh", mm = "mm", ss = "ss") ⇒ String

Returns:

  • (String)

233
234
235
236
237
238
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/time_val.rb', line 233

def to_s(hh = "hh", mm = "mm", ss = "ss")
  hh =   @hour.try{|h| "%02d" % h } || hh
  mm = @minute.try{|m| "%02d" % m } || mm
  ss = @second.try{|s| "%02f" % s } || ss
  "#{hh}#{mm}#{ss}"
end

#to_time(date, minute = nil, second = nil) ⇒ Time

Returns:

  • (Time)

198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/time_val.rb', line 198

def to_time(date, minute = nil, second = nil)
  minute = @minute || minute
  second = @second || second

  if not second.nil?
    Time.utc(date.year, date.month, date.day, @hour, minute, second.to_f)
  elsif not minute.nil?
    Time.utc(date.year, date.month, date.day, @hour, minute)
  else
    Time.utc(date.year, date.month, date.day, @hour)
  end
end

#to_x12(truncate = true) ⇒ String

Returns:

  • (String)

241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/time_val.rb', line 241

def to_x12(truncate = true)
  hh =   @hour.try{|h| "%02d" % h }
  mm = @minute.try{|m| "%02d" % m }
  ss = @second.try{|s| "%02d" % s }
  ff = @second.try do |s|
    s.frac.to_s("F").
      gsub(/^0*\./, "").
      gsub(/0+$/, "")
  end

  x12 = "#{hh}#{mm}#{ss}#{ff}"

  truncate ? x12.take(definition.max_length) : x12
end

#too_short?Boolean

Returns:

  • (Boolean)

256
257
258
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/time_val.rb', line 256

def too_short?
  to_s(nil, nil, nil).length < definition.min_length
end

#valid?Boolean

Returns:

  • (Boolean)

189
190
191
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/time_val.rb', line 189

def valid?
  true
end