Class: StoreAgent::Node::Object

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Validator
Defined in:
lib/store_agent/node/object.rb

Overview

ファイルやディレクトリなど、オブジェクトの雛形となるクラス
実際にはファイルやディレクトリなど、このクラスを継承したクラスを使用する

Direct Known Subclasses

DirectoryObject, FileObject, VirtualObject

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#initialize(workspace: nil, path: "/") ⇒ Object

:nodoc:



32
33
34
35
36
# File 'lib/store_agent/node/object.rb', line 32

def initialize(workspace: nil, path: "/") # :nodoc:
  @workspace = workspace
  validates_to_be_not_nil_value!(:workspace)
  @path = sanitize_path(path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



29
30
31
# File 'lib/store_agent/node/object.rb', line 29

def path
  @path
end

#workspaceObject (readonly)

Returns the value of attribute workspace.



29
30
31
# File 'lib/store_agent/node/object.rb', line 29

def workspace
  @workspace
end

Instance Method Details

#chown(identifier: nil, **_) ⇒ Object



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

def chown(*, identifier: nil, **_)
  workspace.version_manager.transaction("change_owner #{path}") do
    yield
    .owner = identifier
    .save
  end
end

#copy(dest_path = nil) ⇒ Object



105
106
107
108
109
# File 'lib/store_agent/node/object.rb', line 105

def copy(dest_path = nil)
  workspace.version_manager.transaction("copy #{path} to #{dest_path}") do
    yield
  end
end

#createObject



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

def create(*)
  workspace.version_manager.transaction("created #{path}") do
    yield
    .create
    permission.create
  end
  self
end

#default_metadataObject

:nodoc:



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/store_agent/node/object.rb', line 175

def  # :nodoc:
  {
    "size" => StoreAgent::Node::Metadata.datasize_format(initial_bytesize),
    "bytes" => initial_bytesize,
    "owner" => initial_owner,
    "is_dir" => directory?,
    "created_at" => updated_at.to_s,
    "updated_at" => updated_at.to_s,
    "created_at_unix_timestamp" => updated_at.to_i,
    "updated_at_unix_timestamp" => updated_at.to_i,
  }.merge()
end

#deleteObject



86
87
88
89
90
91
92
93
# File 'lib/store_agent/node/object.rb', line 86

def delete(*)
  workspace.version_manager.transaction("deleted #{path}") do
    yield
    .delete
    permission.delete
  end
  true
end

#directory?Boolean

true を返す場合、ディレクトリとして認識される

Returns:

  • (Boolean)


194
195
196
# File 'lib/store_agent/node/object.rb', line 194

def directory?
  false
end

#exists?Boolean

オブジェクトが存在するなら true を返す

Returns:

  • (Boolean)


162
163
164
# File 'lib/store_agent/node/object.rb', line 162

def exists?
  File.exists?(storage_object_path)
end

#file?Boolean

true を返す場合、ファイルとして認識される

Returns:

  • (Boolean)


199
200
201
# File 'lib/store_agent/node/object.rb', line 199

def file?
  false
end

#filetypeObject

ファイルの種類。file、directory など



167
168
169
# File 'lib/store_agent/node/object.rb', line 167

def filetype
  File.ftype(storage_object_path)
end

#get_metadataObject

:nodoc:



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

def (*) # :nodoc:
  yield
  .data
end

#get_permissionsObject

:nodoc:



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

def get_permissions(*) # :nodoc:
  yield
  permission.data
end

#initial_metadataObject

:nodoc:



171
172
173
# File 'lib/store_agent/node/object.rb', line 171

def  # :nodoc:
  @initial_metadata ||= {}
end

#initial_ownerObject

:nodoc:



48
49
50
# File 'lib/store_agent/node/object.rb', line 48

def initial_owner # :nodoc:
  @initial_owner ||= current_user.identifier
end

#initial_owner=(owner_identifier) ⇒ Object

:nodoc:



52
53
54
55
# File 'lib/store_agent/node/object.rb', line 52

def initial_owner=(owner_identifier) # :nodoc:
  authorize!("chown")
  @initial_owner = owner_identifier
end

#initial_permissionObject

:nodoc:



57
58
59
# File 'lib/store_agent/node/object.rb', line 57

def initial_permission # :nodoc:
  @initial_permission ||= StoreAgent.config.default_owner_permission
end

#initial_permission=(permissions) ⇒ Object

:nodoc:



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

def initial_permission=(permissions) # :nodoc:
  authorize!("chmod")
  @initial_permission = permissions
end

#metadataObject

オブジェクトに紐づくメタデータのインスタンス



39
40
41
# File 'lib/store_agent/node/object.rb', line 39

def 
  @metadata ||= StoreAgent::Node::Metadata.new(object: self)
end

#move(dest_path = nil) ⇒ Object



111
112
113
114
115
# File 'lib/store_agent/node/object.rb', line 111

def move(dest_path = nil)
  workspace.version_manager.transaction("move #{path} to #{dest_path}") do
    yield
  end
end

#parent_directoryObject

親階層のディレクトリオブジェクトを返す



150
151
152
153
154
# File 'lib/store_agent/node/object.rb', line 150

def parent_directory
  if !root?
    @parent_directory ||= StoreAgent::Node::DirectoryObject.new(workspace: @workspace, path: File.dirname(@path))
  end
end

#permissionObject

オブジェクトに紐づく権限情報のインスタンス



44
45
46
# File 'lib/store_agent/node/object.rb', line 44

def permission
  @permission ||= StoreAgent::Node::Permission.new(object: self)
end

#readObject



75
76
77
# File 'lib/store_agent/node/object.rb', line 75

def read(*)
  yield
end

#revisionsObject

バージョン管理をしている場合、変更があったリビジョンの一覧を返す



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

def revisions
  workspace.version_manager.revisions("storage#{path}")
end

#root?Boolean

true を返す場合、ファイルツリーの最上位ディレクトリとして認識される

Returns:

  • (Boolean)


189
190
191
# File 'lib/store_agent/node/object.rb', line 189

def root?
  @path == "/"
end

#set_permission(identifier: nil, permission_values: {}, **_) ⇒ Object



135
136
137
138
139
140
# File 'lib/store_agent/node/object.rb', line 135

def set_permission(identifier: nil, permission_values: {}, **_)
  workspace.version_manager.transaction("add_permission #{path}") do
    permission.set!(identifier: identifier, permission_values: permission_values)
    yield
  end
end

#storage_object_pathObject

オブジェクトの絶対パス



204
205
206
# File 'lib/store_agent/node/object.rb', line 204

def storage_object_path
  "#{@workspace.storage_dirname}#{@path}"
end

#touchObject



95
96
97
98
99
100
101
102
103
# File 'lib/store_agent/node/object.rb', line 95

def touch(*)
  workspace.version_manager.transaction("touch #{path}") do
    .reload
    yield
    .updated_at = Time.now
    .save
    workspace.version_manager.add(permission.file_path)
  end
end

#unset_permission(identifier: nil, permission_names: [], **_) ⇒ Object



142
143
144
145
146
147
# File 'lib/store_agent/node/object.rb', line 142

def unset_permission(identifier: nil, permission_names: [], **_)
  workspace.version_manager.transaction("remove_permission #{path}") do
    permission.unset!(identifier: identifier, permission_names: permission_names)
    yield
  end
end

#updateObject



79
80
81
82
83
84
# File 'lib/store_agent/node/object.rb', line 79

def update(*)
  workspace.version_manager.transaction("updated #{path}") do
    yield
  end
  true
end