Class: Convection::Model::Template::Resource::PropertyInstance
- Inherits:
-
Object
- Object
- Convection::Model::Template::Resource::PropertyInstance
- Defined in:
- lib/convection/model/template/resource.rb
Overview
An instance of a poperty in a resoruce
Direct Known Subclasses
HashPropertyInstance, ListPropertyInstance, ScalarPropertyInstance
Instance Attribute Summary collapse
-
#current_value ⇒ Object
Returns the value of attribute current_value.
-
#property ⇒ Object
readonly
Returns the value of attribute property.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #current(val) ⇒ Object
- #default ⇒ Object
-
#initialize(resource, property = nil) ⇒ PropertyInstance
constructor
A new instance of PropertyInstance.
- #transform(value) ⇒ Object
- #validate!(value) ⇒ Object
Constructor Details
#initialize(resource, property = nil) ⇒ PropertyInstance
Returns a new instance of PropertyInstance
96 97 98 99 |
# File 'lib/convection/model/template/resource.rb', line 96 def initialize(resource, property = nil) @resource = resource @property = property end |
Instance Attribute Details
#current_value ⇒ Object
Returns the value of attribute current_value
94 95 96 |
# File 'lib/convection/model/template/resource.rb', line 94 def current_value @current_value end |
#property ⇒ Object (readonly)
Returns the value of attribute property
92 93 94 |
# File 'lib/convection/model/template/resource.rb', line 92 def property @property end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource
91 92 93 |
# File 'lib/convection/model/template/resource.rb', line 91 def resource @resource end |
#value ⇒ Object (readonly)
Returns the value of attribute value
93 94 95 |
# File 'lib/convection/model/template/resource.rb', line 93 def value @value end |
Instance Method Details
#current(val) ⇒ Object
142 143 144 |
# File 'lib/convection/model/template/resource.rb', line 142 def current(val) @current_value = @value = val end |
#default ⇒ Object
137 138 139 140 |
# File 'lib/convection/model/template/resource.rb', line 137 def default return if property.nil? property.default end |
#transform(value) ⇒ Object
101 102 103 104 |
# File 'lib/convection/model/template/resource.rb', line 101 def transform(value) return value if property.nil? property.transform.inject(value) { |a, e| resource.instance_exec(a, &e) } end |
#validate!(value) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/convection/model/template/resource.rb', line 106 def validate!(value) return value if property.nil? if resource.exist? && property.immutable && current_value != value fail ArgumentError, "Property #{ property.name } is immutable!" end if property.required && value.nil? fail ArgumentError, "Property #{ property.name } is required!" end unless property.equal_to.empty? || property.equal_to.include?(value) fail ArgumentError, "Property #{ property.name } must be one of #{ property.equal_to.join(', ') }!" end unless property.kind_of.empty? || property.kind_of.any? { |t| value.is_a?(t) } fail ArgumentError, "Property #{ property.name } must be one of #{ property.kind_of.join(', ') }!" end unless !property.regex || property.regex.match(value.to_s) fail ArgumentError, "Property #{ property.name } must match #{ property.regex.inspect }!" end value end |