Sha256: 8a07eed2f240e0594e840b465305d98b708d228f39882b82bfdf343b3958c5b3

Contents?: true

Size: 976 Bytes

Versions: 13

Compression:

Stored size: 976 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::Store)
        model.store(:memory, {})
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
toystore-0.8.3 spec/support/constants.rb
toystore-0.8.2 spec/support/constants.rb
toystore-0.8.1 spec/support/constants.rb
toystore-0.8.0 spec/support/constants.rb
toystore-0.7.0 spec/support/constants.rb
toystore-0.6.6 spec/support/constants.rb
toystore-0.6.5 spec/support/constants.rb
toystore-0.6.4 spec/support/constants.rb
toystore-0.6.3 spec/support/constants.rb
toystore-0.6.2 spec/support/constants.rb
toystore-0.6.1 spec/support/constants.rb
toystore-0.6 spec/support/constants.rb
toystore-0.5 spec/support/constants.rb