Class: StoreAgent::Node::FileObject

Inherits:
Object
  • Object
show all
Defined in:
lib/store_agent/node/object/file_object.rb

Overview

ファイル

Instance Attribute Summary collapse

Attributes inherited from Object

#path, #workspace

Instance Method Summary collapse

Methods inherited from Object

#default_metadata, #directory?, #exists?, #filetype, #initial_metadata, #initial_owner, #initial_owner=, #initial_permission, #initial_permission=, #initialize, #metadata, #parent_directory, #permission, #revisions, #root?, #storage_object_path

Methods included from Validator

#validates_to_be_excluded_slash!, #validates_to_be_not_guest_identifier!, #validates_to_be_not_nil_value!, #validates_to_be_not_superuser_identifier!, #validates_to_be_string_or_symbol!

Constructor Details

This class inherits a constructor from StoreAgent::Node::Object

Instance Attribute Details

#body=(value) ⇒ Object (writeonly)

Sets the attribute body

Parameters:

  • value

    the value to set the attribute body to.



21
22
23
# File 'lib/store_agent/node/object/file_object.rb', line 21

def body=(value)
  @body = value
end

Instance Method Details

#childrenObject

:nodoc:



154
155
156
# File 'lib/store_agent/node/object/file_object.rb', line 154

def children # :nodoc:
  []
end

#chownObject



118
119
120
121
# File 'lib/store_agent/node/object/file_object.rb', line 118

def chown(*)
  super do
  end
end

#copy(dest_path = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/store_agent/node/object/file_object.rb', line 73

def copy(dest_path = nil, *)
  super do
    file_body = read
    dest_file = build_dest_file(dest_path)
    if dest_file.exists?
      dest_file.update(file_body)
    else
      dest_file.create(read)
    end
  end
end

#create(*params, &block) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/store_agent/node/object/file_object.rb', line 23

def create(*params, &block)
  super do
    set_body(*params, &block)
    save
    workspace.version_manager.add(storage_object_path)
  end
end

#deleteObject



59
60
61
62
63
64
# File 'lib/store_agent/node/object/file_object.rb', line 59

def delete(*)
  super do
    FileUtils.rm(storage_object_path)
    workspace.version_manager.remove(storage_object_path)
  end
end

#directory(_) ⇒ Object

:nodoc:



137
138
139
# File 'lib/store_agent/node/object/file_object.rb', line 137

def directory(_) # :nodoc:
  raise "#{@path} is not directory"
end

#file(_) ⇒ Object

:nodoc:



141
142
143
# File 'lib/store_agent/node/object/file_object.rb', line 141

def file(_) # :nodoc:
  raise "#{@path} is not directory"
end

#file?Boolean

:nodoc:

Returns:

  • (Boolean)


158
159
160
# File 'lib/store_agent/node/object/file_object.rb', line 158

def file? # :nodoc:
  true
end

#find_object(_) ⇒ Object

:nodoc:



133
134
135
# File 'lib/store_agent/node/object/file_object.rb', line 133

def find_object(_) # :nodoc:
  raise "#{@path} is not directory"
end

#get_metadataObject

:nodoc:



108
109
110
111
# File 'lib/store_agent/node/object/file_object.rb', line 108

def (*) # :nodoc:
  super do
  end
end

#get_permissionsObject

:nodoc:



113
114
115
116
# File 'lib/store_agent/node/object/file_object.rb', line 113

def get_permissions(*) # :nodoc:
  super do
  end
end

#move(dest_path = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/store_agent/node/object/file_object.rb', line 85

def move(dest_path = nil, *)
  super do
    dest_file = build_dest_file(dest_path)
    if dest_file.exists?
      disk_usage_diff = .disk_usage - dest_file..disk_usage
      file_count = 0
    else
      disk_usage_diff = .disk_usage
      file_count = 1
    end
    FileUtils.mv(storage_object_path, dest_file.storage_object_path)
    FileUtils.mv(.file_path, dest_file..file_path)
    FileUtils.mv(permission.file_path, dest_file.permission.file_path)
    dest_file.touch
    dest_file.parent_directory..update(disk_usage: disk_usage_diff, directory_file_count: file_count, tree_file_count: file_count, recursive: true)
    parent_directory..update(disk_usage: -dest_file..disk_usage, directory_file_count: -1, tree_file_count: -1, recursive: true)

    [storage_object_path, .file_path, permission.file_path].each do |file_path|
      workspace.version_manager.remove(file_path)
    end
  end
end

#read(revision: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/store_agent/node/object/file_object.rb', line 31

def read(revision: nil)
  super do
    if revision.nil?
      encoded_data = open(storage_object_path) do |f|
        f.read
      end
    else
      encoded_data = workspace.version_manager.read(path: storage_object_path, revision: revision)
    end
    StoreAgent.config.attachment_data_encoders.reverse.inject(encoded_data) do |data, encoder|
      encoder.decode(data)
    end
  end
end

#saveObject



145
146
147
148
149
150
151
152
# File 'lib/store_agent/node/object/file_object.rb', line 145

def save
  encoded_data = StoreAgent.config.storage_data_encoders.inject(@body) do |data, encoder|
    encoder.encode(data)
  end
  open(storage_object_path, "w") do |f|
    f.write encoded_data
  end
end

#set_permissionObject



123
124
125
126
# File 'lib/store_agent/node/object/file_object.rb', line 123

def set_permission(*)
  super do
  end
end

#touchObject



66
67
68
69
70
71
# File 'lib/store_agent/node/object/file_object.rb', line 66

def touch(*)
  super do
    FileUtils.touch(storage_object_path)
    workspace.version_manager.add(storage_object_path)
  end
end

#unset_permissionObject



128
129
130
131
# File 'lib/store_agent/node/object/file_object.rb', line 128

def unset_permission(*)
  super do
  end
end

#update(*params, &block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/store_agent/node/object/file_object.rb', line 46

def update(*params, &block)
  super do
    set_body(*params, &block)
    if @body.nil?
      raise "file body required"
    end
    save
    disk_usage_diff = @body.length - .disk_usage
    .update(disk_usage: disk_usage_diff, recursive: true)
    workspace.version_manager.add(storage_object_path)
  end
end