Class: Ducktape::BindableAttributeMetadata
- Inherits:
-
Object
- Object
- Ducktape::BindableAttributeMetadata
- Defined in:
- lib/ducktape/bindable_attribute_metadata.rb
Constant Summary collapse
- VALID_OPTIONS =
[:access, :coerce, :default, :getter, :setter, :validate].freeze
Instance Attribute Summary collapse
-
#access ⇒ Object
readonly
Returns the value of attribute access.
-
#getter ⇒ Object
readonly
Returns the value of attribute getter.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#setter ⇒ Object
readonly
Returns the value of attribute setter.
Class Method Summary collapse
Instance Method Summary collapse
- #coerce(owner, value) ⇒ Object
- #coercion(&block) ⇒ Object
- #default ⇒ Object
- #default=(value) ⇒ Object
- #getter_proc ⇒ Object
-
#initialize(name, options = {}) ⇒ BindableAttributeMetadata
constructor
A new instance of BindableAttributeMetadata.
- #setter_proc ⇒ Object
- #valid?(value) ⇒ Boolean
- #validate(value) ⇒ Object
- #validation(*validators, &block) ⇒ Object
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, = {}) .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?(BindableAttributeMetadata) @name = name.name = name.send(:as_options).merge!() else @name = name end @default = [:default] @validation = validation(*[:validate]) @coercion = [:coerce] || ->(_owner, value) { value } @access = [:access] || :both @getter = [:getter] @setter = [:setter] end |
Instance Attribute Details
#access ⇒ Object (readonly)
Returns the value of attribute access.
8 9 10 |
# File 'lib/ducktape/bindable_attribute_metadata.rb', line 8 def access @access end |
#getter ⇒ Object (readonly)
Returns the value of attribute getter.
8 9 10 |
# File 'lib/ducktape/bindable_attribute_metadata.rb', line 8 def getter @getter end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/ducktape/bindable_attribute_metadata.rb', line 8 def name @name end |
#setter ⇒ Object (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 |
#default ⇒ Object
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_proc ⇒ Object
38 39 40 |
# File 'lib/ducktape/bindable_attribute_metadata.rb', line 38 def getter_proc self.class.getter_proc(@getter, @name) end |
#setter_proc ⇒ Object
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
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 |