Class: Ducktape::BindableAttributeMetadata

Inherits:
Object
  • Object
show all
Defined in:
lib/ducktape/bindable_attribute_metadata.rb

Constant Summary collapse

VALID_OPTIONS =
[:access, :coerce, :default, :getter, :setter, :validate].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ BindableAttributeMetadata

Returns a new instance of BindableAttributeMetadata.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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." }

  if name.is_a?()
    @name   = name.name
    options = name.send(:as_options).merge!(options)
  else
    @name = name
  end

  @default    = options[:default]
  @validation = validation(*options[:validate])
  @coercion   = options[:coerce] || ->(_owner, value) { value }
  @access     = options[:access] || :both
  @getter     = options[:getter]
  @setter     = options[:setter]
end

Instance Attribute Details

#accessObject (readonly)

Returns the value of attribute access.



8
9
10
# File 'lib/ducktape/bindable_attribute_metadata.rb', line 8

def access
  @access
end

#getterObject (readonly)

Returns the value of attribute getter.



8
9
10
# File 'lib/ducktape/bindable_attribute_metadata.rb', line 8

def getter
  @getter
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/ducktape/bindable_attribute_metadata.rb', line 8

def name
  @name
end

#setterObject (readonly)

Returns the value of attribute setter.



8
9
10
# File 'lib/ducktape/bindable_attribute_metadata.rb', line 8

def setter
  @setter
end

Class Method Details

.register_validator(validator_class) ⇒ Object



70
71
72
# File 'lib/ducktape/bindable_attribute_metadata.rb', line 70

def self.register_validator(validator_class)
  @validators << validator_class
end

Instance Method Details

#coerce(owner, value) ⇒ Object



66
67
68
# File 'lib/ducktape/bindable_attribute_metadata.rb', line 66

def coerce(owner, value)
  @coercion ? @coercion.(owner, value) : value
end

#coercion(&block) ⇒ Object



62
63
64
# File 'lib/ducktape/bindable_attribute_metadata.rb', line 62

def coercion(&block)
  @coercion = block
end

#defaultObject



34
35
36
# File 'lib/ducktape/bindable_attribute_metadata.rb', line 34

def default
  @default.respond_to?(:call) ? @default.call : @default
end

#default=(value) ⇒ Object



30
31
32
# File 'lib/ducktape/bindable_attribute_metadata.rb', line 30

def default=(value)
  @default = value
end

#getter_procObject



38
39
40
# File 'lib/ducktape/bindable_attribute_metadata.rb', line 38

def getter_proc
  self.class.getter_proc(@getter, @name)
end

#setter_procObject



42
43
44
# File 'lib/ducktape/bindable_attribute_metadata.rb', line 42

def setter_proc
  self.class.setter_proc(@setter, @name)
end

#valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/ducktape/bindable_attribute_metadata.rb', line 54

def valid?(value)
  @validation.empty? || @validation.any? { |validator| validator.validate(value) }
end

#validate(value) ⇒ Object



58
59
60
# File 'lib/ducktape/bindable_attribute_metadata.rb', line 58

def validate(value)
  raise InvalidAttributeValueError.new(@name, value) unless valid?(value)
end

#validation(*validators, &block) ⇒ Object



46
47
48
49
50
51
52
# 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