Class: Chef::RunList::RunListItem

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/run_list/run_list_item.rb

Constant Summary collapse

QUALIFIED_RECIPE =
%r{^recipe\[([^\]]+)\]$}
QUALIFIED_ROLE =
%r{^role\[([^\]]+)\]$}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item) ⇒ RunListItem

Returns a new instance of RunListItem.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chef/run_list/run_list_item.rb', line 28

def initialize(item)
  case item
  when Hash
    assert_hash_is_valid_run_list_item!(item)
    @type = (item['type'] || item[:type]).to_sym
    @name = item['name'] || item[:name]
  when String
    if match = QUALIFIED_RECIPE.match(item)
      @type = :recipe
      @name = match[1]
    elsif match = QUALIFIED_ROLE.match(item)
      @type = :role
      @name = match[1]
    else
      @type = :recipe
      @name = item
    end
  else
    raise ArgumentError, "Unable to create #{self.class} from #{item.class}:#{item.inspect}: must be a Hash or String"
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'lib/chef/run_list/run_list_item.rb', line 24

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



26
27
28
# File 'lib/chef/run_list/run_list_item.rb', line 26

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/chef/run_list/run_list_item.rb', line 62

def ==(other)
  if other.kind_of?(String)
    self.to_s == other.to_s
  else
    other.respond_to?(:type) && other.respond_to?(:name) && other.type == @type && other.name == @name
  end
end

#assert_hash_is_valid_run_list_item!(item) ⇒ Object



70
71
72
73
74
# File 'lib/chef/run_list/run_list_item.rb', line 70

def assert_hash_is_valid_run_list_item!(item)
  unless (item.key?('type')|| item.key?(:type)) && (item.key?('name') || item.key?(:name))
    raise ArgumentError, "Initializing a #{self.class} from a hash requires that it have a 'type' and 'name' key"
  end
end

#recipe?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/chef/run_list/run_list_item.rb', line 58

def recipe?
  @type == :recipe
end

#role?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/chef/run_list/run_list_item.rb', line 54

def role?
  @type == :role
end

#to_sObject



50
51
52
# File 'lib/chef/run_list/run_list_item.rb', line 50

def to_s
  "#{@type}[#{@name}]"
end