Sha256: dc5dd813bb38f14d99e356d933e87b21638d1352c554526233237348ed1dfe37

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module Stevenson
  module Input
    autoload :Email, 'stevenson/input/email'
    autoload :Password, 'stevenson/input/password'
    autoload :Select, 'stevenson/input/select'
    autoload :Text, 'stevenson/input/text'
    autoload :Url, 'stevenson/input/url'

    module Base
      attr_reader :options

      def self.included(input)
        input.extend ClassMethods

        Stevenson.inputs[input.input_name] = input
      end

      module ClassMethods
        def input_name
          name.gsub(/^.*::/, '').downcase.to_sym
        end
      end

      def initialize(options, default=nil)
        @options, @default = options, default
      end

      def collect!
        raise NotImplementedError
      end

      def default
        @default ||= options['default'] if options['default']
        @default ||=  ''
      end
    end

    def self.input_for(options)
      input_klass = input_klass_for(options['type'])
      input_klass.new(options)
    end

    private

    def self.input_klass_for(type)
      Stevenson.inputs[type] || const_get(type.to_s.capitalize)
    rescue NameError => e
      raise NameError.new "Type '#{type}' is not a valid input type.", e
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stevenson-1.0.1 lib/stevenson/input.rb