Class: Stylish::Developer::Modification

Inherits:
Object
  • Object
show all
Defined in:
lib/stylish/developer/modification.rb

Overview

Handles a request to modify an asset that belongs to a Stylish package.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actual_path, request_type, request) ⇒ Modification

Returns a new instance of Modification.



13
14
15
16
17
18
19
20
21
# File 'lib/stylish/developer/modification.rb', line 13

def initialize(actual_path, request_type, request)
  @actual_path    = actual_path
  @request_type   = request_type
  @request        = request

  @code    = nil
  @headers = {}
  @body    = {}
end

Instance Attribute Details

#actual_pathObject (readonly)

Returns the value of attribute actual_path.



7
8
9
# File 'lib/stylish/developer/modification.rb', line 7

def actual_path
  @actual_path
end

#bodyObject

Returns the value of attribute body.



11
12
13
# File 'lib/stylish/developer/modification.rb', line 11

def body
  @body
end

#codeObject

Returns the value of attribute code.



11
12
13
# File 'lib/stylish/developer/modification.rb', line 11

def code
  @code
end

#headersObject

Returns the value of attribute headers.



11
12
13
# File 'lib/stylish/developer/modification.rb', line 11

def headers
  @headers
end

#requestObject (readonly)

Returns the value of attribute request.



7
8
9
# File 'lib/stylish/developer/modification.rb', line 7

def request
  @request
end

#request_typeObject (readonly)

Returns the value of attribute request_type.



7
8
9
# File 'lib/stylish/developer/modification.rb', line 7

def request_type
  @request_type
end

Instance Method Details

#add_error(message) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/stylish/developer/modification.rb', line 59

def add_error(message)
  self.code = 500

  binding.pry

  body[:errors] ||= []
  body[:errors] << message
end

#body_contentsObject



120
121
122
123
124
# File 'lib/stylish/developer/modification.rb', line 120

def body_contents
  @body = @body.to_json if @body.is_a?(Hash)
  @body = @body.first if @body.is_a?(Array)
  @body.to_s
end

#contentsObject



101
102
103
# File 'lib/stylish/developer/modification.rb', line 101

def contents
  request.params['contents']
end

#create?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/stylish/developer/modification.rb', line 51

def create?
  request_type == "create"
end

#current_rootObject



23
24
25
# File 'lib/stylish/developer/modification.rb', line 23

def current_root
  Stylish::Developer.server.root
end

#delete?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/stylish/developer/modification.rb', line 47

def delete?
  request_type == "delete"
end

#fileObject



27
28
29
# File 'lib/stylish/developer/modification.rb', line 27

def file
  current_root.join(actual_path)
end

#file_exist?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/stylish/developer/modification.rb', line 35

def file_exist?
  file_exists?
end

#file_exists?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/stylish/developer/modification.rb', line 31

def file_exists?
  file && file.exist?
end

#file_writable?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/stylish/developer/modification.rb', line 39

def file_writable?
  file && file.writable?
end

#handleObject



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/stylish/developer/modification.rb', line 68

def handle
  return if @handled

  if file_exists? && file_writable?
    handle_existing_file_modification
  elsif file_exist? && !file_writable?
    handle_access_denied_error
  else
    handle_non_existing_file_modification
  end

  @handled = true
end

#handle_access_denied_errorObject



82
83
84
# File 'lib/stylish/developer/modification.rb', line 82

def handle_access_denied_error
  add_error("Can not access file at #{ actual_path }. File not writable")
end

#handle_existing_file_modificationObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/stylish/developer/modification.rb', line 86

def handle_existing_file_modification
  if create?
    add_error("Can not create file at #{ actual_path }. Already exists")
  elsif update?
    file.open("w") {|fh| fh.write(contents) }
    load_path_meta
  elsif delete?
    path = file.to_s
    file.unlink if file.file?
    file.rmdir if file.directory?
    self.code = 200
    self.body = {status: "deleted", path: path}
  end
end

#handle_non_existing_file_modificationObject



105
106
107
108
109
110
111
112
113
114
# File 'lib/stylish/developer/modification.rb', line 105

def handle_non_existing_file_modification
  if update? || delete?
    add_error("Can not perform #{ request_type } on #{ actual_path }. File not found")
  elsif create?
    FileUtils.mkdir_p(File.dirname(file))

    file.open("w+") {|fh| fh.write(contents) }
    load_path_meta
  end
end

#has_errors?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/stylish/developer/modification.rb', line 55

def has_errors?
  body.key?(:errors) && !body[:errors].empty?
end

#load_path_metaObject



116
117
118
# File 'lib/stylish/developer/modification.rb', line 116

def load_path_meta
  @meta_path = Stylish::Developer::Path.new(actual_path, "meta")
end

#to_rack_responseObject



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/stylish/developer/modification.rb', line 126

def to_rack_response
  handle

  return @meta_path.to_rack_response if @meta_path

  [
    code,
    headers,
    [body_contents]
  ]
end

#update?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/stylish/developer/modification.rb', line 43

def update?
  request_type == "update"
end