Sha256: c9395ee83b02a3aeecbd1c75e3b2ddc9862a62f2cac6a3f1fce32d2cd8ee3640

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 KB

Contents

module JSONAPI
  class Association
    def initialize(name, options={})
      @name          = name.to_s
      @options       = options
      @key           = options[:key] ? options[:key].to_sym : nil
      @primary_key   = options.fetch(:primary_key, 'id').to_sym
      @acts_as_set   = options.fetch(:acts_as_set, false) == true
    end

    def key
      @key
    end

    def primary_key
      @primary_key
    end

    def acts_as_set
      @acts_as_set
    end

    def serialize_type_name
      @serialize_type_name
    end

    class HasOne < Association
      def initialize(name, options={})
        super
        class_name = options.fetch(:class_name, name.to_s.capitalize)
        @serialize_type_name = class_name.underscore.pluralize.to_sym
        @key ||= "#{name}_id".to_sym
      end
    end

    class HasMany < Association
      def initialize(name, options={})
        super
        class_name = options.fetch(:class_name, name.to_s.capitalize.singularize).to_sym
        @serialize_type_name = class_name.to_s.underscore.pluralize.to_sym
        @key ||= "#{name.to_s.singularize}_ids".to_sym
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jsonapi-resources-0.0.4 lib/jsonapi/association.rb
jsonapi-resources-0.0.3 lib/jsonapi/association.rb
jsonapi-resources-0.0.2 lib/jsonapi/association.rb