Sha256: b949b0fe5287dfee7ec8254b22f20825777f9ff51adf06d4d958349795800741

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

require 'json'

module Excon
  module HyperMedia
    # Collection
    #
    # Given a `Hash`, provides dot-notation properties and other helper methods.
    #
    module Collection
      include Enumerable

      def initialize(collection = {})
        @collection ||= collection
        to_properties
      end

      def each(&block)
        collection.each(&block)
      end

      def keys
        @collection.keys
      end

      def key?(key)
        collection.key?(key.to_s)
      end

      def [](key)
        to_property(key)
      end

      private

      def to_properties
        collection.each do |key, value|
          key = key.downcase
          next unless /[@$"]/ !~ key.to_sym.inspect

          singleton_class.class_eval { attr_reader key }
          instance_variable_set("@#{key}", property(value))
        end
      end

      def property(value)
        value.respond_to?(:keys) ? self.class.new(value) : value
      end

      def to_property(key)
        key?(key) ? property(collection[key]) : nil
      end

      def to_property!(key)
        key?(key) ? to_property(key) : method_missing(key)
      end

      attr_reader :collection
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
excon-hypermedia-0.4.3 lib/excon/hypermedia/helpers/collection.rb
excon-hypermedia-0.4.2 lib/excon/hypermedia/helpers/collection.rb
excon-hypermedia-0.4.1 lib/excon/hypermedia/helpers/collection.rb
excon-hypermedia-0.4.0 lib/excon/hypermedia/helpers/collection.rb