Sha256: a2961d8420cf6e148087cde04f08e71f64e52c9267c383fbbde7250ea8513000

Contents?: true

Size: 1.92 KB

Versions: 26

Compression:

Stored size: 1.92 KB

Contents

module NetSuite
  module Support
    module Records
      include Attributes
      include Namespaces::PlatformCore

      def to_record
        attributes.reject { |k,v| self.class.read_only_fields.include?(k) }.inject({}) do |hash, (k,v)|
          kname = "#{record_namespace}:"
          kname += k == :klass ? 'class' : k.to_s.lower_camelcase

          to_attributes!(hash, kname, v)

          if Array === v
            v = v.map { |i| i.respond_to?(:to_record) ? i.to_record : i }
          elsif v.respond_to?(:to_record)
            v = v.to_record
          end

          hash[kname] = v
          hash
        end
      end

      def to_attributes!(hash, kname, v)
        if v.respond_to?(:internal_id) && v.internal_id
          hash[:attributes!] ||= {}
          hash[:attributes!][kname] ||= {}
          hash[:attributes!][kname]['internalId'] = v.internal_id
        end

        if v.respond_to?(:external_id) && v.external_id
          hash[:attributes!] ||= {}
          hash[:attributes!][kname] ||= {}
          hash[:attributes!][kname]['externalId'] = v.external_id
        end

        if v.kind_of?(NetSuite::Records::RecordRef) && v.type
          hash[:attributes!] ||= {}
          hash[:attributes!][kname] ||= {}
          hash[:attributes!][kname]['type'] = v.type.lower_camelcase
        end

        if v.kind_of?(NetSuite::Records::CustomRecordRef) && v.type_id
          hash[:attributes!] ||= {}
          hash[:attributes!][kname] ||= {}
          hash[:attributes!][kname]['typeId'] = v.type_id.lower_camelcase
        end
      end

      def record_type
        "#{record_namespace}:#{self.class.to_s.split('::').last}"
      end

      def refresh(credentials = {})
        fresh_record = self.class.get(self.internal_id, credentials)

        self.attributes = fresh_record.send(:attributes)
        self.external_id = fresh_record.external_id
        self.errors = nil

        self
      end

    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
netsuite-0.7.5 lib/netsuite/support/records.rb
netsuite-0.7.4 lib/netsuite/support/records.rb
netsuite-0.7.3 lib/netsuite/support/records.rb
netsuite-0.7.2 lib/netsuite/support/records.rb
netsuite-0.7.1 lib/netsuite/support/records.rb
netsuite-0.7.0 lib/netsuite/support/records.rb
netsuite-0.6.9 lib/netsuite/support/records.rb
netsuite-0.6.8 lib/netsuite/support/records.rb
netsuite-0.6.7 lib/netsuite/support/records.rb
netsuite-0.6.6 lib/netsuite/support/records.rb
netsuite-0.6.5 lib/netsuite/support/records.rb
netsuite-0.6.4 lib/netsuite/support/records.rb
netsuite-0.6.3 lib/netsuite/support/records.rb
netsuite-0.6.2 lib/netsuite/support/records.rb
netsuite-0.6.1 lib/netsuite/support/records.rb
netsuite-0.6.0 lib/netsuite/support/records.rb
netsuite-0.5.9 lib/netsuite/support/records.rb
netsuite-0.5.8 lib/netsuite/support/records.rb
netsuite-0.5.7 lib/netsuite/support/records.rb
netsuite-0.5.6 lib/netsuite/support/records.rb