class Ducktape::BindableAttributeMetadata
Constants
- VALID_OPTIONS
Attributes
access[R]
getter[R]
name[R]
setter[R]
Public Class Methods
new(name, options = {})
click to toggle source
# File lib/ducktape/bindable_attribute_metadata.rb, line 10 def initialize(name, options = {}) options.keys.reject { |k| VALID_OPTIONS.member?(k) }. each { |k| $stderr.puts "WARNING: invalid option #{k.inspect} for #{name.inspect} attribute. Will be ignored." } @name = if name.is_a?(BindableAttributeMetadata) options = name.send(:as_options).merge!(options) name.name else name end @default = options[:default] @validation = validation(*options[:validate]) @coercion = options[:coerce] @access = options[:access] || :both @getter = options[:getter] @setter = options[:setter] end
register_validator(validator_class)
click to toggle source
# File lib/ducktape/bindable_attribute_metadata.rb, line 71 def self.register_validator(validator_class) @validators.unshift validator_class end
Private Class Methods
create_validator(validator)
click to toggle source
# File lib/ducktape/bindable_attribute_metadata.rb, line 88 def self.create_validator(validator) validator_class = @validators.find { |validator_class| validator_class.matches?(validator) } validator_class.new(validator) end
getter_proc(getter, name)
click to toggle source
# File lib/ducktape/bindable_attribute_metadata.rb, line 93 def self.getter_proc(getter, name) case getter when Proc then getter when Symbol, String then ->() { send(getter) } when nil then ->() { get_value(name) } else raise ArgumentError, 'requires a Proc, a Symbol or nil' end end
setter_proc(setter, name)
click to toggle source
# File lib/ducktape/bindable_attribute_metadata.rb, line 102 def self.setter_proc(setter, name) case setter when Proc then setter when Symbol, String then ->(value) { send(setter, value) } when nil then ->(value) { set_value(name, value) } else raise ArgumentError, 'requires a Proc, a Symbol or nil' end end
Public Instance Methods
coerce(owner, value)
click to toggle source
# File lib/ducktape/bindable_attribute_metadata.rb, line 67 def coerce(owner, value) @coercion ? @coercion.(owner, value) : value end
coercion(proc_obj = nil, &block)
click to toggle source
# File lib/ducktape/bindable_attribute_metadata.rb, line 62 def coercion(proc_obj = nil, &block) raise ArgumentError, 'Expected only a parameter or a block, but both were passed.' if proc_obj && block @coercion = block || proc_obj end
default()
click to toggle source
# File lib/ducktape/bindable_attribute_metadata.rb, line 34 def default @default.respond_to?(:call) ? @default.call : @default end
default=(value)
click to toggle source
# File lib/ducktape/bindable_attribute_metadata.rb, line 30 def default=(value) @default = value end
getter_proc()
click to toggle source
# File lib/ducktape/bindable_attribute_metadata.rb, line 38 def getter_proc self.class.getter_proc(@getter, @name) end
setter_proc()
click to toggle source
# File lib/ducktape/bindable_attribute_metadata.rb, line 42 def setter_proc self.class.setter_proc(@setter, @name) end
valid?(value)
click to toggle source
# File lib/ducktape/bindable_attribute_metadata.rb, line 54 def valid?(value) @validation.empty? || @validation.any? { |validator| validator.validate(value) } end
validate(value)
click to toggle source
# File lib/ducktape/bindable_attribute_metadata.rb, line 58 def validate(value) raise InvalidAttributeValueError.new(@name, value) unless valid?(value) end
validation(*validators, &block)
click to toggle source
# File lib/ducktape/bindable_attribute_metadata.rb, line 46 def validation(*validators, &block) validators << block if block class_validators = Set.new(self.class.instance_variable_get(:@validators)) @validation = validators.map do |validator| class_validators.include?(validator.class) ? validator : self.class.create_validator(validator) end end
Private Instance Methods
as_options()
click to toggle source
# File lib/ducktape/bindable_attribute_metadata.rb, line 77 def as_options { default: @default, validate: @validation, coerce: @coercion, access: @access, getter: @getter, setter: @setter } end