Skip to content

Commit

Permalink
Actually add ArrayProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
jkeiser committed Oct 29, 2015
1 parent c7d84c1 commit d368077
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/cheffish/array_property.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'chef_compat/property'

module Cheffish
# A typical array property. Defaults to [], accepts multiple args to setter, accumulates values.
class ArrayProperty < ChefCompat::Property
def initialize(**options)
options[:is] ||= Array
options[:default] ||= []
options[:coerce] ||= proc { |v| v.is_a?(Array) ? v : [ v ] }
super
end

# Support my_property 'a', 'b', 'c'; my_property 'a'; and my_property ['a', 'b']
def emit_dsl
declared_in.class_eval(<<-EOM, __FILE__, __LINE__+1)
def #{name}(*values)
property = self.class.properties[#{name.inspect}]
if values.empty?
property.get(self)
elsif property.is_set?(self)
property.set(self, property.get(self) + values.flatten)
else
property.set(self, values.flatten)
end
end
EOM
end
end
end

0 comments on commit d368077

Please sign in to comment.