Class: Chef::Knife::Core::ObjectLoader
- Inherits:
-
Object
- Object
- 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.
36 37 38 39 |
# File 'lib/chef/knife/core/object_loader.rb', line 36 def initialize(klass, ui) @klass = klass @ui = ui end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
29 30 31 |
# File 'lib/chef/knife/core/object_loader.rb', line 29 def klass @klass end |
#ui ⇒ Object (readonly)
Returns the value of attribute ui.
28 29 30 |
# File 'lib/chef/knife/core/object_loader.rb', line 28 def ui @ui end |
Instance Method Details
#file_exists_and_is_readable?(file) ⇒ Boolean
108 109 110 |
# File 'lib/chef/knife/core/object_loader.rb', line 108 def file_exists_and_is_readable?(file) File.exist?(file) && File.readable?(file) end |
#find_all_object_dirs(path) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/chef/knife/core/object_loader.rb', line 80 def find_all_object_dirs(path) path = File.join(ChefConfig::PathHelper.escape_glob_dir(File.(path)), "*") objects = Dir.glob(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)
73 74 75 76 77 78 |
# File 'lib/chef/knife/core/object_loader.rb', line 73 def find_all_objects(path) path = File.join(ChefConfig::PathHelper.escape_glob_dir(File.(path)), "*") path << ".{json,rb}" objects = Dir.glob(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.
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/chef/knife/core/object_loader.rb', line 50 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
41 42 43 44 45 46 47 |
# File 'lib/chef/knife/core/object_loader.rb', line 41 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
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/chef/knife/core/object_loader.rb', line 87 def object_from_file(filename) case filename when /\.(js|json)$/ r = FFI_Yajl::Parser.parse(IO.read(filename)) # Chef::DataBagItem doesn't work well with the json_create method if @klass == Chef::DataBagItem r else @klass.from_hash(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 |