Sha256: 9226d737ff8f1560e07ee2ced54e44322a566b9616958408aa3fa74456a12a2f

Contents?: true

Size: 1.62 KB

Versions: 11

Compression:

Stored size: 1.62 KB

Contents

module JSONAPI
  module Attributes
    def self.included(target)
      target.send(:include, InstanceMethods)
      target.extend ClassMethods
    end

    module InstanceMethods
    end

    module ClassMethods
      attr_accessor :attributes_map
      attr_accessor :to_one_associations
      attr_accessor :to_many_associations

      def attribute(name, options = {}, &block)
        add_attribute(name, options, &block)
      end

      def has_one(name, options = {}, &block)
        add_to_one_association(name, options, &block)
      end

      def has_many(name, options = {}, &block)
        add_to_many_association(name, options, &block)
      end

      def add_attribute(name, options = {}, &block)
        # Blocks are optional and can override the default attribute discovery. They are just
        # stored here, but evaluated by the Serializer within the instance context.
        @attributes_map ||= {}
        @attributes_map[name] = {
          attr_or_block: block_given? ? block : name,
          options: options,
        }
      end
      private :add_attribute

      def add_to_one_association(name, options = {}, &block)
        @to_one_associations ||= {}
        @to_one_associations[name] = {
          attr_or_block: block_given? ? block : name,
          options: options,
        }
      end
      private :add_to_one_association

      def add_to_many_association(name, options = {}, &block)
        @to_many_associations ||= {}
        @to_many_associations[name] = {
          attr_or_block: block_given? ? block : name,
          options: options,
        }
      end
      private :add_to_many_association
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
jsonapi-serializers-0.3.1 lib/jsonapi-serializers/attributes.rb
jsonapi-serializers-0.3.0 lib/jsonapi-serializers/attributes.rb
jsonapi-serializers-0.2.6 lib/jsonapi-serializers/attributes.rb
jsonapi-serializers-0.2.5 lib/jsonapi-serializers/attributes.rb
jsonapi-serializers-0.2.4 lib/jsonapi-serializers/attributes.rb
jsonapi-serializers-0.2.3 lib/jsonapi-serializers/attributes.rb
jsonapi-serializers-0.2.2 lib/jsonapi-serializers/attributes.rb
jsonapi-serializers-0.2.1 lib/jsonapi-serializers/attributes.rb
jsonapi-serializers-0.2.0 lib/jsonapi-serializers/attributes.rb
jsonapi-serializers-0.1.2 lib/jsonapi-serializers/attributes.rb
jsonapi-serializers-0.1.1 lib/jsonapi-serializers/attributes.rb