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_sym

        predefined_attributes[name] = attributes

        class_eval %{
          def self.#{name}
            self[:#{name}]
          end
        }

        attributes
      end
    end # ClassMethods
  end # Predefined
end # DataMapper

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 %{
          define_method(#{name.dump}) do
            self[#{name.dump}]
          end
        }

        attributes
      end
    end # ClassMethods
  end # Predefined
end # DataMapper

Use better escaping of Strings when defining methods.
Use better escaping of Strings when defining methods.
