Sha256: 18c41b653e453e4a6ec1596ca1b90881a2a816b15dac4e03d0e2c218ababd8cb

Contents?: true

Size: 1.63 KB

Versions: 11

Compression:

Stored size: 1.63 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) : @attributes[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

11 entries across 11 versions & 1 rubygems

Version Path
active_remote-1.6.1 lib/active_remote/publication.rb
active_remote-1.6.0 lib/active_remote/publication.rb
active_remote-1.5.9 lib/active_remote/publication.rb
active_remote-1.5.8 lib/active_remote/publication.rb
active_remote-1.5.7 lib/active_remote/publication.rb
active_remote-1.5.6 lib/active_remote/publication.rb
active_remote-1.5.5 lib/active_remote/publication.rb
active_remote-1.5.4 lib/active_remote/publication.rb
active_remote-1.5.2 lib/active_remote/publication.rb
active_remote-1.5.1 lib/active_remote/publication.rb
active_remote-1.5.0 lib/active_remote/publication.rb