Sha256: 188af1b0f6f0dcb741ac74dd5807ae30c4eb65e61724f380b2e73ef304495018

Contents?: true

Size: 968 Bytes

Versions: 5

Compression:

Stored size: 968 Bytes

Contents

# frozen_string_literal: true
module OFX
  class Aggregate
    attr_reader :tag
    protected :tag

    def initialize(options = {})
      @tag = options[:name] || :ofx
    end

    def root
      dump(@tag)
    end

    protected

    def dump(element, body = nil)
      tag = element.upcase
      "<#{tag}>#{body}</#{tag}>"
    end

    def elements
      respond_to?(:order, true) ? sort_by_order : public_methods(false)
    end
    
    def ordered?
      respond_to?(:order, true)
    end
    
    def sort_by_order
      public_methods(false).sort_by { |element| order.index(element.to_s) }
    end
    
    def build_element_group(tag)
      if send(tag).respond_to?(:element_group, true)
        send(tag).send(:element_group)
      else
        dump(tag, send(tag))
      end
    end
    
    def element_group
      body = elements.inject('') do |result, tag|
        result.dup << build_element_group(tag)
      end
      dump(@tag, body)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ofx_ruby-0.0.15 lib/ofx/aggregate.rb
ofx_ruby-0.0.14 lib/ofx/aggregate.rb
ofx_ruby-0.0.13 lib/ofx/aggregate.rb
ofx_ruby-0.0.12 lib/ofx/aggregate.rb
ofx_ruby-0.0.11 lib/ofx/aggregate.rb