Class: IOStreams::Tabular::Parser::Fixed::Column
- Inherits:
-
Object
- Object
- IOStreams::Tabular::Parser::Fixed::Column
- Defined in:
- lib/io_streams/tabular/parser/fixed.rb
Constant Summary collapse
- TYPES =
%i[string integer float].freeze
Instance Attribute Summary collapse
-
#decimals ⇒ Object
readonly
Returns the value of attribute decimals.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(key: nil, size:, type: :string, decimals: 2) ⇒ Column
constructor
A new instance of Column.
- #parse(value) ⇒ Object
- #render(value, truncate) ⇒ Object
Constructor Details
#initialize(key: nil, size:, type: :string, decimals: 2) ⇒ Column
Returns a new instance of Column.
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/io_streams/tabular/parser/fixed.rb', line 149 def initialize(key: nil, size:, type: :string, decimals: 2) @key = key @size = size == :remainder ? -1 : size.to_i @type = type.to_sym @decimals = decimals unless @size.positive? || (@size == -1) raise(Errors::InvalidLayout, "Size #{size.inspect} must be positive or :remainder") end raise(Errors::InvalidLayout, "Unknown type: #{type.inspect}") unless TYPES.include?(type) end |
Instance Attribute Details
#decimals ⇒ Object (readonly)
Returns the value of attribute decimals.
147 148 149 |
# File 'lib/io_streams/tabular/parser/fixed.rb', line 147 def decimals @decimals end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
147 148 149 |
# File 'lib/io_streams/tabular/parser/fixed.rb', line 147 def key @key end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
147 148 149 |
# File 'lib/io_streams/tabular/parser/fixed.rb', line 147 def size @size end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
147 148 149 |
# File 'lib/io_streams/tabular/parser/fixed.rb', line 147 def type @type end |
Instance Method Details
#parse(value) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/io_streams/tabular/parser/fixed.rb', line 161 def parse(value) return if value.nil? stripped_value = value.to_s.strip case type when :string stripped_value when :integer stripped_value.length.zero? ? nil : value.to_i when :float stripped_value.length.zero? ? nil : value.to_f else raise(Errors::InvalidLayout, "Unsupported type: #{type.inspect}") end end |
#render(value, truncate) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/io_streams/tabular/parser/fixed.rb', line 178 def render(value, truncate) formatted = case type when :string value = value.to_s return value if size == -1 format(truncate ? "%-#{size}.#{size}s" : "%-#{size}s", value) when :integer return value.to_i.to_s if size == -1 truncate = false format("%0#{size}d", value.to_i) when :float return value.to_f.to_s if size == -1 truncate = false format("%0#{size}.#{decimals}f", value.to_f) else raise(Errors::InvalidLayout, "Unsupported type: #{type.inspect}") end if !truncate && formatted.length > size raise(Errors::ValueTooLong, "Value: #{value} is too large to fit into column:#{key} of size:#{size}") end formatted end |