Sha256: de9f36c3463d54411d0ac9624efa41b0e5a12277cea249cdcdbf2838b167d796
Contents?: true
Size: 1.75 KB
Versions: 1
Compression:
Stored size: 1.75 KB
Contents
require 'datev/field' module Datev class Base class << self attr_accessor :fields, :default_attributes def inherited(subclass) subclass.fields = self.fields subclass.default_attributes = self.default_attributes end end def self.field(name, type, options={}, &block) self.fields ||= [] # Check if there is already a field with the same name if field_by_name(name) raise ArgumentError.new("Field '#{name}' already exists") end self.fields << Field.new(name, type, options, &block) end def self.field_by_name(name) self.fields.find { |f| f.name == name } end attr_accessor :attributes def initialize(attributes) self.attributes = self.class.default_attributes || {} raise ArgumentError.new('Hash required') unless attributes.is_a?(Hash) # Check existing names and set value (if valid) attributes.each_pair do |name,value| unless field = self.class.field_by_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("Value for field '#{field.name}' is required but missing") end end end def [](name) field = self.class.field_by_name(name) raise ArgumentError.new("Field '#{name}' not found") unless field attributes[field.name] end def output(context=nil) self.class.fields.map do |field| value = attributes[field.name] field.output(value, context) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
datev-0.3.0 | lib/datev/base.rb |