class Hippo_eyeDoc::Field

Attributes

composite[RW]
composite_sequence[RW]
datatype[RW]
maximum[RW]
minimum[RW]
name[RW]
options[RW]
required[RW]
restrictions[RW]
separator[RW]
sequence[RW]

Public Instance Methods

formatted_value(value) click to toggle source
# File lib/hippo_eyeDoc/field.rb, line 15
def formatted_value(value)
  return nil if value.nil?

  case datatype
  when :binary  then value
  when :integer then value.to_i
  when :decimal then parse_decimal(value)
  when :date    then parse_date(value)
  when :time    then parse_time(value)
  else parse_string(value)
  end
end
generate_date(value) click to toggle source
# File lib/hippo_eyeDoc/field.rb, line 105
def generate_date(value)
  value ||= Date.today

  if value.class.to_s == 'Range'
    "#{value.first.strftime('%Y%m%d')}-#{value.last.strftime('%Y%m%d')}"
  elsif maximum == 6
    value.strftime('%y%m%d')
  else
    value.strftime('%Y%m%d')
  end
end
generate_decimal(value) click to toggle source
# File lib/hippo_eyeDoc/field.rb, line 57
def generate_decimal(value)
  value ||= BigDecimal('0')

  value.to_s('F').sub(/\.0\z/,'').rjust(minimum, '0')
end
generate_string(value) click to toggle source
# File lib/hippo_eyeDoc/field.rb, line 41
def generate_string(value)
  if required
    value.to_s.ljust(minimum)
  else
    value.to_s
  end
end
generate_time(value) click to toggle source
# File lib/hippo_eyeDoc/field.rb, line 72
def generate_time(value)
  value ||= Time.now

  if maximum == 4 || value.sec == 0
    value.strftime('%H%M')
  else
    value.strftime('%H%M%S')
  end
end
invalid!(message = "Invalid value specified for click to toggle source
# File lib/hippo_eyeDoc/field.rb, line 145
def invalid!(message = "Invalid value specified for #{self.datatype} field.")
  raise Hippo_eyeDoc::Exceptions::InvalidValue.new message
end
parse_date(value) click to toggle source
# File lib/hippo_eyeDoc/field.rb, line 117
def parse_date(value)
  if value == ''
    invalid! if required
    return nil
  end

  case value.class.to_s
  when "Range"  then value
  when "Date"   then value
  when "Time"   then value.to_date
  when "String"
    format =  case value
              when /\A\d{6}\z/ then '%y%m%d'
              when /\A\d{8}\z/ then '%Y%m%d'
              when /\A(\d{8})-(\d{8})\z/ then
                return Date.parse($1, '%Y%m%d')..Date.parse($2, '%Y%m%d')
              else
                invalid!
              end

    Date.parse(value, format)
  else
    invalid! "Invalid datatype(#{value.class}) for date field."
  end
rescue
  invalid!
end
parse_decimal(value) click to toggle source
# File lib/hippo_eyeDoc/field.rb, line 63
def parse_decimal(value)
  if value == ''
    invalid! if required
    return nil
  end

  BigDecimal(value.to_s)
end
parse_string(value) click to toggle source
# File lib/hippo_eyeDoc/field.rb, line 49
def parse_string(value)
  if value.to_s.empty? && !required
    nil
  else
    value.to_s.strip
  end
end
parse_time(value) click to toggle source
# File lib/hippo_eyeDoc/field.rb, line 82
def parse_time(value)
  if value == ''
    invalid! if required
    return nil
  end

  case value.class.to_s
  when 'Time' then value
  when 'String'
    format =  case value
              when /\A\d{4}\z/ then '%H%M'
              when /\A\d{6}\z/ then '%H%M%S'
              when /\A\d{7,8}\z/ then '%H%M%S%N'
              else invalid!
              end

    Time.strptime(value, format)
  else invalid!
  end
rescue
  invalid!
end
string_value(value) click to toggle source
# File lib/hippo_eyeDoc/field.rb, line 28
def string_value(value)
  return '' if value.nil? && !required

  case datatype
  when :binary  then value
  when :integer then value.to_s.rjust(minimum, '0')
  when :decimal then generate_decimal(value)
  when :date    then generate_date(value)
  when :time    then generate_time(value)
  else generate_string(value)
  end
end