Class: Cheffish::ArrayProperty

Inherits:
Chef::Property
  • Object
show all
Defined in:
lib/cheffish/array_property.rb

Overview

A typical array property. Defaults to [], accepts multiple args to setter, accumulates values.

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ ArrayProperty

Returns a new instance of ArrayProperty.



6
7
8
9
10
11
# File 'lib/cheffish/array_property.rb', line 6

def initialize(**options)
  options[:is] ||= Array
  options[:default] ||= []
  options[:coerce] ||= proc { |v| v.is_a?(Array) ? v : [ v ] }
  super
end

Instance Method Details

#emit_dslObject

Support my_property ‘a’, ‘b’, ‘c’; my_property ‘a’; and my_property [‘a’, ‘b’]



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cheffish/array_property.rb', line 14

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