Class: MotherBrain::Chef::RunListItem

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

Constant Summary collapse

RECIPE_SEPARATOR =

MB Specific

"::"
QUALIFIED_RECIPE =

/MB Specific

%r{^recipe\[([^\]@]+)(@([0-9]+(\.[0-9]+){1,2}))?\]$}
QUALIFIED_ROLE =
%r{^role\[([^\]]+)\]$}
VERSIONED_UNQUALIFIED_RECIPE =
%r{^([^@]+)(@([0-9]+(\.[0-9]+){1,2}))$}
FALSE_FRIEND =
%r{[\[\]]}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item) ⇒ RunListItem

Returns a new instance of RunListItem.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mb/chef/run_list_item.rb', line 36

def initialize(item)
  @version = nil
  case item
  when Hash
    assert_hash_is_valid_run_list_item!(item)
    @type = (item['type'] || item[:type]).to_sym
    @name = item['name'] || item[:name]
    if (item.has_key?('version') || item.has_key?(:version))
      @version = item['version'] || item[:version]
    end
  when String
    if match = QUALIFIED_RECIPE.match(item)
      # recipe[recipe_name]
      # recipe[[email protected]]
      @type = :recipe
      @name = match[1]
      @version = match[3] if match[3]
    elsif match = QUALIFIED_ROLE.match(item)
      # role[role_name]
      @type = :role
      @name = match[1]
    elsif match = VERSIONED_UNQUALIFIED_RECIPE.match(item)
      # [email protected]
      @type = :recipe
      @name = match[1]
      @version = match[3] if match[3]
    elsif match = FALSE_FRIEND.match(item)
      # Recipe[recipe_name]
      # roles[role_name]
      name = match[1]
      raise ArgumentError, "Unable to create #{self.class} from #{item.class}:#{item.inspect}: must be recipe[#{name}] or role[#{name}]"

    else
      # recipe_name
      @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.



33
34
35
# File 'lib/mb/chef/run_list_item.rb', line 33

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



33
34
35
# File 'lib/mb/chef/run_list_item.rb', line 33

def type
  @type
end

#versionObject (readonly)

Returns the value of attribute version.



33
34
35
# File 'lib/mb/chef/run_list_item.rb', line 33

def version
  @version
end

Instance Method Details

#==(other) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/mb/chef/run_list_item.rb', line 90

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

#assert_hash_is_valid_run_list_item!(item) ⇒ Object



98
99
100
101
102
# File 'lib/mb/chef/run_list_item.rb', line 98

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

#cookbook_nameObject

MB specific



105
106
107
108
109
110
111
# File 'lib/mb/chef/run_list_item.rb', line 105

def cookbook_name
  if self.recipe?
    cookbook, _ = self.name.split(RECIPE_SEPARATOR)
    return cookbook
  end
  return nil
end

#recipe?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/mb/chef/run_list_item.rb', line 86

def recipe?
  @type == :recipe
end

#role?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/mb/chef/run_list_item.rb', line 82

def role?
  @type == :role
end

#to_sObject



78
79
80
# File 'lib/mb/chef/run_list_item.rb', line 78

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