Class: Knife::Helper::Config
- Inherits:
-
Object
- Object
- Knife::Helper::Config
- Defined in:
- lib/knife/helper/config.rb
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#option_sets ⇒ Object
readonly
Returns the value of attribute option_sets.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
Instance Method Summary collapse
- #default_file ⇒ Object
- #get_content(str) ⇒ Object
-
#initialize(file = nil) ⇒ Config
constructor
A new instance of Config.
- #load_config(file) ⇒ Object
- #load_yml(str) ⇒ Object
- #read_file(file) ⇒ Object
Constructor Details
#initialize(file = nil) ⇒ Config
Returns a new instance of Config.
11 12 13 14 15 16 17 |
# File 'lib/knife/helper/config.rb', line 11 def initialize(file=nil) file ||= default_file conf = load_config(file) @settings = conf['settings'] @option_sets = conf['option_sets'] @commands = conf['commands'] end |
Instance Attribute Details
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
9 10 11 |
# File 'lib/knife/helper/config.rb', line 9 def commands @commands end |
#option_sets ⇒ Object (readonly)
Returns the value of attribute option_sets.
9 10 11 |
# File 'lib/knife/helper/config.rb', line 9 def option_sets @option_sets end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
9 10 11 |
# File 'lib/knife/helper/config.rb', line 9 def settings @settings end |
Instance Method Details
#default_file ⇒ Object
34 35 36 |
# File 'lib/knife/helper/config.rb', line 34 def default_file ::File.join(Dir.pwd, ".knife.helper.yml") end |
#get_content(str) ⇒ Object
42 43 44 |
# File 'lib/knife/helper/config.rb', line 42 def get_content(str) ::ERB.new(str).result end |
#load_config(file) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/knife/helper/config.rb', line 19 def load_config(file) conf = load_yml(get_content(read_file(file))) Array(conf['includes']).each do |inc| inc_path = nil rel_path = File.("../#{inc}", file) [inc, rel_path].each {|f| inc_path = f if File.exists?(f) } raise "'#{inc}' is not exists." if inc_path.nil? conf = Chef::Mixin::DeepMerge.deep_merge( load_yml(get_content(read_file(inc_path))), conf ) end conf end |
#load_yml(str) ⇒ Object
46 47 48 |
# File 'lib/knife/helper/config.rb', line 46 def load_yml(str) ::SafeYAML.load(str) || Hash.new end |
#read_file(file) ⇒ Object
38 39 40 |
# File 'lib/knife/helper/config.rb', line 38 def read_file(file) ::File.exist?(file.to_s) ? IO.read(file) : "" end |