Class: Reenrb::Change

Inherits:
Object
  • 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
  extract_request(requested)
  decide_object
  decide_change
  decide_status_reason
end

Instance Attribute Details

#changeObject (readonly)

Returns the value of attribute change.



11
12
13
# File 'lib/reenrb/change.rb', line 11

def change
  @change
end

#originalObject (readonly)

Returns the value of attribute original.



11
12
13
# File 'lib/reenrb/change.rb', line 11

def original
  @original
end

#requestedObject (readonly)

Returns the value of attribute requested.



11
12
13
# File 'lib/reenrb/change.rb', line 11

def requested
  @requested
end

#statusObject (readonly)

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

Returns:

  • (Boolean)


119
# File 'lib/reenrb/change.rb', line 119

def accepted? = @status == STATUS::ACCEPTED

#decide_changeObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/reenrb/change.rb', line 63

def decide_change
  @change =
    if @operator == "--"
      CHANGE::FORCE_DELETE
    elsif @operator == "-"
      CHANGE::DELETE
    elsif @original != @requested
      CHANGE::RENAME
    else
      CHANGE::NONE
    end
end

#decide_objectObject



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_reasonObject



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

#executeObject



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

Returns:

  • (Boolean)


123
# File 'lib/reenrb/change.rb', line 123

def executed? = @status == STATUS::EXECUTED

#executed_or_rejected?Boolean

Returns:

  • (Boolean)


131
# File 'lib/reenrb/change.rb', line 131

def executed_or_rejected? = i[executed rejected].include?(@status)

#extract_request(str) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/reenrb/change.rb', line 54

def extract_request(str)
  op_chars = "[-]"
  obj_chars = "."
  requested = str.match(/^(?<op>#{op_chars}*)(?<name>#{obj_chars}*)/)

  @operator = requested[:op]
  @requested = requested[:name].strip
end

#failed?Boolean

Returns:

  • (Boolean)


127
# File 'lib/reenrb/change.rb', line 127

def failed? = @status == STATUS::FAILED

#not_accepted?Boolean

Returns:

  • (Boolean)


121
# File 'lib/reenrb/change.rb', line 121

def not_accepted? = !accepted?

#rejected?Boolean

Returns:

  • (Boolean)


125
# File 'lib/reenrb/change.rb', line 125

def rejected? = @status == STATUS::REJECTED

#request_delete?Boolean

Returns:

  • (Boolean)


115
# File 'lib/reenrb/change.rb', line 115

def request_delete? = @change == CHANGE::DELETE

#request_dir?Boolean

Predicates

Returns:

  • (Boolean)


107
# File 'lib/reenrb/change.rb', line 107

def request_dir? = Dir.exist?(@original)

#request_empty_dir?Boolean

Returns:

  • (Boolean)


117
# File 'lib/reenrb/change.rb', line 117

def request_empty_dir? = Dir.empty?(@original)

#request_file?Boolean

Returns:

  • (Boolean)


109
# File 'lib/reenrb/change.rb', line 109

def request_file? = File.file?(@original)

#request_full_dir?Boolean

Returns:

  • (Boolean)


129
# File 'lib/reenrb/change.rb', line 129

def request_full_dir? = request_dir? && !request_empty_dir?

#request_nothing?Boolean

Returns:

  • (Boolean)


111
# File 'lib/reenrb/change.rb', line 111

def request_nothing? = @change == CHANGE::NONE

#request_rename?Boolean

Returns:

  • (Boolean)


113
# File 'lib/reenrb/change.rb', line 113

def request_rename? = @change == CHANGE::RENAME

#to_sObject

Decoration



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