Class: Convection::Model::Template::Resource::PropertyInstance

Inherits:
Object
  • Object
show all
Defined in:
lib/convection/model/template/resource.rb

Overview

An instance of a poperty in a resoruce

Instance Attribute Summary collapse

Instance Method Summary collapse

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_valueObject

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

#propertyObject (readonly)

Returns the value of attribute property



92
93
94
# File 'lib/convection/model/template/resource.rb', line 92

def property
  @property
end

#resourceObject (readonly)

Returns the value of attribute resource



91
92
93
# File 'lib/convection/model/template/resource.rb', line 91

def resource
  @resource
end

#valueObject (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

#defaultObject



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