Sha256: fcc900318ca7fdb4a1e09ac6adde00d71012a559b435b919418ee61d7b51f471

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

require 'dm-predefined/exceptions/unknown_resource'

module DataMapper
  module Predefined
    ##
    # fired when your plugin gets included into Resource
    #
    def self.included(base)
      base.extend DataMapper::Predefined::ClassMethods
    end

    module ClassMethods
      def [](name)
        name = name.to_sym
        attributes = predefined_attributes[name]

        unless attributes
          raise(UnknownResource,"the resource '#{name}' was not predefined",caller)
        end

        first_or_create(predefined_attributes[name.to_sym])
      end

      protected

      def predefined_attributes
        @@predefined_attributes ||= {}
      end

      def define(name,attributes={})
        name = name.to_s

        predefined_attributes[name.to_sym] = attributes

        class_eval %{
          class << self
            define_method(#{name.dump}) do
              self[#{name.dump}]
            end
          end
        }

        attributes
      end
    end # ClassMethods
  end # Predefined
end # DataMapper

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dm-predefined-0.1.1 lib/dm-predefined/predefined.rb
dm-predefined-0.1.0 lib/dm-predefined/predefined.rb
dm-predefined-0.0.1 lib/dm-predefined/predefined.rb