Class: SousChef::Resource::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sous_chef/resource/base.rb

Direct Known Subclasses

Directory, Execute, Log

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, name, &block) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
12
# File 'lib/sous_chef/resource/base.rb', line 6

def initialize(context, name, &block)
  @context = context
  @name = name
  @block = block
  @only_if = nil
  @commands = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (protected)



70
71
72
73
74
75
76
# File 'lib/sous_chef/resource/base.rb', line 70

def method_missing(meth, *args, &block)
  if context.respond_to?(meth)
    context.__send__(meth, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/sous_chef/resource/base.rb', line 4

def name
  @name
end

Instance Method Details

#append(cmd) ⇒ Object Also known as: command



42
43
44
# File 'lib/sous_chef/resource/base.rb', line 42

def append(cmd)
  @commands.push(cmd)
end

#not_if(cmd = nil) ⇒ Object



21
22
23
# File 'lib/sous_chef/resource/base.rb', line 21

def not_if(cmd=nil)
  only_if "! #{cmd}"
end

#only_if(cmd = nil) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/sous_chef/resource/base.rb', line 25

def only_if(cmd=nil)
  if cmd
    @only_if = cmd
  else
    @only_if
  end
end

#prepend(cmd) ⇒ Object



38
39
40
# File 'lib/sous_chef/resource/base.rb', line 38

def prepend(cmd)
  @commands.unshift(cmd)
end

#resource_respond_to?Object



33
# File 'lib/sous_chef/resource/base.rb', line 33

alias_method :resource_respond_to?, :respond_to?

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/sous_chef/resource/base.rb', line 34

def respond_to?(meth)
  super || context.respond_to?(meth)
end

#setupObject



14
15
16
17
18
19
# File 'lib/sous_chef/resource/base.rb', line 14

def setup
  if @block
    instance_eval &@block
    @block = nil
  end
end

#to_scriptObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/sous_chef/resource/base.rb', line 48

def to_script
  setup
  if only_if
    @commands.compact!
    @commands.map! { |line| "  #{line}" }
    prepend "if #{only_if}; then"
    append  "fi"
  end
  @commands.join("\n").strip
end