Sha256: f563a70da5f769be705e379305fcf8277e81c459446c6f8d246aa23e731e6464

Contents?: true

Size: 1.16 KB

Versions: 7

Compression:

Stored size: 1.16 KB

Contents

module JsonApiClient
  module Relationships
    class Relations
      include Helpers::DynamicAttributes
      include Helpers::Dirty
      include ActiveModel::Serialization

      def initialize(relations)
        self.attributes = relations
        clear_changes_information
      end

      def present?
        attributes.present?
      end

      def as_json_api
        Hash[attributes_for_serialization.map do |k, v|
               [k, v.slice("data")]  if v.has_key?("data")
             end.compact]
      end

      def as_json
        Hash[attributes.map do |k, v|
               [k, v.slice("data")]  if v.has_key?("data")
             end.compact]
      end

      def attributes_for_serialization
        attributes.slice(*changed)
      end

      protected

      def set_attribute(name, value)
        value = case value
        when JsonApiClient::Resource
          {data: value.as_relation}
        when Array
          {data: value.map(&:as_relation)}
        when NilClass
          {data: nil}
        else
          value
        end
        attribute_will_change!(name) if value != attributes[name]
        attributes[name] = value
      end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
json_api_client-1.2.0 lib/json_api_client/relationships/relations.rb
json_api_client-1.1.1 lib/json_api_client/relationships/relations.rb
json_api_client-1.1.0 lib/json_api_client/relationships/relations.rb
json_api_client-1.0.2 lib/json_api_client/relationships/relations.rb
json_api_client-1.0.1 lib/json_api_client/relationships/relations.rb
json_api_client-1.0.0 lib/json_api_client/relationships/relations.rb
json_api_client-1.0.0.beta7 lib/json_api_client/relationships/relations.rb