Class: Datev::Field

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, options = {}) ⇒ Field

Returns a new instance of Field.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/datev/field.rb', line 5

def initialize(name, type, options={})
  unless name.is_a?(String)
    raise ArgumentError.new('Name param has to be a String')
  end

  unless [:string, :decimal, :boolean, :date, :integer].include?(type)
    raise ArgumentError.new('Type param not recognized')
  end

  unless options.is_a?(Hash)
    raise ArgumentError.new('Options param has to be a Hash')
  end

  unless (options.keys - [:limit, :required, :format, :precision, :scale]).empty?
    raise ArgumentError.new('Options param includes unknown key')
  end

  self.name = name
  self.type = type
  self.options = options
end

Instance Attribute Details

#name ⇒ Object

Returns the value of attribute name.



3
4
5
# File 'lib/datev/field.rb', line 3

def name
  @name
end

#options ⇒ Object

Returns the value of attribute options.



3
4
5
# File 'lib/datev/field.rb', line 3

def options
  @options
end

#type ⇒ Object

Returns the value of attribute type.



3
4
5
# File 'lib/datev/field.rb', line 3

def type
  @type
end

Instance Method Details

#format ⇒ Object



31
32
33
# File 'lib/datev/field.rb', line 31

def format
  options[:format]
end

#limit ⇒ Object



35
36
37
# File 'lib/datev/field.rb', line 35

def limit
  options[:limit]
end

#output(value) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/datev/field.rb', line 69

def output(value)
  return if value.nil?

  case type
  when :string
    value
  when :integer
    value.to_s
  when :decimal
    ("%.#{scale}f" % value).sub('.',',')
  when :boolean
    value ? 1 : 0
  when :date
    value.strftime(format)
  end
end

#precision ⇒ Object



39
40
41
# File 'lib/datev/field.rb', line 39

def precision
  options[:precision]
end

#required? ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/datev/field.rb', line 27

def required?
  options[:required] == true
end

#scale ⇒ Object



43
44
45
# File 'lib/datev/field.rb', line 43

def scale
  options[:scale]
end

#validate!(value) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/datev/field.rb', line 47

def validate!(value)
  if value.nil?
    raise ArgumentError.new("Value for field '#{name}' is required") if required?
  else
    case type
    when :string
      raise ArgumentError.new("Value given for field '#{name}' is not a String") unless value.is_a?(String)
      raise ArgumentError.new("Value '#{value}' for field '#{name}' is too long") if limit && value.length > limit
    when :integer
      raise ArgumentError.new("Value given for field '#{name}' is not an Integer") unless value.is_a?(Integer)
      raise ArgumentError.new("Value '#{value}' for field '#{name}' is too long") if limit && value.to_s.length > limit
    when :decimal
      raise ArgumentError.new("Value given for field '#{name}' is not a Decimal") unless value.is_a?(Numeric)
      raise ArgumentError.new("Value '#{value}' for field '#{name}' is too long") if precision && value.to_s.length > precision+1
    when :date
      raise ArgumentError.new("Value given for field '#{name}' is not a Date or Time") unless value.is_a?(Time) || value.is_a?(Date)
    when :boolean
      raise ArgumentError.new("Value given for field '#{name}' is not a Boolean") unless [true, false].include?(value)
    end
  end
end