Sha256: cef38e795ac0e568598d4b455f20f7a92747431ec927729fe9eb8eaaee1ce63d

Contents?: true

Size: 1.64 KB

Versions: 25

Compression:

Stored size: 1.64 KB

Contents

module ActiveRemote
  module Publication
    # Returns a hash of publishable attributes.
    #
    def publishable_hash
      keys = _publishable_attributes_or_attribute_keys

      attributes_hash = keys.inject({}) do |publishable_hash, key|
        value = respond_to?(key) ? __send__(key) : read_attribute(key)

        publishable_hash[key] = case
                                when value.respond_to?(:map) then
                                  _map_value(value)
                                when value.respond_to?(:publishable_hash) then
                                  value.publishable_hash
                                when value.respond_to?(:to_hash) then
                                  value.to_hash
                                else
                                  value
                                end

        publishable_hash
      end

      attributes_hash
    end

    def _publishable_json_attributes
      _publishable_attributes_or_attribute_keys - _publishable_json_methods
    end

    def _publishable_json_methods
      _publishable_attributes_or_attribute_keys.reject { |attribute| @attributes.key?(attribute) }
    end

  private

    def _map_value(value)
      case
      when value.any? { |obj| obj.respond_to?(:publishable_hash) } then
        value.map(&:publishable_hash)
      when value.any? { |obj| obj.respond_to?(:to_hash) } then
        value.map(&:to_hash)
      else
        value
      end
    end

    def _publishable_attributes_or_attribute_keys
      @_publishable_attributes_or_attribute_keys = _publishable_attributes
      @_publishable_attributes_or_attribute_keys ||= @attributes.keys
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
active_remote-2.4.0 lib/active_remote/publication.rb
active_remote-2.3.5 lib/active_remote/publication.rb
active_remote-2.3.4 lib/active_remote/publication.rb
active_remote-2.3.3 lib/active_remote/publication.rb
active_remote-2.3.3.pre lib/active_remote/publication.rb
active_remote-2.3.2 lib/active_remote/publication.rb
active_remote-2.3.1 lib/active_remote/publication.rb
active_remote-2.3.0 lib/active_remote/publication.rb
active_remote-2.2.0 lib/active_remote/publication.rb
active_remote-2.1.1 lib/active_remote/publication.rb
active_remote-2.1.0 lib/active_remote/publication.rb
active_remote-2.1.0.rc2 lib/active_remote/publication.rb
active_remote-2.1.0.rc1 lib/active_remote/publication.rb
active_remote-2.1.0.beta2 lib/active_remote/publication.rb
active_remote-2.1.0.beta1 lib/active_remote/publication.rb
active_remote-2.0.2 lib/active_remote/publication.rb
active_remote-2.0.1 lib/active_remote/publication.rb
active_remote-2.0.0 lib/active_remote/publication.rb
active_remote-2.0.0.rc2 lib/active_remote/publication.rb
active_remote-2.0.0.rc1 lib/active_remote/publication.rb