Class: Reenrb::Change
- Inherits:
-
Object
show all
- Defined in:
- lib/reenrb/change.rb
Overview
Change to an orignal file
Defined Under Namespace
Modules: CHANGE, OBJECT, STATUS
Constant Summary
collapse
- ACTION_HANDLER =
{
CHANGE::NONE => Actions::DoNothing,
CHANGE::DELETE => Actions::Delete,
CHANGE::FORCE_DELETE => Actions::ForceDelete,
CHANGE::RENAME => Actions::Rename
}.freeze
- CHANGES_DESC =
{
CHANGE::NONE => "Nothing",
CHANGE::DELETE => "Deleting",
CHANGE::FORCE_DELETE => "Force deleting",
CHANGE::RENAME => "Renaming"
}.freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(original, requested) ⇒ Change
Returns a new instance of Change.
46
47
48
49
50
51
52
|
# File 'lib/reenrb/change.rb', line 46
def initialize(original, requested)
@original = original
(requested)
decide_object
decide_change
decide_status_reason
end
|
Instance Attribute Details
#change ⇒ Object
Returns the value of attribute change.
11
12
13
|
# File 'lib/reenrb/change.rb', line 11
def change
@change
end
|
#original ⇒ Object
Returns the value of attribute original.
11
12
13
|
# File 'lib/reenrb/change.rb', line 11
def original
@original
end
|
#requested ⇒ Object
Returns the value of attribute requested.
11
12
13
|
# File 'lib/reenrb/change.rb', line 11
def requested
@requested
end
|
#status ⇒ Object
Returns the value of attribute status.
11
12
13
|
# File 'lib/reenrb/change.rb', line 11
def status
@status
end
|
Instance Method Details
#accepted? ⇒ Boolean
119
|
# File 'lib/reenrb/change.rb', line 119
def accepted? = @status == STATUS::ACCEPTED
|
#decide_object ⇒ Object
76
77
78
79
|
# File 'lib/reenrb/change.rb', line 76
def decide_object
@object = OBJECT::FILE if request_file?
@object = OBJECT::FOLDER if request_dir?
end
|
#decide_status_reason ⇒ Object
81
82
83
84
85
86
87
88
89
|
# File 'lib/reenrb/change.rb', line 81
def decide_status_reason
if request_full_dir? && request_delete?
@status = STATUS::REJECTED
@reason = "Directories with files cannot be changed"
else
@status = STATUS::ACCEPTED
@reason = ""
end
end
|
#execute ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/reenrb/change.rb', line 91
def execute
return(self) if not_accepted?
@status = STATUS::EXECUTED
error = ACTION_HANDLER[@change].new(@original, @requested).call
if error
@status = STATUS::FAILED
@reason = error
end
self
end
|
#executed? ⇒ Boolean
123
|
# File 'lib/reenrb/change.rb', line 123
def executed? = @status == STATUS::EXECUTED
|
#executed_or_rejected? ⇒ Boolean
131
|
# File 'lib/reenrb/change.rb', line 131
def executed_or_rejected? = %i[executed rejected].include?(@status)
|
54
55
56
57
58
59
60
61
|
# File 'lib/reenrb/change.rb', line 54
def (str)
op_chars = "[-]"
obj_chars = "."
requested = str.match(/^(?<op>#{op_chars}*)(?<name>#{obj_chars}*)/)
@operator = requested[:op]
@requested = requested[:name].strip
end
|
#failed? ⇒ Boolean
127
|
# File 'lib/reenrb/change.rb', line 127
def failed? = @status == STATUS::FAILED
|
#not_accepted? ⇒ Boolean
121
|
# File 'lib/reenrb/change.rb', line 121
def not_accepted? = !accepted?
|
#rejected? ⇒ Boolean
125
|
# File 'lib/reenrb/change.rb', line 125
def rejected? = @status == STATUS::REJECTED
|
#request_delete? ⇒ Boolean
115
|
# File 'lib/reenrb/change.rb', line 115
def request_delete? = @change == CHANGE::DELETE
|
#request_dir? ⇒ Boolean
107
|
# File 'lib/reenrb/change.rb', line 107
def request_dir? = Dir.exist?(@original)
|
#request_empty_dir? ⇒ Boolean
117
|
# File 'lib/reenrb/change.rb', line 117
def request_empty_dir? = Dir.empty?(@original)
|
#request_file? ⇒ Boolean
109
|
# File 'lib/reenrb/change.rb', line 109
def request_file? = File.file?(@original)
|
#request_full_dir? ⇒ Boolean
129
|
# File 'lib/reenrb/change.rb', line 129
def request_full_dir? = request_dir? && !request_empty_dir?
|
#request_nothing? ⇒ Boolean
111
|
# File 'lib/reenrb/change.rb', line 111
def request_nothing? = @change == CHANGE::NONE
|
#request_rename? ⇒ Boolean
113
|
# File 'lib/reenrb/change.rb', line 113
def request_rename? = @change == CHANGE::RENAME
|
#to_s ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/reenrb/change.rb', line 135
def to_s
file_desc =
case @change
when CHANGE::RENAME
"#{@original} -> #{@requested}"
else
@original
end
reason_desc = rejected? || failed? ? " (failed: #{@reason})" : ""
"#{CHANGES_DESC[@change]}: #{file_desc}#{reason_desc}"
end
|