Sha256: 5c38c71338df61a816aad9e2cb55ab5891c67eaba4384237b01535483a095b5c

Contents?: true

Size: 1.09 KB

Versions: 10

Compression:

Stored size: 1.09 KB

Contents

require 'valise/errors'

module Valise
  module Strategies
    class Set
      class << self
        def root_class(&block)
          if superclass == Valise::Strategies::Set
            instance_eval(&block)
          elsif self == Valise::Strategies::Set
            raise "Complete class hierarchy fail"
          else
            superclass.root_class(&block)
          end
        end

        def classes
          root_class{@classes ||= {}}
        end

        def instances
          root_class{@instances ||= {}}
        end

        def [](index)
          classes[index]
        end

        def register(index)
          classes[index] = self
        end

        def set_default(klass)
          classes.default = klass
        end

        def default
          set_default(self)
        end

        def check!(type)
          classes.fetch(type)
        rescue KeyError
          raise Errors::UnregisteredStrategy.new(self.name, type)
        end

        def instance(type, options = nil)
          instances[[type, options]] ||= classes[type].new(options)
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
valise-1.2.1 lib/valise/strategies/set.rb
valise-1.2.0 lib/valise/strategies/set.rb
valise-1.1.4 lib/valise/strategies/set.rb
valise-1.1.3 lib/valise/strategies/set.rb
valise-1.1.2 lib/valise/strategies/set.rb
valise-1.1.1 lib/valise/strategies/set.rb
valise-1.1.0 lib/valise/strategies/set.rb
valise-1.0.0 lib/valise/strategies/set.rb
valise-0.9.1 lib/valise/strategies/set.rb
valise-0.9.0 lib/valise/strategies/set.rb