class Opto::Type
Defines a type handler. Used as a base from which to inherit in the type handlers.
Constants
- GLOBAL_OPTIONS
Attributes
options[RW]
Public Class Methods
for(type_name)
click to toggle source
Find a type handler for :type_name, for example: Opto::Type.for
(:string) @param [String,Symbol] type_name
# File lib/opto/type.rb, line 33 def for(type_name) raise NameError, "No handler for type #{type_name}" unless types[type_name] types[type_name] end
inherited(where)
click to toggle source
# File lib/opto/type.rb, line 19 def inherited(where) types[where.type] = where end
new(options = {})
click to toggle source
# File lib/opto/type.rb, line 92 def initialize(options = {}) @options = Type::GLOBAL_OPTIONS.merge(self.class.const_defined?(:OPTIONS) ? self.class.const_get(:OPTIONS) : {}).merge(options.symbolize_keys) end
sanitizer(name, &block)
click to toggle source
Define a sanitizer. Can be used to for example convert strings to integers or to remove whitespace, etc.
@example
class Foo < Opto::Type sanitizer :add_suffix |value| value.to_s + "-1" end end
# File lib/opto/type.rb, line 69 def sanitizer(name, &block) raise TypeError, "Block required" unless block_given? sanitizers << define_method("sanitize_#{name}", &block) end
sanitizers()
click to toggle source
# File lib/opto/type.rb, line 56 def sanitizers @sanitizers ||= [] end
true_when(&block)
click to toggle source
# File lib/opto/type.rb, line 74 def true_when(&block) raise TypeError, "Block required" unless block_given? define_method(:truthy?, &block) end
type()
click to toggle source
# File lib/opto/type.rb, line 27 def type name.to_s.split('::').last.snakecase.to_sym end
types()
click to toggle source
# File lib/opto/type.rb, line 23 def types @types ||= {} end
validator(name, &block)
click to toggle source
Define a validator: @example
class Foo < Opto::Type validator :is_foo do |value| unless value == 'foo' "Foo is not foo." end end end
# File lib/opto/type.rb, line 51 def validator(name, &block) raise TypeError, "Block required" unless block_given? validators << define_method("validate_#{name}", &block) end
validators()
click to toggle source
# File lib/opto/type.rb, line 38 def validators @validators ||= [] end
Public Instance Methods
errors()
click to toggle source
# File lib/opto/type.rb, line 116 def errors @errors ||= {} end
required?()
click to toggle source
# File lib/opto/type.rb, line 100 def required? !!options[:required] end
sanitize(value)
click to toggle source
# File lib/opto/type.rb, line 104 def sanitize(value) new_value = value self.class.sanitizers.each do |sanitizer| begin new_value = self.send(sanitizer, new_value) rescue StandardError => ex raise ex, "Sanitizer #{sanitizer} : #{ex.message}" end end new_value end
type()
click to toggle source
# File lib/opto/type.rb, line 96 def type self.class.type end
valid?(value)
click to toggle source
# File lib/opto/type.rb, line 120 def valid?(value) validate(value) errors.empty? end
validate(value)
click to toggle source
# File lib/opto/type.rb, line 125 def validate(value) errors.clear if value.nil? errors[:presence] = "Required value missing" if required? else (Type.validators + self.class.validators).each do |validator| begin result = self.send(validator, value) rescue StandardError => ex raise ex, "Validator #{validator} : #{ex.message}" end unless result.kind_of?(NilClass) || result.kind_of?(TrueClass) errors[validator] = result end end end end