Class: Nacha::Field
- Inherits:
-
Object
- Object
- Nacha::Field
- Defined in:
- lib/nacha/field.rb
Instance Attribute Summary collapse
-
#contents ⇒ Object
Returns the value of attribute contents.
-
#data ⇒ Object
Returns the value of attribute data.
-
#data_type ⇒ Object
readonly
Returns the value of attribute data_type.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#fill_character ⇒ Object
readonly
Returns the value of attribute fill_character.
-
#inclusion ⇒ Object
Returns the value of attribute inclusion.
-
#input_data ⇒ Object
readonly
Returns the value of attribute input_data.
-
#json_output ⇒ Object
readonly
Returns the value of attribute json_output.
-
#justification ⇒ Object
readonly
Returns the value of attribute justification.
-
#name ⇒ Object
Returns the value of attribute name.
-
#output_conversion ⇒ Object
readonly
Returns the value of attribute output_conversion.
-
#position ⇒ Object
Returns the value of attribute position.
-
#validator ⇒ Object
readonly
Returns the value of attribute validator.
Class Method Summary collapse
Instance Method Summary collapse
- #add_error(err_string) ⇒ Object
- #human_name ⇒ Object
-
#initialize(opts = {}) ⇒ Field
constructor
A new instance of Field.
- #mandatory? ⇒ Boolean
- #optional? ⇒ Boolean
- #raw ⇒ Object
- #required? ⇒ Boolean
- #to_ach ⇒ Object
- #to_html ⇒ Object
- #to_json_output ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
- #validate ⇒ Object
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
#contents ⇒ Object
Returns the value of attribute contents.
11 12 13 |
# File 'lib/nacha/field.rb', line 11 def contents @contents end |
#data ⇒ Object
Returns the value of attribute data.
11 12 13 |
# File 'lib/nacha/field.rb', line 11 def data @data end |
#data_type ⇒ Object (readonly)
Returns the value of attribute data_type.
11 12 13 |
# File 'lib/nacha/field.rb', line 11 def data_type @data_type end |
#errors ⇒ Object
Returns the value of attribute errors.
10 11 12 |
# File 'lib/nacha/field.rb', line 10 def errors @errors end |
#fill_character ⇒ Object (readonly)
Returns the value of attribute fill_character.
11 12 13 |
# File 'lib/nacha/field.rb', line 11 def fill_character @fill_character end |
#inclusion ⇒ Object
Returns the value of attribute inclusion.
10 11 12 |
# File 'lib/nacha/field.rb', line 10 def inclusion @inclusion end |
#input_data ⇒ Object (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_output ⇒ Object (readonly)
Returns the value of attribute json_output.
11 12 13 |
# File 'lib/nacha/field.rb', line 11 def json_output @json_output end |
#justification ⇒ Object (readonly)
Returns the value of attribute justification.
11 12 13 |
# File 'lib/nacha/field.rb', line 11 def justification @justification end |
#name ⇒ Object
Returns the value of attribute name.
10 11 12 |
# File 'lib/nacha/field.rb', line 10 def name @name end |
#output_conversion ⇒ Object (readonly)
Returns the value of attribute output_conversion.
11 12 13 |
# File 'lib/nacha/field.rb', line 11 def output_conversion @output_conversion end |
#position ⇒ Object
Returns the value of attribute position.
10 11 12 |
# File 'lib/nacha/field.rb', line 10 def position @position end |
#validator ⇒ Object (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_name ⇒ Object
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
94 95 96 |
# File 'lib/nacha/field.rb', line 94 def mandatory? @inclusion == 'M' end |
#optional? ⇒ Boolean
102 103 104 |
# File 'lib/nacha/field.rb', line 102 def optional? @inclusion == 'O' end |
#raw ⇒ Object
173 174 175 |
# File 'lib/nacha/field.rb', line 173 def raw @data end |
#required? ⇒ Boolean
98 99 100 |
# File 'lib/nacha/field.rb', line 98 def required? @inclusion == 'R' end |
#to_ach ⇒ Object
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_html ⇒ Object
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(' ', ' ') "<span data-field-name=\"#{@name}\" contentEditable=true " \ "class=\"#{field_classes.join(' ')}\" data-name=\"#{@name}\">" \ "#{ach_string}#{tooltip_text}</span>" end |
#to_json_output ⇒ Object
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_s ⇒ Object
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
117 118 119 120 |
# File 'lib/nacha/field.rb', line 117 def valid? validate errors.empty? end |
#validate ⇒ Object
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 |