Class: Paxmex::Schema::Field
- Inherits:
-
Object
- Object
- Paxmex::Schema::Field
- Defined in:
- lib/paxmex/schema/field.rb
Instance Attribute Summary collapse
-
#final ⇒ Object
readonly
Returns the value of attribute final.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#start ⇒ Object
readonly
Returns the value of attribute start.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Field
constructor
A new instance of Field.
- #parse(raw_value) ⇒ Object
- #parse_decimal(value) ⇒ Object
- #parse_julian_date(date_string) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Field
Returns a new instance of Field.
7 8 9 10 11 12 |
# File 'lib/paxmex/schema/field.rb', line 7 def initialize(opts = {}) @name = opts[:name] @start = opts[:start] @final = opts[:final] @type = opts[:type] || 'string' end |
Instance Attribute Details
#final ⇒ Object (readonly)
Returns the value of attribute final.
5 6 7 |
# File 'lib/paxmex/schema/field.rb', line 5 def final @final end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/paxmex/schema/field.rb', line 5 def name @name end |
#start ⇒ Object (readonly)
Returns the value of attribute start.
5 6 7 |
# File 'lib/paxmex/schema/field.rb', line 5 def start @start end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
5 6 7 |
# File 'lib/paxmex/schema/field.rb', line 5 def type @type end |
Instance Method Details
#parse(raw_value) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/paxmex/schema/field.rb', line 14 def parse(raw_value) date_pattern = /^date\((.+)\)$/ time_pattern = /^time\((.+)\)$/ case type when 'string' then raw_value.rstrip when 'julian' then parse_julian_date(raw_value) when 'date' then Date.strptime(raw_value, '%m%d%Y') when 'numeric' then raw_value.strip.to_i when 'decimal' then parse_decimal(raw_value) when date_pattern then Date.strptime(raw_value, date_pattern.match(type).captures.first) when time_pattern then Time.strptime(raw_value, time_pattern.match(type).captures.first) else fail "Could not parse field type #{type}. Supported types: string, julian, date, numeric, decimal, date(format), time(format)" end end |
#parse_decimal(value) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/paxmex/schema/field.rb', line 30 def parse_decimal(value) # fields _may_ end with a letter unless value.match(/^[0-9]*[0-9A-R{}]$/) fail "Unexpected value '#{value}' for field '#{name}'" end is_credit = !!(value =~ /[JKLMNOPQR}]/) value = value.gsub(/[ABCDEFGHIJKLMNOPQR{}]/, 'A'=>1, 'B'=>2, 'C'=>3, 'D'=>4, 'E'=>5, 'F'=>6, 'G'=>7, 'H'=>8, 'I'=>9, 'J'=>1, 'K'=>2, 'L'=>3, 'M'=>4, 'N'=>5, 'O'=>6, 'P'=>7, 'Q'=>8, 'R'=>9, '{'=>0, '}'=>0) parsed_value = value.to_i * (is_credit ? -1 : 1) / 100.0 BigDecimal.new(parsed_value.to_s, 7) end |
#parse_julian_date(date_string) ⇒ Object
46 47 48 |
# File 'lib/paxmex/schema/field.rb', line 46 def parse_julian_date(date_string) Date.ordinal(date_string[0..3].to_i, date_string[4..6].to_i) end |