module Struct::Validatable::SubclassClassMethods
Public Instance Methods
WHEN(pattern, adjuster)
click to toggle source
Adjuster Builders Apply adjuster when passed pattern. @param pattern [Proc, Method, ===] @param adjuster [Proc, to_proc] @return [Proc]
# File lib/struct/validatable/subclass_class_methods.rb, line 80 def WHEN(pattern, adjuster) unless Eqq.valid?(pattern) raise ArgumentError, 'wrong object for pattern' end unless adjustable?(adjuster) raise ArgumentError, 'wrong object for adjuster' end ->v { _valid?(pattern, v) ? adjuster.call(v) : v } end
adjustable?(proc)
click to toggle source
@param [Proc] proc
# File lib/struct/validatable/subclass_class_methods.rb, line 69 def adjustable?(proc) (Proc === proc) && (proc.arity.abs == 1) end
adjuster_for(key)
click to toggle source
@param [Symbol, String, to_sym, Integer, to_int] key
# File lib/struct/validatable/subclass_class_methods.rb, line 22 def adjuster_for(key) @adjusters.fetch(autonym_for_key(key)) end
autonym_for_index(index)
click to toggle source
@param [Integer, to_int] index
# File lib/struct/validatable/subclass_class_methods.rb, line 53 def autonym_for_index(index) members.fetch(index.to_int) end
autonym_for_key(key)
click to toggle source
@param [Symbol, String, to_sym, Integer, to_int] key
# File lib/struct/validatable/subclass_class_methods.rb, line 32 def autonym_for_key(key) case key when ->k { k.respond_to?(:to_sym) } autonym_for_member(key) when ->k { k.respond_to?(:to_int) } members.fetch(key.to_int) else raise TypeError, key end end
autonym_for_member(name)
click to toggle source
@param [Symbol, String, to_sym] name
# File lib/struct/validatable/subclass_class_methods.rb, line 44 def autonym_for_member(name) name = name.to_sym raise NameError, name unless members.include?(name) name end
condition_for(key)
click to toggle source
@param [Symbol, String, to_sym, Integer, to_int] key
# File lib/struct/validatable/subclass_class_methods.rb, line 10 def condition_for(key) @conditions.fetch(autonym_for_key(key)) end
conditionable?(obj)
click to toggle source
# File lib/struct/validatable/subclass_class_methods.rb, line 57 def conditionable?(obj) case obj when Proc obj.arity.abs == 1 when Method obj.arity.abs <= 1 else obj.respond_to?(:===) end end
with_adjuster?(key)
click to toggle source
@param [Symbol, String, to_sym, Integer, to_int] key
# File lib/struct/validatable/subclass_class_methods.rb, line 27 def with_adjuster?(key) @adjusters.key?(autonym_for_key(key)) end
with_condition?(key)
click to toggle source
@param [Symbol, String, to_sym, Integer, to_int] key
# File lib/struct/validatable/subclass_class_methods.rb, line 15 def with_condition?(key) @conditions.key?(autonym_for_key(key)) end
Also aliased as: restrict?
Private Instance Methods
validator(key, condition=BasicObject, &adjuster)
click to toggle source
@param [Symbol, String, to_sym, Integer, to_int] key @param [Proc, Method, ===] condition @return [nil] @yield [value] @yieldreturn [nil]
# File lib/struct/validatable/subclass_class_methods.rb, line 101 def validator(key, condition=BasicObject, &adjuster) autonym = autonym_for_key(key) raise NameError unless members.include?(autonym) if conditionable?(condition) @conditions[autonym] = condition else raise TypeError, 'invalid condition thrown' end if adjuster if adjustable?(adjuster) @adjusters[autonym] = adjuster else raise TypeError, 'invalid adjuster thrown' end end setter = :"#{autonym}=" alias_method(:"_original_setter_#{setter}", setter) undef_method(setter) define_method(setter) do |value| self[autonym] = value end nil end