Sha256: 55fda0fe9f3ea1f42307e6dbcc318aec657f2da5c4155fad5bd16a24e5949bb2

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 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 attributes(*names)
        names.each { |name| add_attribute(name) }
      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

2 entries across 2 versions & 1 rubygems

Version Path
jsonapi-serializers-0.5.0 lib/jsonapi-serializers/attributes.rb
jsonapi-serializers-0.4.0 lib/jsonapi-serializers/attributes.rb