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 = {}, &block) ⇒ 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
26
27
28
29
# File 'lib/datev/field.rb', line 5

def initialize(name, type, options={}, &block)
  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

  if block_given?
    self.instance_eval(&block)
  end
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



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

def block
  @block
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#formatObject



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

def format
  options[:format]
end

#limitObject



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

def limit
  options[:limit]
end

#output(value, context = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/datev/field.rb', line 73

def output(value, context=nil)
  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

#precisionObject



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

def precision
  options[:precision]
end

#required?Boolean

Returns:

  • (Boolean)


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

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

#scaleObject



47
48
49
# File 'lib/datev/field.rb', line 47

def scale
  options[:scale]
end

#validate!(value) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/datev/field.rb', line 51

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