Class: Paxmex::Schema::Field

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

Constant Summary collapse

DATE_PATTERN =
/^date\((.+)\)$/
TIME_PATTERN =
/^time\((.+)\)$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Field

Returns a new instance of Field.



10
11
12
13
14
15
# File 'lib/paxmex/schema/field.rb', line 10

def initialize(opts = {})
  @name  = opts[:name]
  @start = opts[:start]
  @final = opts[:final]
  @type  = opts[:type] || 'string'
end

Instance Attribute Details

#finalObject (readonly)

Returns the value of attribute final.



8
9
10
# File 'lib/paxmex/schema/field.rb', line 8

def final
  @final
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/paxmex/schema/field.rb', line 8

def name
  @name
end

#startObject (readonly)

Returns the value of attribute start.



8
9
10
# File 'lib/paxmex/schema/field.rb', line 8

def start
  @start
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/paxmex/schema/field.rb', line 8

def type
  @type
end

Instance Method Details

#parse(raw_value) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/paxmex/schema/field.rb', line 17

def parse(raw_value)
  case type
  when 'string' then raw_value.rstrip
  when 'julian' then parse_julian_date(raw_value) rescue nil
  when 'date' then Date.strptime(raw_value, '%m%d%Y') rescue nil
  when 'numeric' then raw_value.strip.to_i
  when 'decimal' then parse_decimal(raw_value)
  when DATE_PATTERN then parse_date_pattern(raw_value) rescue nil
  when TIME_PATTERN then parse_time_pattern(raw_value) rescue nil
  else fail "Could not parse field type #{type}. Supported types: string, julian, date, numeric, decimal, date(format), time(format)"
  end
end