Class: Eancom::Edifact::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/eancom/edifact/data.rb

Defined Under Namespace

Classes: LengthError, NotFoundInDictionary, RequiredError, TypeError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, length:, dictionary: nil, description:, required:) ⇒ Data

Returns a new instance of Data.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/eancom/edifact/data.rb', line 11

def initialize(type:,
               length:,
               dictionary: nil,
               description:,
               required:
              )
  @type = type
  @length = length
  @dictionary = dictionary
  @description = description
  @required = required
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



9
10
11
# File 'lib/eancom/edifact/data.rb', line 9

def description
  @description
end

#dictionaryObject

Returns the value of attribute dictionary.



9
10
11
# File 'lib/eancom/edifact/data.rb', line 9

def dictionary
  @dictionary
end

#lengthObject

Returns the value of attribute length.



9
10
11
# File 'lib/eancom/edifact/data.rb', line 9

def length
  @length
end

#requiredObject

Returns the value of attribute required.



9
10
11
# File 'lib/eancom/edifact/data.rb', line 9

def required
  @required
end

#typeObject

Returns the value of attribute type.



9
10
11
# File 'lib/eancom/edifact/data.rb', line 9

def type
  @type
end

Instance Method Details

#identifier(value) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/eancom/edifact/data.rb', line 32

def identifier(value)
  if @dictionary
    hash = @dictionary[value]
    unless hash
      raise NotFoundInDictionary.new(
        "Value not found for #{value}. Error in Data: #{self.to_s}"
      )
    else
      hash[:identifier]
    end
  else
    ""
  end
end

#to_sObject



47
48
49
50
51
52
53
# File 'lib/eancom/edifact/data.rb', line 47

def to_s
  "type: #{@type} " \
  "length: #{@length} " \
  "dictionary: #{@dictionary} " \
  "description: #{@description} " \
  "required: #{required} "
end

#valid?(key, value) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
# File 'lib/eancom/edifact/data.rb', line 24

def valid?(key, value)
  type?(key, value)
  length?(key, value)
  required?(key, value)
  dictionary?(key, value) unless value.empty?
end