Sha256: 826a773b62118f8d420b9509389bbfb2fcdd3e26fc7e41dbddb79b25a91a2e33

Contents?: true

Size: 1.84 KB

Versions: 6

Compression:

Stored size: 1.84 KB

Contents

module HyperResource::Modules
  module InternalAttributes

    def self.included(base)
      base.extend(ClassMethods)

      base._hr_class_attributes.each do |attr|
        base._hr_class_attribute attr
      end

      (base._hr_attributes - base._hr_class_attributes).each do |attr|
         base.send(:attr_accessor, attr)
      end

      ## Fallback attributes fall back from instance to class.
      (base._hr_attributes & base._hr_class_attributes).each do |attr|
         base._hr_fallback_attribute attr
      end
    end

    module ClassMethods
      # @private
      def _hr_class_attributes
        [ :root ]
      end

      # @private
      def _hr_attributes
        [ :root,
          :href,
          :request,
          :response,
          :body,
          :attributes,
          :links,
          :objects,
          :loaded
        ]
      end

      ## Inheritable class attribute, kinda like in Rails.
      # @private
      def _hr_class_attribute(*names)
        names.map(&:to_sym).each do |name|
          instance_eval <<-EOT
            def #{name}=(val)
              @#{name} = val
            end
            def #{name}
              return @#{name} if defined?(@#{name})
              return superclass.#{name} if superclass.respond_to?(:#{name})
              nil
            end
          EOT
        end
      end

      ## Instance attributes which fall back to class attributes.
      # @private
      def _hr_fallback_attribute(*names)
        names.map(&:to_sym).each do |name|
          class_eval <<-EOT
            def #{name}=(val)
              @#{name} = val
            end
            def #{name}
              return @#{name} if defined?(@#{name})
              return self.class.#{name} if self.class.respond_to?(:#{name})
              nil
            end
          EOT
        end
      end

    end # ClassMethods

  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
hyperresource-0.9.4 lib/hyper_resource/modules/internal_attributes.rb
hyperresource-0.9.3 lib/hyper_resource/modules/internal_attributes.rb
hyperresource-0.9.2 lib/hyper_resource/modules/internal_attributes.rb
hyperresource-0.9.1 lib/hyper_resource/modules/internal_attributes.rb
hyperresource_zuhrig-0.1.0 lib/hyper_resource/modules/internal_attributes.rb
hyperresource-0.9.0 lib/hyper_resource/modules/internal_attributes.rb