Class: Chef::Knife::Core::ObjectLoader
- Defined in:
- lib/chef/knife/core/object_loader.rb
Defined Under Namespace
Classes: ObjectType
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#ui ⇒ Object
readonly
Returns the value of attribute ui.
Instance Method Summary collapse
- #file_exists_and_is_readable?(file) ⇒ Boolean
- #find_all_object_dirs(path) ⇒ Object
-
#find_all_objects(path) ⇒ Array<String>
Find all objects in the given location If the object type is File it will look for all *.json,rb files, otherwise it will lookup for folders only (useful for data_bags).
-
#find_file(repo_location, *components) ⇒ Object
When someone makes this awesome, please update the above error message.
-
#initialize(klass, ui) ⇒ ObjectLoader
constructor
A new instance of ObjectLoader.
- #load_from(repo_location, *components) ⇒ Object
- #object_from_file(filename) ⇒ Object
Constructor Details
#initialize(klass, ui) ⇒ ObjectLoader
Returns a new instance of ObjectLoader.
32 33 34 35 |
# File 'lib/chef/knife/core/object_loader.rb', line 32 def initialize(klass, ui) @klass = klass @ui = ui end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
25 26 27 |
# File 'lib/chef/knife/core/object_loader.rb', line 25 def klass @klass end |
#ui ⇒ Object (readonly)
Returns the value of attribute ui.
24 25 26 |
# File 'lib/chef/knife/core/object_loader.rb', line 24 def ui @ui end |
Instance Method Details
#file_exists_and_is_readable?(file) ⇒ Boolean
104 105 106 |
# File 'lib/chef/knife/core/object_loader.rb', line 104 def file_exists_and_is_readable?(file) File.exists?(file) && File.readable?(file) end |
#find_all_object_dirs(path) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/chef/knife/core/object_loader.rb', line 76 def find_all_object_dirs(path) path = File.join(path, '*') objects = Dir.glob(File.(path)) objects.delete_if { |o| !File.directory?(o) } objects.map { |o| File.basename(o) } end |
#find_all_objects(path) ⇒ Array<String>
Find all objects in the given location If the object type is File it will look for all *.json,rb files, otherwise it will lookup for folders only (useful for data_bags)
69 70 71 72 73 74 |
# File 'lib/chef/knife/core/object_loader.rb', line 69 def find_all_objects(path) path = File.join(path, '*') path << '.{json,rb}' objects = Dir.glob(File.(path)) objects.map { |o| File.basename(o) } end |
#find_file(repo_location, *components) ⇒ Object
When someone makes this awesome, please update the above error message.
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/chef/knife/core/object_loader.rb', line 46 def find_file(repo_location, *components) if file_exists_and_is_readable?(File.( components.last )) File.( components.last ) else relative_path = File.join(Dir.pwd, repo_location, *components) if file_exists_and_is_readable?(relative_path) relative_path else nil end end end |
#load_from(repo_location, *components) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/chef/knife/core/object_loader.rb', line 37 def load_from(repo_location, *components) unless object_file = find_file(repo_location, *components) ui.error "Could not find or open file '#{components.last}' in current directory or in '#{repo_location}/#{components.join('/')}'" exit 1 end object_from_file(object_file) end |
#object_from_file(filename) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/chef/knife/core/object_loader.rb', line 83 def object_from_file(filename) case filename when /\.(js|json)$/ r = Yajl::Parser.parse(IO.read(filename)) # Chef::DataBagItem doesn't work well with the json_create method if @klass == Chef::DataBagItem r else @klass.json_create(r) end when /\.rb$/ r = klass.new r.from_file(filename) r else ui.fatal("File must end in .js, .json, or .rb") exit 30 end end |