Class: Brief::Server::Handlers::Modify::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/brief/server/handlers/modify.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(briefcase, path_args, params) ⇒ Writer

Returns a new instance of Writer.



23
24
25
26
27
28
29
30
# File 'lib/brief/server/handlers/modify.rb', line 23

def initialize(briefcase, path_args, params)
  @params = params
  @briefcase = briefcase
  @errors = {}
  @path = briefcase.normalize_path(path_args)

  raise "Invalid path for document write" unless @path
end

Instance Attribute Details

#briefcaseObject

Returns the value of attribute briefcase.



21
22
23
# File 'lib/brief/server/handlers/modify.rb', line 21

def briefcase
  @briefcase
end

#paramsObject

Returns the value of attribute params.



21
22
23
# File 'lib/brief/server/handlers/modify.rb', line 21

def params
  @params
end

#pathObject

Returns the value of attribute path.



21
22
23
# File 'lib/brief/server/handlers/modify.rb', line 21

def path
  @path
end

Instance Method Details

#createObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/brief/server/handlers/modify.rb', line 49

def create
  data      = params[:data]
  contents  = params[:content] || params[:contents]
  raw       = params[:raw]

  if path && path.exist?
    @errors[:path] = "Path already exists"
    return @errors
  end

  if !Brief::Util.ensure_child_path(briefcase.root, path)
    @errors[:path] = "Invalid Path"
    return @errors
  end

  doc = Brief::Document.new(path).tap do |document|
    document.in_briefcase(briefcase)

    if raw
      document.raw = raw
    elsif contents || data
      document.content = contents if contents.to_s.length > 0
      document.data = data if data && !data.empty?
    end

    document.save!
  end

  doc.to_model.as_json(content: true, rendered: true)
end

#deleteObject



124
125
126
# File 'lib/brief/server/handlers/modify.rb', line 124

def delete
  remove
end

#errorsObject



128
129
130
# File 'lib/brief/server/handlers/modify.rb', line 128

def errors
  @errors || {}
end

#has_errors?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/brief/server/handlers/modify.rb', line 45

def has_errors?
  not ok?
end

#ok?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/brief/server/handlers/modify.rb', line 41

def ok?
  @errors.empty?
end

#removeObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/brief/server/handlers/modify.rb', line 80

def remove
  if !Brief::Util.ensure_child_path(briefcase.root, path)
    @errors[:path] = "Invalid Path"
    return @errors
  end

  doc = Brief::Document.new(path)
  doc.path.unlink rescue nil

  {
    success: ok?,
    path: path
  }
end

#run(action) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/brief/server/handlers/modify.rb', line 32

def run(action)
  if !respond_to?(action)
    @errors[:action] = "invalid"
    return @errors
  end

  send(action)
end

#updateObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/brief/server/handlers/modify.rb', line 95

def update
  data      = params[:data]
  contents  = params[:content] || params[:contents]
  raw       = params[:raw]

  if !Brief::Util.ensure_child_path(briefcase.root, path)
    @errors[:path] = "Invalid Path"
    return @errors
  end

  document  = briefcase.document_at(path)

  if document.nil? || !document.exist?
    @errors[:document] = "No document was found at #{ path_args }"
    @errors
  else
    if raw
      document.raw = raw
    elsif contents || data
      document.content = contents if contents.to_s.length > 0
      document.data = (document.data || {}).merge(params[:data]) if params[:data].is_a?(Hash)
    end

    document.save

    document.to_model.as_json(content: true, rendered: true)
  end
end