Sha256: 59bac1774803b7fe3d8d1644a789ae2263cc05dc0e5011533fd88fb429b206ce

Contents?: true

Size: 1.09 KB

Versions: 21

Compression:

Stored size: 1.09 KB

Contents

module FunWithJsonAPi
  # Builds an options hash for ActiveModelSerializers::Deserialization.jsonapi_parse
  class DeserializerConfigBuilder
    def self.build(deserializer)
      new(deserializer).build
    end

    private_class_method :new

    attr_reader :deserializer

    def initialize(deserializer)
      @deserializer = deserializer
    end

    def build
      {
        only: build_only_values,
        keys: build_keys_value
      }
    end

    protected

    def build_only_values
      attribute_only_values(deserializer.attributes) +
        attribute_only_values(deserializer.relationships)
    end

    def build_keys_value
      Hash[
        attribute_key_values(deserializer.attributes) +
        attribute_key_values(deserializer.relationships)
      ]
    end

    private

    def attribute_only_values(attributes_or_relationships)
      attributes_or_relationships.map(&:name)
    end

    def attribute_key_values(attributes_or_relationships)
      attributes_or_relationships.select { |a| a.name != a.as }
                                 .map { |a| [a.name, a.as] }
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
fun_with_json_api-0.0.11.1 lib/fun_with_json_api/deserializer_config_builder.rb
fun_with_json_api-0.0.11 lib/fun_with_json_api/deserializer_config_builder.rb
fun_with_json_api-0.0.10.4 lib/fun_with_json_api/deserializer_config_builder.rb
fun_with_json_api-0.0.10.3 lib/fun_with_json_api/deserializer_config_builder.rb
fun_with_json_api-0.0.10.2 lib/fun_with_json_api/deserializer_config_builder.rb
fun_with_json_api-0.0.10.1 lib/fun_with_json_api/deserializer_config_builder.rb
fun_with_json_api-0.0.10 lib/fun_with_json_api/deserializer_config_builder.rb
fun_with_json_api-0.0.9 lib/fun_with_json_api/deserializer_config_builder.rb
fun_with_json_api-0.0.8.2 lib/fun_with_json_api/deserializer_config_builder.rb
fun_with_json_api-0.0.8.1 lib/fun_with_json_api/deserializer_config_builder.rb
fun_with_json_api-0.0.8 lib/fun_with_json_api/deserializer_config_builder.rb
fun_with_json_api-0.0.7 lib/fun_with_json_api/deserializer_config_builder.rb
fun_with_json_api-0.0.6.pre.alpha.2 lib/fun_with_json_api/deserializer_config_builder.rb
fun_with_json_api-0.0.6.pre.alpha.1 lib/fun_with_json_api/deserializer_config_builder.rb
fun_with_json_api-0.0.6.1 lib/fun_with_json_api/deserializer_config_builder.rb
fun_with_json_api-0.0.6 lib/fun_with_json_api/deserializer_config_builder.rb
fun_with_json_api-0.0.5 lib/fun_with_json_api/deserializer_config_builder.rb
fun_with_json_api-0.0.4 lib/fun_with_json_api/deserializer_config_builder.rb
fun_with_json_api-0.0.3 lib/fun_with_json_api/deserializer_config_builder.rb
fun_with_json_api-0.0.2 lib/fun_with_json_api/deserializer_config_builder.rb