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("    def \#{name}(*values)\n      property = self.class.properties[\#{name.inspect}]\n      if values.empty?\n        property.get(self)\n      elsif property.is_set?(self)\n        property.set(self, property.get(self) + values.flatten)\n      else\n        property.set(self, values.flatten)\n      end\n    end\n  EOM\nend\n", __FILE__, __LINE__ + 1)