Class: Eddy::Models::Element::DT

Inherits:
Base
  • Object
show all
Defined in:
lib/eddy/models/element/dt.rb

Overview

Date in YYMMDD or CCYYMMDD format based on EDI version being used (DLMS Baseline is 004010)

Values for this type are generated from a UTC formatted Time object.

Instance Attribute Summary collapse

Attributes inherited from Base

#description, #id, #max, #min, #name, #ref, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#doc_comment, #req, #req=

Constructor Details

#initialize(min: nil, max: nil, req: nil, ref: nil, val: nil, fmt: nil) ⇒ void

Parameters:

  • min (Integer) (defaults to: nil)

    (nil)

  • max (Integer) (defaults to: nil)

    (nil)

  • req (String) (defaults to: nil)

    (nil)

  • ref (String) (defaults to: nil)

    (nil)

  • val (Time) (defaults to: nil)

    (nil) A UTC formatted Time object.

  • fmt (Symbol) (defaults to: nil)

    (nil) Format for the date. Valid values: :yymmdd or :ccyymmdd.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/eddy/models/element/dt.rb', line 24

def initialize(
  min: nil,
  max: nil,
  req: nil,
  ref: nil,
  val: nil,
  fmt: nil
)
  @type = "DT"
  @min = min
  @max = max
  self.req = req
  self.ref = ref
  if fmt.nil?
    raise ArgumentError, "DT elements require either a `fmt` value, or `min` and `max` values." if min.nil? || max.nil?
    @fmt = determine_format()
  else
    self.fmt = fmt
  end
  self.value = val
end

Instance Attribute Details

#fmtSymbol<:yymmdd, :ccyymmdd>

Format for the date. Valid values: :yymmdd or :ccyymmdd

Returns:

  • (Symbol<:yymmdd, :ccyymmdd>)


15
16
17
# File 'lib/eddy/models/element/dt.rb', line 15

def fmt
  @fmt
end

Class Method Details

.process_value(val, fmt) ⇒ String

Parameters:

  • val (Time)

    A UTC formatted Time object.

  • fmt (Symbol)

Returns:

  • (String)


77
78
79
80
81
82
83
# File 'lib/eddy/models/element/dt.rb', line 77

def self.process_value(val, fmt)
  case fmt
  when :yymmdd then return Eddy::Util::Time.yymmdd(val)
  when :ccyymmdd then return Eddy::Util::Time.ccyymmdd(val)
  else raise Eddy::Errors::Error "invalid fmt value for DT object"
  end
end

Instance Method Details

#accepted_formatsArray<Symbol>

Returns:

  • (Array<Symbol>)


95
96
97
# File 'lib/eddy/models/element/dt.rb', line 95

def accepted_formats()
  return [:yymmdd, :ccyymmdd]
end

#determine_formatSymbol

Returns:

  • (Symbol)


100
101
102
103
104
105
106
# File 'lib/eddy/models/element/dt.rb', line 100

def determine_format()
  case self.max
  when 6 then return :yymmdd
  when 8 then return :ccyymmdd
  else raise Eddy::Errors::Error, "unable to determine format for dt element"
  end
end

#process_valueString

Returns:

  • (String)


70
71
72
# File 'lib/eddy/models/element/dt.rb', line 70

def process_value()
  return self.class.process_value(@val, self.fmt)
end

#valueString

Returns:

  • (String)

Raises:



65
66
67
# File 'lib/eddy/models/element/dt.rb', line 65

def value()
  return super()
end

#value=(arg) ⇒ void

This method returns an undefined value.

Parameters:

  • arg (Time)

    A UTC formatted Time object.

Raises:

  • (ElementValidationError)

    Unless passed a UTC formatted Time object.



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/eddy/models/element/dt.rb', line 49

def value=(arg)
  if arg == :skip
    @val = :skip
    return
  end
  if arg.nil?()
    @val = arg
    return
  end
  raise Eddy::Errors::TypeValidationError.new(element: self, arg: arg) unless arg.is_a?(Time)
  raise Eddy::Errors::ElementValidationError.new("Argument passed is not in UTC format", element: self) unless arg.utc?()
  @val = arg
end