Sha256: c7e75a4bb763b75ebaaf34444cbdb621bd0c79ba04bb1724dc5179520f0a567b

Contents?: true

Size: 1.33 KB

Versions: 12

Compression:

Stored size: 1.33 KB

Contents

module JSONAPI
  class Association
    attr_reader :acts_as_set, :foreign_key, :type, :options, :name, :class_name

    def initialize(name, options={})
      @name                = name.to_s
      @options             = options
      @acts_as_set         = options.fetch(:acts_as_set, false) == true
      @key                 = options[:key] ? options[:key].to_sym : nil

      if @key.nil?
        @foreign_key  = options[:foreign_key ] ? options[:foreign_key ].to_sym : nil
      else
        # :nocov:
        warn '[DEPRECATION] `key` is deprecated in associations.  Please use `foreign_key` instead.'
        # :nocov:
      end
    end

    def primary_key
      @primary_key ||= Resource.resource_for(@name)._primary_key
    end

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

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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
jsonapi-resources-0.2.0 lib/jsonapi/association.rb
jsonapi-resources-0.1.1 lib/jsonapi/association.rb
jsonapi-resources-0.1.0 lib/jsonapi/association.rb
jsonapi-resources-0.0.16 lib/jsonapi/association.rb
jsonapi-resources-0.0.15 lib/jsonapi/association.rb
jsonapi-resources-0.0.14 lib/jsonapi/association.rb
jsonapi-resources-0.0.13 lib/jsonapi/association.rb
jsonapi-resources-0.0.12 lib/jsonapi/association.rb
jsonapi-resources-0.0.11 lib/jsonapi/association.rb
jsonapi-resources-0.0.10 lib/jsonapi/association.rb
jsonapi-resources-0.0.9 lib/jsonapi/association.rb
jsonapi-resources-0.0.8 lib/jsonapi/association.rb