Class: Datev::Field
- Inherits:
-
Object
- Object
- Datev::Field
- Defined in:
- lib/datev/field.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
Returns the value of attribute block.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #format ⇒ Object
-
#initialize(name, type, options = {}, &block) ⇒ Field
constructor
A new instance of Field.
- #limit ⇒ Object
- #output(value, context = nil) ⇒ Object
- #precision ⇒ Object
- #required? ⇒ Boolean
- #scale ⇒ Object
- #validate!(value) ⇒ Object
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, ={}, &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 .is_a?(Hash) raise ArgumentError.new('Options param has to be a Hash') end unless (.keys - [:limit, :required, :format, :precision, :scale]).empty? raise ArgumentError.new('Options param includes unknown key') end self.name = name self.type = type self. = if block_given? self.instance_eval(&block) end end |
Instance Attribute Details
#block ⇒ Object
Returns the value of attribute block.
3 4 5 |
# File 'lib/datev/field.rb', line 3 def block @block end |
#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 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
35 36 37 |
# File 'lib/datev/field.rb', line 35 def format [:format] end |
#limit ⇒ Object
39 40 41 |
# File 'lib/datev/field.rb', line 39 def limit [: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 |
#precision ⇒ Object
43 44 45 |
# File 'lib/datev/field.rb', line 43 def precision [:precision] end |
#required? ⇒ Boolean
31 32 33 |
# File 'lib/datev/field.rb', line 31 def required? [:required] == true end |
#scale ⇒ Object
47 48 49 |
# File 'lib/datev/field.rb', line 47 def scale [: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 |