Class: ACH::Component

Inherits:
Object
  • Object
show all
Includes:
Constants, Validations
Defined in:
lib/ach/component.rb

Direct Known Subclasses

Batch, File

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validations

#errors, #valid?

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

#method_missing(meth, *args) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/ach/component.rb', line 24

def method_missing meth, *args
  if Formatter::RULES.key?(meth)
    args.empty? ? @attributes[meth] : (@attributes[meth] = args.first)
  else
    super
  end
end

Instance Attribute Details

#attributesObject (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

#controlObject



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