Sha256: a6390a171b26bf5f215924a3a23d5fe222fac239179544bce53e8a209adae337

Contents?: true

Size: 978 Bytes

Versions: 3

Compression:

Stored size: 978 Bytes

Contents

module Support
  module Constants
    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods
      def uses_constants(*constants)
        before { create_constants(*constants) }
      end
    end

    def create_constants(*constants)
      constants.each { |constant| create_constant(constant) }
    end

    def remove_constants(*constants)
      constants.each { |constant| remove_constant(constant) }
    end

    def create_constant(constant)
      remove_constant(constant)
      Kernel.const_set(constant, Model(constant))
    end

    def remove_constant(constant)
      Kernel.send(:remove_const, constant) if Kernel.const_defined?(constant)
    end

    def Model(name=nil)
      Class.new.tap do |model|
        model.class_eval """
          def self.name; '#{name}' end
          def self.to_s; '#{name}' end
        """ if name
        model.send(:include, Toy::Mongo)
        model.store(:mongo, STORE)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
toystore-mongo-0.8.2 spec/support/constants.rb
toystore-mongo-0.8.1 spec/support/constants.rb
toystore-mongo-0.8.0 spec/support/constants.rb