Class: ACH::Component
- Inherits:
-
Object
- Object
- ACH::Component
- Includes:
- Constants, Validations
- Defined in:
- lib/ach/component.rb
Defined Under Namespace
Classes: UnknownAttribute
Constant Summary
Constants included from Constants
ACH::Constants::BLOCKING_FACTOR, ACH::Constants::FORMAT_CODE, ACH::Constants::RECORD_SIZE
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
Instance Method Summary collapse
- #control ⇒ Object
- #fields_for(component_or_class) ⇒ Object
- #header(fields = {}, &block) ⇒ Object
-
#initialize(fields = {}, &block) ⇒ Component
constructor
A new instance of Component.
- #method_missing(meth, *args) ⇒ Object
Methods included from Validations
Constructor Details
#initialize(fields = {}, &block) ⇒ Component
Returns a new instance of Component.
14 15 16 17 18 19 20 21 22 |
# File 'lib/ach/component.rb', line 14 def initialize fields = {}, &block @attributes = {} fields.each do |name, value| raise UnknownAttribute.new(name) unless Formatter::RULES.key?(name) @attributes[name] = value end after_initialize if respond_to?(:after_initialize) instance_eval(&block) if block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
12 13 14 |
# File 'lib/ach/component.rb', line 12 def attributes @attributes end |
Class Method Details
.has_many(plural_name, proc_defaults = nil) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/ach/component.rb', line 56 def self.has_many plural_name, proc_defaults = nil attr_reader plural_name singular_name = plural_name.to_s.singularize klass = "ACH::#{singular_name.camelize}".constantize define_method(singular_name) do |*args, &block| index_or_fields = args.first || {} return send(plural_name)[index_or_fields] if Fixnum === index_or_fields defaults = proc_defaults ? instance_exec(&proc_defaults) : {} klass.new(fields_for(singular_name).merge(defaults).merge(index_or_fields)).tap do |component| component.instance_eval(&block) if block send(plural_name) << component end end define_method :after_initialize do instance_variable_set("@#{plural_name}", []) end end |
Instance Method Details
#control ⇒ Object
45 46 47 48 49 |
# File 'lib/ach/component.rb', line 45 def control klass = self.class::Control fields = klass.fields.select{ |f| respond_to?(f) || attributes[f] } klass.new Hash[*fields.zip(fields.map{ |f| send(f) }).flatten] end |
#fields_for(component_or_class) ⇒ Object
51 52 53 54 |
# File 'lib/ach/component.rb', line 51 def fields_for component_or_class klass = component_or_class.is_a?(Class) ? component_or_class : "ACH::#{component_or_class.camelize}".constantize klass < Component ? attributes : attributes.select{ |k, v| klass.fields.include?(k) && attributes[k] } end |
#header(fields = {}, &block) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/ach/component.rb', line 36 def header fields = {}, &block before_header merged_fields = fields_for(self.class::Header).merge(fields) @header ||= self.class::Header.new(merged_fields) @header.tap do |head| head.instance_eval(&block) if block end end |