Class: CouchPillow::Attribute
- Inherits:
-
Object
- Object
- CouchPillow::Attribute
- Defined in:
- lib/couchpillow/attribute.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#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.
-
#content(message = nil) {|v| ... } ⇒ Object
Directive to perform a validation over the value of this Attribute.
-
#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
-
#trigger_auto_convert_directive(value) ⇒ Object
Auto convert the value.
-
#trigger_content_directive(value) ⇒ Object
Check value.
-
#trigger_default_directive ⇒ Object
Check the default value.
-
#trigger_type_directive(value) ⇒ Object
Check type.
-
#type(t) ⇒ Object
Directive to enforce the data type of this Attribute.
-
#validate(value) ⇒ Object
Run the validation directives, except required directive.
Constructor Details
#initialize(name) ⇒ Attribute
Returns a new instance of Attribute.
15 16 17 |
# File 'lib/couchpillow/attribute.rb', line 15 def initialize name @name = name.to_s.to_sym end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/couchpillow/attribute.rb', line 5 def name @name end |
Instance Method Details
#auto_convert ⇒ Object
45 46 47 48 |
# File 'lib/couchpillow/attribute.rb', line 45 def auto_convert @auto_convert = true self end |
#content(message = nil) {|v| ... } ⇒ Object
Directive to perform a validation over the value of this Attribute.
79 80 81 82 83 |
# File 'lib/couchpillow/attribute.rb', line 79 def content = nil, &block @check_value_message = @check_value_block = block 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.
59 60 61 62 |
# File 'lib/couchpillow/attribute.rb', line 59 def default &block @default_block = block self end |
#has_default? ⇒ Boolean
65 66 67 |
# File 'lib/couchpillow/attribute.rb', line 65 def has_default? @default_block != nil end |
#required ⇒ Object
Directive to mark this Attribute as required.
22 23 24 25 |
# File 'lib/couchpillow/attribute.rb', line 22 def required @required = true self end |
#required? ⇒ Boolean
28 29 30 |
# File 'lib/couchpillow/attribute.rb', line 28 def required? @required end |
#trigger_auto_convert_directive(value) ⇒ Object
Auto convert the value
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/couchpillow/attribute.rb', line 126 def trigger_auto_convert_directive value if @auto_convert if @type == Integer value = Integer(value) elsif @type == Float value = Float(value) elsif @type == String value = String(value) elsif @type == Array value = Array(value) elsif @type == Time && !value.is_a?(Time) value = Time.parse(value) elsif @type == CouchPillow::Boolean value = value == 0 || value.to_s.downcase == "false" || !value ? false : true end end value end |
#trigger_content_directive(value) ⇒ Object
Check value.
97 98 99 100 101 102 103 |
# File 'lib/couchpillow/attribute.rb', line 97 def trigger_content_directive value if @check_value_block raise ValidationError, @check_value_message unless @check_value_block.call(value) end value end |
#trigger_default_directive ⇒ Object
Check the default value.
90 91 92 |
# File 'lib/couchpillow/attribute.rb', line 90 def trigger_default_directive @default_block.call if has_default? end |
#trigger_type_directive(value) ⇒ Object
Check type.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/couchpillow/attribute.rb', line 108 def trigger_type_directive value if @type # Run auto-conversion first. value = trigger_auto_convert_directive(value) if @type == CouchPillow::Boolean raise ValidationError unless !!value == value else raise ValidationError unless value.is_a?(@type) end end value end |
#type(t) ⇒ Object
Directive to enforce the data type of this Attribute.
35 36 37 38 |
# File 'lib/couchpillow/attribute.rb', line 35 def type t @type = t self end |
#validate(value) ⇒ Object
Run the validation directives, except required directive. First it executes the #default directive, then #auto_convert to type, then #type validation, then finally the #content directive.
152 153 154 155 |
# File 'lib/couchpillow/attribute.rb', line 152 def validate value value = trigger_default_directive if value.nil? trigger_content_directive(trigger_type_directive(value)) end |