Sha256: 5d314d227a832c8ac00230c98888282605f49ad43eb6f297b1e2376d8cb68308

Contents?: true

Size: 776 Bytes

Versions: 16

Compression:

Stored size: 776 Bytes

Contents

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

16 entries across 16 versions & 1 rubygems

Version Path
moysklad-0.5 lib/moysklad/entities/collection.rb
moysklad-0.4.10 lib/moysklad/entities/collection.rb
moysklad-0.4.9 lib/moysklad/entities/collection.rb
moysklad-0.4.8 lib/moysklad/entities/collection.rb
moysklad-0.4.7 lib/moysklad/entities/collection.rb
moysklad-0.4.6 lib/moysklad/entities/collection.rb
moysklad-0.4.5 lib/moysklad/entities/collection.rb
moysklad-0.4.4 lib/moysklad/entities/collection.rb
moysklad-0.4.3 lib/moysklad/entities/collection.rb
moysklad-0.4.2 lib/moysklad/entities/collection.rb
moysklad-0.4.1 lib/moysklad/entities/collection.rb
moysklad-0.4.0 lib/moysklad/entities/collection.rb
moysklad-0.3.4 lib/moysklad/entities/collection.rb
moysklad-0.3.3 lib/moysklad/entities/collection.rb
moysklad-0.3.2 lib/moysklad/entities/collection.rb
moysklad-0.3.1 lib/moysklad/entities/collection.rb