class Convection::Model::Template::Resource::PropertyInstance

An instance of a poperty in a resoruce

Attributes

current_value[RW]
property[R]
resource[R]
value[R]

Public Class Methods

new(resource, property = nil) click to toggle source
# File lib/convection/model/template/resource.rb, line 96
def initialize(resource, property = nil)
  @resource = resource
  @property = property
end

Public Instance Methods

current(val) click to toggle source
# File lib/convection/model/template/resource.rb, line 142
def current(val)
  @current_value = @value = val
end
default() click to toggle source
# File lib/convection/model/template/resource.rb, line 137
def default
  return if property.nil?
  property.default
end
transform(value) click to toggle source
# 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) click to toggle source
# 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