Class: Fit::GenericAdapter

Inherits:
TypeAdapter show all
Defined in:
lib/fit/type_adapter.rb

Overview

A generic adapter uses the appartent type of its input to store appropriate values. For example, if a cell contains <td>1</td>, a Fixnum is generated; if a cell contains <td>0.5</td>, a Float is generated. Note that this process might not feasible for every type of primitive data.

Instance Attribute Summary

Attributes inherited from TypeAdapter

#fixture, #target, #type

Instance Method Summary collapse

Methods inherited from TypeAdapter

#equals, for, #get, #initialize, #is_output?, on, #set, #to_s

Constructor Details

This class inherits a constructor from Fit::TypeAdapter

Instance Method Details

#parse(value) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fit/type_adapter.rb', line 78

def parse value
  return true if value.downcase == 'true'
  return false if value.downcase == 'false'
  unless @type.nil?
    result = @fixture.parse value, @type
    return result unless result.nil?
  end
  return Integer(value) if value =~ /\A-?\d+\Z/
  return Float(value) if value =~ /\A[+-]?\d*\.\d+([eE][+-]?\d+)?\Z/
  elements = value.split(',')
  unless elements.size == 1
    array = []
    element_adapter = TypeAdapter.for @target, @name, @is_output
    elements.each do |e|
      array << element_adapter.parse(e.strip)
    end
    return array
  end
  return value
end