summaryrefslogtreecommitdiff
path: root/lib/chef
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-06-03 14:05:46 -0700
committerJohn Keiser <john@johnkeiser.com>2015-06-23 15:23:01 -0700
commitc4257f9d016871c6b4243079542d803d5e7fa383 (patch)
tree89fe153347148f7643cd32abb46ad5033e645371 /lib/chef
parent2d4e68e84540c536508c9f33fc1e47c3d8ff60f6 (diff)
downloadchef-c4257f9d016871c6b4243079542d803d5e7fa383.tar.gz
Create property on resource, alias attribute to it
Diffstat (limited to 'lib/chef')
-rw-r--r--lib/chef/mixin/params_validate.rb21
-rw-r--r--lib/chef/resource.rb58
-rw-r--r--lib/chef/resource/lwrp_base.rb8
3 files changed, 71 insertions, 16 deletions
diff --git a/lib/chef/mixin/params_validate.rb b/lib/chef/mixin/params_validate.rb
index 78d72dc801..ccc313db32 100644
--- a/lib/chef/mixin/params_validate.rb
+++ b/lib/chef/mixin/params_validate.rb
@@ -136,7 +136,8 @@ class Chef
value = _pv_opts_lookup(opts, key)
unless value.nil?
passes = false
- Array(to_be).each do |tb|
+ to_be = Array(to_be)
+ to_be.each do |tb|
passes = true if value == tb
end
unless passes
@@ -150,7 +151,8 @@ class Chef
value = _pv_opts_lookup(opts, key)
unless value.nil?
passes = false
- Array(to_be).each do |tb|
+ to_be = Array(to_be)
+ to_be.each do |tb|
passes = true if value.kind_of?(tb)
end
unless passes
@@ -177,14 +179,16 @@ class Chef
#
# Note, this will *PASS* if the object doesn't respond to the method.
# So, to make sure a value is not nil and not blank, you need to do
- # both :cannot_be => :blank *and* :cannot_be => :nil (or :required => true)
+ # both :cannot_be => [ :blank, :nil ]
def _pv_cannot_be(opts, key, predicate_method_base_name)
value = _pv_opts_lookup(opts, key)
- predicate_method = (predicate_method_base_name.to_s + "?").to_sym
+ Array(predicate_method_base_name).each do |method_name|
+ predicate_method = :"#{method_name}?"
- if value.respond_to?(predicate_method)
- if value.send(predicate_method)
- raise Exceptions::ValidationFailed, "Option #{key} cannot be #{predicate_method_base_name}"
+ if value.respond_to?(predicate_method)
+ if value.send(predicate_method)
+ raise Exceptions::ValidationFailed, "Option #{key} cannot be #{predicate_method_base_name}"
+ end
end
end
end
@@ -202,7 +206,7 @@ class Chef
value = _pv_opts_lookup(opts, key)
if value != nil
passes = false
- [ regex ].flatten.each do |r|
+ Array(regex).each do |r|
if value != nil
if r.match(value.to_s)
passes = true
@@ -239,4 +243,3 @@ class Chef
end
end
end
-
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index bae7608f5b..40a6e911a6 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -712,6 +712,64 @@ class Chef
provider(arg)
end
+ #
+ # Create a property on this resource class.
+ #
+ # If a superclass has this property, or if this property has already been
+ # defined by this resource, this will *override* the previous value.
+ #
+ # @param name [Symbol] The name of the property.
+ # @param options [Hash<Symbol,Object>] Validation options.
+ # @option options [Object,Array] :equal_to An object, or list
+ # of objects, that must be equal to the value using Ruby's `==`
+ # operator (`options[:is].any? { |v| v == value }`)
+ # @option options [Regexp,Array<Regexp>] :regex An object, or
+ # list of objects, that must match the value with `regex.match(value)`.
+ # @option options [Class,Array<Class>] :kind_of A class, or
+ # list of classes, that the value must be an instance of.
+ # @option options [Hash<String,Proc>] :callbacks A hash of
+ # messages -> procs, all of which match the value. The proc must
+ # return a truthy or falsey value (true means it matches).
+ # @option options [Symbol,Array<Symbol>] :respond_to A method
+ # name, or list of method names, the value must respond to.
+ # @option options [Symbol,Array<Symbol>] :cannot_be A property,
+ # or a list of properties, that the value cannot have (such as `:nil` or
+ # `:empty`). The method with a questionmark at the end is called on the
+ # value (e.g. `value.empty?`). If the value does not have this method,
+ # it is considered valid (i.e. if you don't respond to `empty?` we
+ # assume you are not empty).
+ # @option options [Proc] :coerce A proc which will be called to
+ # transform the user input to canonical form. The value is passed in,
+ # and the transformed value returned as output. Lazy values will *not*
+ # be passed to this method until after they are evaluated. Called in the
+ # context of the resource (meaning you can access other properties).
+ # @option options [Boolean] :required `true` if this property
+ # must be present; `false` otherwise. This is checked after the resource
+ # is fully initialized.
+ # @option options [Boolean] :name_property `true` if this
+ # property defaults to the same value as `name`. Equivalent to
+ # `default: lazy { name }`, except that #property_is_set? will
+ # return `true` if the property is set *or* if `name` is set.
+ # @option options [Boolean] :name_attribute Same as `name_property`.
+ # @option options [Object] :default The value this property
+ # will return if the user does not set one. If this is `lazy`, it will
+ # be run in the context of the instance (and able to access other
+ # properties).
+ #
+ # @return [Chef::Resource::PropertyType] The property type.
+ #
+ # @example With nothing
+ # property :x
+ #
+ # @example With options
+ # property :x, default: 'hi'
+ #
+ def self.property(name, **options)
+ define_method(name) do |arg=nil|
+ set_or_return(name.to_sym, arg, options)
+ end
+ end
+
# Set or return the list of "state attributes" implemented by the Resource
# subclass. State attributes are attributes that describe the desired state
# of the system, such as file permissions or ownership. In general, state
diff --git a/lib/chef/resource/lwrp_base.rb b/lib/chef/resource/lwrp_base.rb
index ef3c2b5bba..443e0ed819 100644
--- a/lib/chef/resource/lwrp_base.rb
+++ b/lib/chef/resource/lwrp_base.rb
@@ -74,13 +74,7 @@ class Chef
resource_class
end
- # Define an attribute on this resource, including optional validation
- # parameters.
- def attribute(attr_name, validation_opts={})
- define_method(attr_name) do |arg=nil|
- set_or_return(attr_name.to_sym, arg, validation_opts)
- end
- end
+ alias :attribute :property
# Adds +action_names+ to the list of valid actions for this resource.
# Does not include superclass's action list when appending.