Class: CouchPillow::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/couchpillow/directives/attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Attribute

Returns a new instance of Attribute.



45
46
47
# File 'lib/couchpillow/directives/attribute.rb', line 45

def initialize name
  @name = name.to_s.to_sym
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/couchpillow/directives/attribute.rb', line 36

def name
  @name
end

Instance Method Details

#auto_convertObject

Attempts to auto convert values to the type specified by the #type directive if the value is not of the same type. Has no effect if #type is not specified.



75
76
77
78
# File 'lib/couchpillow/directives/attribute.rb', line 75

def auto_convert
  @auto_convert = true
  self
end

#content {|v| ... } ⇒ Object Also known as: validate_content

Directive to perform a validation over the value of this Attribute.

Examples:

content { |v| v == "John" }

Yields:

  • (v)

    Value of the Attribute.



107
108
109
110
# File 'lib/couchpillow/directives/attribute.rb', line 107

def content &block
  @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.

Yields:

  • Sets the value of this Attribute to the value returned by the block.



89
90
91
92
# File 'lib/couchpillow/directives/attribute.rb', line 89

def default &block
  @default_block = block
  self
end

#has_default?Boolean

Returns:



95
96
97
# File 'lib/couchpillow/directives/attribute.rb', line 95

def has_default?
  @default_block != nil
end

#requiredObject

Directive to mark this Attribute as required.



52
53
54
55
# File 'lib/couchpillow/directives/attribute.rb', line 52

def required
  @required = true
  self
end

#required?Boolean

Returns:



58
59
60
# File 'lib/couchpillow/directives/attribute.rb', line 58

def required?
  @required
end

#trigger_auto_convert_directive(value) ⇒ Object

Auto convert the value



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/couchpillow/directives/attribute.rb', line 155

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.



125
126
127
128
129
130
131
# File 'lib/couchpillow/directives/attribute.rb', line 125

def trigger_content_directive value
  if @check_value_block
    raise ValidationError, "Content validation error: #{value}" unless
      @check_value_block.call(value)
  end
  value
end

#trigger_default_directiveObject

Check the default value.

Parameters:

  • value

    The value of this attribute to validate.



118
119
120
# File 'lib/couchpillow/directives/attribute.rb', line 118

def trigger_default_directive
  @default_block.call if has_default?
end

#trigger_type_directive(value) ⇒ Object

Check type.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/couchpillow/directives/attribute.rb', line 136

def trigger_type_directive value
  if @type
    # Run auto-conversion first.
    value = trigger_auto_convert_directive(value)

    msg = "Type mismatch for attribute #{@name}"
    if @type == CouchPillow::Boolean
      raise ValidationError, msg unless !!value == value
    else
      raise ValidationError, msg unless value.is_a?(@type)
    end
  end

  value
end

#type(t) ⇒ Object

Directive to enforce the data type of this Attribute.



65
66
67
68
# File 'lib/couchpillow/directives/attribute.rb', line 65

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.

Returns:

  • The final value after the validation.



181
182
183
184
# File 'lib/couchpillow/directives/attribute.rb', line 181

def validate value
  value = trigger_default_directive if value.nil?
  trigger_content_directive(trigger_type_directive(value))
end