Sha256: de02ad3f2b8c08773c8a9a80bd0170bd446d0b066a43d31fed13254045c1f3ad

Contents?: true

Size: 1.5 KB

Versions: 30

Compression:

Stored size: 1.5 KB

Contents

module NetSuite
  module Support
    class Sublist
      include Support::Fields

      def self.inherited(subclass)
        subclass.class_eval do
          field :replace_all
        end
      end

      class << self

        def sublist(key, klass)
          field key

          # TODO setting class methods might be better? How to reach into the subclass?

          define_method(:sublist_key) { key }
          define_method(:sublist_class) { klass }

          define_method("#{key}=") do |list|
            self.process_sublist(list)
          end

          define_method("#{key}") do
            @list ||= []
          end
        end

      end

      def initialize(attributes = {})
        initialize_from_attributes_hash(attributes)
      end

      def to_record
        rec = { "#{record_namespace}:#{sublist_key.to_s.lower_camelcase}" => send(self.sublist_key).map(&:to_record) }

        if !replace_all.nil?
          rec["#{record_namespace}:replaceAll"] = !!replace_all
        end

        rec
      end

      def <<(item)
        @list ||= []
        @list << self.process_sublist_item(item)
      end

      protected
        def process_sublist(list)
          list = [ list ] if !list.is_a?(Array)

          @list = list.map do |item|
            self.process_sublist_item(item)
          end
        end

        def process_sublist_item(item)
          if item.class == self.sublist_class
            item
          else
            self.sublist_class.new(item)
          end
        end

    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
netsuite-0.8.11 lib/netsuite/support/sublist.rb
netsuite-0.8.10 lib/netsuite/support/sublist.rb
netsuite-0.8.9 lib/netsuite/support/sublist.rb
netsuite-0.8.8 lib/netsuite/support/sublist.rb
netsuite-0.8.7 lib/netsuite/support/sublist.rb
netsuite-0.8.6 lib/netsuite/support/sublist.rb
netsuite-0.8.5 lib/netsuite/support/sublist.rb
netsuite-0.8.4 lib/netsuite/support/sublist.rb
netsuite-0.8.3 lib/netsuite/support/sublist.rb
netsuite-0.8.2 lib/netsuite/support/sublist.rb
netsuite-0.8.1 lib/netsuite/support/sublist.rb
netsuite-0.8.0 lib/netsuite/support/sublist.rb
netsuite-0.7.9 lib/netsuite/support/sublist.rb
netsuite-0.7.8 lib/netsuite/support/sublist.rb
netsuite-0.7.7 lib/netsuite/support/sublist.rb
netsuite-0.7.6 lib/netsuite/support/sublist.rb
netsuite-0.7.5 lib/netsuite/support/sublist.rb
netsuite-0.7.4 lib/netsuite/support/sublist.rb
netsuite-0.7.3 lib/netsuite/support/sublist.rb
netsuite-0.7.2 lib/netsuite/support/sublist.rb