Class: Nacha::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/nacha/field.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Field

Returns a new instance of Field.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/nacha/field.rb', line 22

def initialize(opts = {})
  @data_type = String
  @errors = []
  @name = 'Unknown'
  @justification = :ljust
  @fill_character = ' '
  @json_output = [:to_s]
  @output_conversion = [:to_s]
  @validated = false
  @data_assigned = false
  opts.each do |k, v|
    setter = "#{k}="
    # rubocop:disable GitlabSecurity/PublicSend
    public_send(setter, v) if respond_to?(setter) && !v.nil?
    # rubocop:enable GitlabSecurity/PublicSend
  end
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



11
12
13
# File 'lib/nacha/field.rb', line 11

def contents
  @contents
end

#dataObject

Returns the value of attribute data.



11
12
13
# File 'lib/nacha/field.rb', line 11

def data
  @data
end

#data_typeObject (readonly)

Returns the value of attribute data_type.



11
12
13
# File 'lib/nacha/field.rb', line 11

def data_type
  @data_type
end

#errorsObject

Returns the value of attribute errors.



10
11
12
# File 'lib/nacha/field.rb', line 10

def errors
  @errors
end

#fill_characterObject (readonly)

Returns the value of attribute fill_character.



11
12
13
# File 'lib/nacha/field.rb', line 11

def fill_character
  @fill_character
end

#inclusionObject

Returns the value of attribute inclusion.



10
11
12
# File 'lib/nacha/field.rb', line 10

def inclusion
  @inclusion
end

#input_dataObject (readonly)

Returns the value of attribute input_data.



11
12
13
# File 'lib/nacha/field.rb', line 11

def input_data
  @input_data
end

#json_outputObject (readonly)

Returns the value of attribute json_output.



11
12
13
# File 'lib/nacha/field.rb', line 11

def json_output
  @json_output
end

#justificationObject (readonly)

Returns the value of attribute justification.



11
12
13
# File 'lib/nacha/field.rb', line 11

def justification
  @justification
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/nacha/field.rb', line 10

def name
  @name
end

#output_conversionObject (readonly)

Returns the value of attribute output_conversion.



11
12
13
# File 'lib/nacha/field.rb', line 11

def output_conversion
  @output_conversion
end

#positionObject

Returns the value of attribute position.



10
11
12
# File 'lib/nacha/field.rb', line 10

def position
  @position
end

#validatorObject (readonly)

Returns the value of attribute validator.



11
12
13
# File 'lib/nacha/field.rb', line 11

def validator
  @validator
end

Class Method Details

.unpack_str(definition = {}) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/nacha/field.rb', line 14

def self.unpack_str(definition = {})
  if definition[:contents].match?(/(Numeric|\$+\u00a2\u00a2)/)
    'a'
  else
    'A'
  end + definition[:position].size.to_s
end

Instance Method Details

#add_error(err_string) ⇒ Object



122
123
124
# File 'lib/nacha/field.rb', line 122

def add_error(err_string)
  errors << err_string
end

#human_nameObject



148
149
150
151
# File 'lib/nacha/field.rb', line 148

def human_name
  # @human_name ||= @name.to_s.gsub('_', ' ').capitalize
  @human_name ||= @name.to_s.split('_').map(&:capitalize).join(' ')
end

#mandatory?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/nacha/field.rb', line 94

def mandatory?
  @inclusion == 'M'
end

#optional?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/nacha/field.rb', line 102

def optional?
  @inclusion == 'O'
end

#rawObject



173
174
175
# File 'lib/nacha/field.rb', line 173

def raw
  @data
end

#required?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/nacha/field.rb', line 98

def required?
  @inclusion == 'R'
end

#to_achObject



126
127
128
129
130
131
132
133
134
# File 'lib/nacha/field.rb', line 126

def to_ach
  str = to_s
  fill_char = @fill_character
  fill_char = ' ' unless str
  str ||= ''
  # rubocop:disable GitlabSecurity/PublicSend
  str.public_send(justification, position.count, fill_char)
  # rubocop:enable GitlabSecurity/PublicSend
end

#to_htmlObject



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/nacha/field.rb', line 153

def to_html
  tooltip_text = "<span class=\"tooltiptext\" >#{human_name} #{errors.join(' ')}</span>"
  field_classes = ["nacha-field tooltip"]
  field_classes += ['mandatory'] if mandatory?
  field_classes += ['required'] if required?
  field_classes += ['optional'] if optional?
  field_classes += ['error'] if errors.any?

  ach_string = to_ach.gsub(' ', '&nbsp;')
  "<span data-field-name=\"#{@name}\" contentEditable=true " \
    "class=\"#{field_classes.join(' ')}\" data-name=\"#{@name}\">" \
    "#{ach_string}#{tooltip_text}</span>"
end

#to_json_outputObject



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/nacha/field.rb', line 136

def to_json_output
  if @json_output
    @json_output.reduce(@data) do |memo, operation|
      # rubocop:disable GitlabSecurity/PublicSend
      memo&.public_send(*operation)
      # rubocop:enable GitlabSecurity/PublicSend
    end
  else
    to_s
  end
end

#to_sObject



167
168
169
170
171
# File 'lib/nacha/field.rb', line 167

def to_s
  # rubocop:disable GitlabSecurity/PublicSend
  @data.public_send(*output_conversion).to_s
  # rubocop:enable GitlabSecurity/PublicSend
end

#valid?Boolean

Returns:

  • (Boolean)


117
118
119
120
# File 'lib/nacha/field.rb', line 117

def valid?
  validate
  errors.empty?
end

#validateObject



106
107
108
109
110
111
112
113
114
115
# File 'lib/nacha/field.rb', line 106

def validate
  return if @validated

  validate_definition_attributes
  validate_data_presence
  validate_numeric_format
  run_custom_validator

  @validated = true
end