Class: Chef::ChefFS::FileSystem::Repository::DataBagItem

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/chef_fs/file_system/repository/data_bag_item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent) ⇒ DataBagItem

Returns a new instance of DataBagItem.



33
34
35
36
37
38
39
# File 'lib/chef/chef_fs/file_system/repository/data_bag_item.rb', line 33

def initialize(name, parent)
  @parent = parent
  @name = name
  @path = Chef::ChefFS::PathUtils.join(parent.path, name)
  @data_handler = Chef::ChefFS::DataHandler::DataBagItemDataHandler.new
  @file_path = "#{parent.file_path}/#{name}"
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



31
32
33
# File 'lib/chef/chef_fs/file_system/repository/data_bag_item.rb', line 31

def file_path
  @file_path
end

#nameObject (readonly)

Returns the value of attribute name.



28
29
30
# File 'lib/chef/chef_fs/file_system/repository/data_bag_item.rb', line 28

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



29
30
31
# File 'lib/chef/chef_fs/file_system/repository/data_bag_item.rb', line 29

def parent
  @parent
end

#pathObject (readonly)

Returns the value of attribute path.



30
31
32
# File 'lib/chef/chef_fs/file_system/repository/data_bag_item.rb', line 30

def path
  @path
end

Instance Method Details

#can_have_child?(name, is_dir) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/chef/chef_fs/file_system/repository/data_bag_item.rb', line 62

def can_have_child?(name, is_dir)
  false
end

#compare_to(other) ⇒ Object



106
107
108
# File 'lib/chef/chef_fs/file_system/repository/data_bag_item.rb', line 106

def compare_to(other)
  nil
end

#create(file_contents) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/chef/chef_fs/file_system/repository/data_bag_item.rb', line 54

def create(file_contents)
  if exists?
    raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, self)
  else
    write(file_contents)
  end
end

#delete(recurse) ⇒ Object



78
79
80
81
82
# File 'lib/chef/chef_fs/file_system/repository/data_bag_item.rb', line 78

def delete(recurse)
  File.delete(file_path)
rescue Errno::ENOENT
  raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
end

#dir?Boolean

Public API callied by chef_fs/file_system

Returns:

  • (Boolean)


42
43
44
# File 'lib/chef/chef_fs/file_system/repository/data_bag_item.rb', line 42

def dir?
  false
end

#exists?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/chef/chef_fs/file_system/repository/data_bag_item.rb', line 84

def exists?
  File.exists?(file_path)
end

#fs_entry_valid?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/chef/chef_fs/file_system/repository/data_bag_item.rb', line 50

def fs_entry_valid?
  name_valid? && File.file?(file_path)
end

#name_valid?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/chef/chef_fs/file_system/repository/data_bag_item.rb', line 46

def name_valid?
  !name.start_with?(".") && name.end_with?(".json")
end

#path_for_printingObject



74
75
76
# File 'lib/chef/chef_fs/file_system/repository/data_bag_item.rb', line 74

def path_for_printing
  file_path
end

#readObject



88
89
90
91
92
93
94
# File 'lib/chef/chef_fs/file_system/repository/data_bag_item.rb', line 88

def read
  begin
    File.open(file_path, "rb") { |f| f.read }
  rescue Errno::ENOENT
    raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
  end
end

#rootObject



102
103
104
# File 'lib/chef/chef_fs/file_system/repository/data_bag_item.rb', line 102

def root
  parent.root
end

#write(content) ⇒ Object



96
97
98
99
100
# File 'lib/chef/chef_fs/file_system/repository/data_bag_item.rb', line 96

def write(content)
  File.open(file_path, "wb") do |file|
    file.write(content)
  end
end

#write_pretty_jsonObject



70
71
72
# File 'lib/chef/chef_fs/file_system/repository/data_bag_item.rb', line 70

def write_pretty_json
  @write_pretty_json.nil? ? root.write_pretty_json : @write_pretty_json
end

#write_pretty_json=(value) ⇒ Object



66
67
68
# File 'lib/chef/chef_fs/file_system/repository/data_bag_item.rb', line 66

def write_pretty_json=(value)
  @write_pretty_json = value
end