Sha256: 9f3d547bae40695a87a07c2a6fb1e5fee71541a74fe6bf291ec4aa13737d2664
Contents?: true
Size: 1.19 KB
Versions: 2
Compression:
Stored size: 1.19 KB
Contents
require 'datev/field' module Datev class Base class << self attr_accessor :fields end attr_accessor :attributes def self.field(name, type, options={}) self.fields ||= [] # Check if there is already a field with the same name if self.fields.find { |f| f.name == name } raise ArgumentError.new("Field #{name} already exists") end self.fields << Field.new(name, type, options) end def initialize(attributes) self.attributes = {} # Check existing name and set value (if valid) attributes.each_pair do |name,value| unless field = self.class.fields.find { |f| f.name == name } raise ArgumentError.new("Field #{name} not found") end field.validate!(value) self.attributes[name] = value end # Check for missing values self.class.fields.select(&:required?).each do |field| if attributes[field.name].nil? raise ArgumentError.new("Field #{field.name} is required but missing") end end end def to_a self.class.fields.map do |field| value = attributes[field.name] field.output(value) end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
datev-0.1.1 | lib/datev/base.rb |
datev-0.1.0 | lib/datev/base.rb |