Sha256: d8cab3daa5b7df611ee74ce16e1a966f2621ee380e771faafb533c4c09bc6461

Contents?: true

Size: 1.43 KB

Versions: 23

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'

module EacRest
  class Entity
    enable_abstract_methods
    enable_simple_cache
    enable_listable
    lists.add_symbol :option, :parent
    common_constructor :api, :data_or_id, :options, default: [{}] do
      self.options = ::EacRest::Entity.lists.option.hash_keys_validate!(options)
    end

    class << self
      def from_array_data(api, array_data, *args)
        array_data.map { |item_data| new(api, item_data, *args) }
      end
    end

    # @param entity_class [Class]
    # @param url_suffix [String]
    # @return [EacRest::Entity]
    def child_entity(entity_class, data_or_id, options = {})
      api.entity(entity_class, data_or_id, options.merge(OPTION_PARENT => self))
    end

    # @return [Hash]
    def data
      if internal_data.blank?
        self.internal_data = data_or_id_data? ? data_or_id : data_from_id
      end

      internal_data
    end

    # @return [Boolean]
    def data_or_id_data?
      data_or_id.is_a?(::Hash)
    end

    # @return [Hash]
    def data_from_id
      raise_abstract_method __method__
    end

    # @return [Object]
    def id
      data_or_id_data? ? id_from_data : data_or_id
    end

    # @return [Object]
    def id_from_data
      raise_abstract_method __method__
    end

    # @return [EacRest::Entity, nil]
    def parent_entity
      options[OPTION_PARENT]
    end

    private

    attr_accessor :internal_data
  end
end

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
eac_rest-0.9.2 lib/eac_rest/entity.rb
eac_tools-0.75.1 sub/eac_rest/lib/eac_rest/entity.rb
eac_tools-0.75.0 sub/eac_rest/lib/eac_rest/entity.rb
eac_tools-0.74.1 sub/eac_rest/lib/eac_rest/entity.rb
eac_tools-0.74.0 sub/eac_rest/lib/eac_rest/entity.rb
eac_tools-0.73.0 sub/eac_rest/lib/eac_rest/entity.rb
eac_tools-0.72.0 sub/eac_rest/lib/eac_rest/entity.rb
eac_rest-0.9.1 lib/eac_rest/entity.rb
eac_tools-0.70.1 sub/eac_rest/lib/eac_rest/entity.rb
eac_tools-0.70.0 sub/eac_rest/lib/eac_rest/entity.rb
eac_tools-0.69.1 sub/eac_rest/lib/eac_rest/entity.rb
eac_tools-0.69.0 sub/eac_rest/lib/eac_rest/entity.rb
eac_tools-0.68.0 sub/eac_rest/lib/eac_rest/entity.rb
eac_tools-0.67.1 sub/eac_rest/lib/eac_rest/entity.rb
eac_tools-0.67.0 sub/eac_rest/lib/eac_rest/entity.rb
eac_tools-0.66.0 sub/eac_rest/lib/eac_rest/entity.rb
eac_tools-0.65.1 sub/eac_rest/lib/eac_rest/entity.rb
eac_tools-0.65.0 sub/eac_rest/lib/eac_rest/entity.rb
eac_tools-0.64.0 sub/eac_rest/lib/eac_rest/entity.rb
eac_tools-0.63.0 sub/eac_rest/lib/eac_rest/entity.rb