Sha256: 9ca46ebe63af8df223d481477cae20bd4c506fee3461c78e03b5a2e68c2a3c43

Contents?: true

Size: 581 Bytes

Versions: 1

Compression:

Stored size: 581 Bytes

Contents

require_relative "./object"
require_relative "../alma_api"

class AlmaApi::Collection < Delegator
  def initialize(hash, items_key, item_class = AlmaApi::Object)
    hash.each do |_key, _value|
      if _key != items_key
        singleton_class.class_eval do
          attr_accessor "#{_key}".to_sym
        end

        instance_variable_set("@#{_key}".to_sym, _value)
      end
    end

    @items = hash.try(:[], items_key).try(:map) do |_item|
      item_class.new(_item)
    end || []
  end

  def __getobj__
    @items
  end

  def __setobj__(obj)
    @items = obj
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
alma_api-0.1.0 lib/alma_api/collection.rb