Class: Stylish::Developer::Modification
- Inherits:
-
Object
- Object
- Stylish::Developer::Modification
- 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
-
#actual_path ⇒ Object
readonly
Returns the value of attribute actual_path.
-
#body ⇒ Object
Returns the value of attribute body.
-
#code ⇒ Object
Returns the value of attribute code.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#request_type ⇒ Object
readonly
Returns the value of attribute request_type.
Instance Method Summary collapse
- #add_error(message) ⇒ Object
- #body_contents ⇒ Object
- #contents ⇒ Object
- #create? ⇒ Boolean
- #current_root ⇒ Object
- #delete? ⇒ Boolean
- #file ⇒ Object
- #file_exist? ⇒ Boolean
- #file_exists? ⇒ Boolean
- #file_writable? ⇒ Boolean
- #handle ⇒ Object
- #handle_access_denied_error ⇒ Object
- #handle_existing_file_modification ⇒ Object
- #handle_non_existing_file_modification ⇒ Object
- #has_errors? ⇒ Boolean
-
#initialize(actual_path, request_type, request) ⇒ Modification
constructor
A new instance of Modification.
- #load_path_meta ⇒ Object
- #to_rack_response ⇒ Object
- #update? ⇒ Boolean
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_path ⇒ Object (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 |
#body ⇒ Object
Returns the value of attribute body.
11 12 13 |
# File 'lib/stylish/developer/modification.rb', line 11 def body @body end |
#code ⇒ Object
Returns the value of attribute code.
11 12 13 |
# File 'lib/stylish/developer/modification.rb', line 11 def code @code end |
#headers ⇒ Object
Returns the value of attribute headers.
11 12 13 |
# File 'lib/stylish/developer/modification.rb', line 11 def headers @headers end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
7 8 9 |
# File 'lib/stylish/developer/modification.rb', line 7 def request @request end |
#request_type ⇒ Object (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() self.code = 500 binding.pry body[:errors] ||= [] body[:errors] << end |
#body_contents ⇒ Object
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 |
#contents ⇒ Object
101 102 103 |
# File 'lib/stylish/developer/modification.rb', line 101 def contents request.params['contents'] end |
#create? ⇒ Boolean
51 52 53 |
# File 'lib/stylish/developer/modification.rb', line 51 def create? request_type == "create" end |
#current_root ⇒ Object
23 24 25 |
# File 'lib/stylish/developer/modification.rb', line 23 def current_root Stylish::Developer.server.root end |
#delete? ⇒ Boolean
47 48 49 |
# File 'lib/stylish/developer/modification.rb', line 47 def delete? request_type == "delete" end |
#file ⇒ Object
27 28 29 |
# File 'lib/stylish/developer/modification.rb', line 27 def file current_root.join(actual_path) end |
#file_exist? ⇒ Boolean
35 36 37 |
# File 'lib/stylish/developer/modification.rb', line 35 def file_exist? file_exists? end |
#file_exists? ⇒ Boolean
31 32 33 |
# File 'lib/stylish/developer/modification.rb', line 31 def file_exists? file && file.exist? end |
#file_writable? ⇒ Boolean
39 40 41 |
# File 'lib/stylish/developer/modification.rb', line 39 def file_writable? file && file.writable? end |
#handle ⇒ Object
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_error ⇒ Object
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_modification ⇒ Object
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) } 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_modification ⇒ Object
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) } end end |
#has_errors? ⇒ Boolean
55 56 57 |
# File 'lib/stylish/developer/modification.rb', line 55 def has_errors? body.key?(:errors) && !body[:errors].empty? end |
#load_path_meta ⇒ Object
116 117 118 |
# File 'lib/stylish/developer/modification.rb', line 116 def @meta_path = Stylish::Developer::Path.new(actual_path, "meta") end |
#to_rack_response ⇒ Object
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
43 44 45 |
# File 'lib/stylish/developer/modification.rb', line 43 def update? request_type == "update" end |