Class: YAS::AttributeExt::Attribute
- Inherits:
-
Object
- Object
- YAS::AttributeExt::Attribute
- Defined in:
- lib/yas/ext/attribute.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#alter {|v| ... } ⇒ Object
Perform adjustment to the value of this Attribute.
-
#auto_convert ⇒ Object
Attempts to auto convert values to the type specified by the #type directive if the value is not of the same type.
-
#default { ... } ⇒ Object
Directive to set the default value of this Attribute.
- #has_default? ⇒ Boolean
-
#initialize(name) ⇒ Attribute
constructor
A new instance of Attribute.
-
#required ⇒ Object
Directive to mark this Attribute as required.
- #required? ⇒ Boolean
-
#type(t) ⇒ Object
Directive to enforce the data type of this Attribute.
-
#validate(value) ⇒ Object
Validates attribute, except the required directive.
-
#validate_value {|v| ... } ⇒ Object
Perform a validation over the value of this Attribute.
Constructor Details
#initialize(name) ⇒ Attribute
Returns a new instance of Attribute.
56 57 58 |
# File 'lib/yas/ext/attribute.rb', line 56 def initialize name @name = name end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
46 47 48 |
# File 'lib/yas/ext/attribute.rb', line 46 def name @name end |
Instance Method Details
#alter {|v| ... } ⇒ Object
Perform adjustment to the value of this Attribute.
131 132 133 134 |
# File 'lib/yas/ext/attribute.rb', line 131 def alter &block @alter_block = block self end |
#auto_convert ⇒ Object
86 87 88 89 |
# File 'lib/yas/ext/attribute.rb', line 86 def auto_convert @auto_convert = true self end |
#default { ... } ⇒ Object
Directive to set the default value of this Attribute. Once specified, if this Attribute does not exist during the Document initialization, whether that’s from Document.get or Document#initialize, the value of the Attribute will be set to the value returned by the block.
100 101 102 103 |
# File 'lib/yas/ext/attribute.rb', line 100 def default &block @default_block = block self end |
#has_default? ⇒ Boolean
106 107 108 |
# File 'lib/yas/ext/attribute.rb', line 106 def has_default? @default_block != nil end |
#required ⇒ Object
Directive to mark this Attribute as required.
63 64 65 66 |
# File 'lib/yas/ext/attribute.rb', line 63 def required @required = true self end |
#required? ⇒ Boolean
69 70 71 |
# File 'lib/yas/ext/attribute.rb', line 69 def required? @required end |
#type(t) ⇒ Object
Directive to enforce the data type of this Attribute.
76 77 78 79 |
# File 'lib/yas/ext/attribute.rb', line 76 def type t @type = t self end |
#validate(value) ⇒ Object
Validates attribute, except the required directive. First it executes the #default directive, then #auto_convert to type, then #type validation, then finally the #validate directive.
143 144 145 146 |
# File 'lib/yas/ext/attribute.rb', line 143 def validate value value = trigger_default_directive if value.nil? trigger_validate_value_directive(trigger_alter_directive(trigger_type_directive(value))) end |
#validate_value {|v| ... } ⇒ Object
Perform a validation over the value of this Attribute.
118 119 120 121 |
# File 'lib/yas/ext/attribute.rb', line 118 def validate_value &block @validate_value_block = block self end |