Class: YAS::AttributeExt::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/yas/ext/attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#nameObject (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.

Examples:

alter { |v| v == "John" }

Yields:

  • (v)

    Value of the Attribute.



131
132
133
134
# File 'lib/yas/ext/attribute.rb', line 131

def alter &block
  @alter_block = block
  self
end

#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.



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.

Yields:

  • Sets the value of this Attribute 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

Returns:

  • (Boolean)


106
107
108
# File 'lib/yas/ext/attribute.rb', line 106

def has_default?
  @default_block != nil
end

#requiredObject

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

Returns:

  • (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.

Returns:

  • The final value after the validation.



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.

Examples:

validate_value { |v| v == "John" }

Yields:

  • (v)

    Value of the Attribute.



118
119
120
121
# File 'lib/yas/ext/attribute.rb', line 118

def validate_value &block
  @validate_value_block = block
  self
end