Sha256: fa7b6521140099b7c1f1eb2af7e98e98d1ce67d0817c9d769e032a7ac7ad7414

Contents?: true

Size: 839 Bytes

Versions: 1

Compression:

Stored size: 839 Bytes

Contents

require_relative 'context'
require_relative 'collection_meta'

module Moysklad::Entities
  class RowItems < Virtus::Attribute
    include ActiveSupport::Inflector

    def coerce(items)
      items.map do |item|
        type = item['meta']['type']
        entity_class = find_entity_class type
        entity_class.new item
      end
    end

    private

    def find_entity_class(type)
      case type
      when 'customentity'
        Moysklad::Entities::CustomEntity
      else
        class_name = 'Moysklad::Entities::' + camelize(type)
        raise Moysklad::Error, "No entity class for #{type} type" unless Object.const_defined? class_name
        constantize class_name
      end
    end
  end

  class Collection < Base
    attribute :context, Context
    attribute :meta, CollectionMeta
    attribute :rows, RowItems
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
moysklad-0.3.0 lib/moysklad/entities/collection.rb