Module: Shef::Extensions::ObjectCoreExtensions

Defined in:
lib/chef/shef/ext.rb

Overview

Extensions to be included in every 'main' object in shef. These objects are extended with this module.

Instance Method Summary collapse

Instance Method Details

#all_help_descriptionsObject



115
116
117
# File 'lib/chef/shef/ext.rb', line 115

def all_help_descriptions
  help_descriptions
end

#desc(help_text) ⇒ Object



119
120
121
# File 'lib/chef/shef/ext.rb', line 119

def desc(help_text)
  @desc = help_text
end

#ensure_session_select_definedObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/chef/shef/ext.rb', line 37

def ensure_session_select_defined
  # irb breaks if you prematurely define IRB::JobMangager
  # so these methods need to be defined at the latest possible time.
  unless jobs.respond_to?(:select_session_by_context)
    def jobs.select_session_by_context(&block)
      @jobs.select { |job| block.call(job[1].context.main)}
    end
  end

  unless jobs.respond_to?(:session_select)
    def jobs.select_shef_session(target_context)
      session = if target_context.kind_of?(Class)
        select_session_by_context { |main| main.kind_of?(target_context) }
      else
        select_session_by_context { |main| main.equal?(target_context) }
      end
      Array(session.first)[1]
    end
  end
end

#explain(explain_text) ⇒ Object



123
124
125
# File 'lib/chef/shef/ext.rb', line 123

def explain(explain_text)
  @explain = explain_text
end

#explain_command(method_name) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/chef/shef/ext.rb', line 85

def explain_command(method_name)
  help = self.all_help_descriptions.find { |h| h.cmd.to_s == method_name.to_s }
  if help
    puts ""
    puts "Command: #{method_name}"
    puts "".ljust(80, "=")
    puts help.explanation || help.desc
    puts "".ljust(80, "=")
    puts ""
  else
    puts ""
    puts "command #{method_name} not found or no help available"
    puts ""
  end
end

#find_or_create_session_for(context_obj) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/chef/shef/ext.rb', line 58

def find_or_create_session_for(context_obj)
  ensure_session_select_defined
  if subsession = jobs.select_shef_session(context_obj)
    jobs.switch(subsession)
  else
    irb(context_obj)
  end
end

#help_bannerObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/chef/shef/ext.rb', line 67

def help_banner
  banner = []
  banner << ""
  banner << "Shef Help"
  banner << "".ljust(80, "=")
  banner << "| " + "Command".ljust(25) + "| " + "Description"
  banner << "".ljust(80, "=")

  self.all_help_descriptions.each do |help_text|
    banner << "| " + help_text.cmd.ljust(25) + "| " + help_text.desc
  end
  banner << "".ljust(80, "=")
  banner << "\n"
  banner << "Use help(:command) to get detailed help with individual commands"
  banner << "\n"
  banner.join("\n")
end

#help_descriptionsObject



111
112
113
# File 'lib/chef/shef/ext.rb', line 111

def help_descriptions
  @help_descriptions ||= []
end

#offObject

returns :off so you can just do `tracing off'



107
108
109
# File 'lib/chef/shef/ext.rb', line 107

def off
  :off
end

#onObject

helpfully returns :on so we can have sugary syntax like `tracing on'



102
103
104
# File 'lib/chef/shef/ext.rb', line 102

def on
  :on
end

#singleton_method_added(mname) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/chef/shef/ext.rb', line 131

def singleton_method_added(mname)
  if @desc
    help_descriptions << Help.new(mname.to_s, @desc.to_s, @explain)
    @desc, @explain = nil, nil
  end
  if @subcommand_help
    @subcommand_help.each do |subcommand, text|
      help_descriptions << Help.new("#{mname}.#{subcommand}", text.to_s, nil)
    end
  end
  @subcommand_help = {}
end

#subcommands(subcommand_help = {}) ⇒ Object



127
128
129
# File 'lib/chef/shef/ext.rb', line 127

def subcommands(subcommand_help={})
  @subcommand_help = subcommand_help
end