Class: Olelo::Page

Inherits:
Object show all
Includes:
Attributes, Hooks, Util
Defined in:
lib/olelo/page.rb

Overview

Wiki page object

Constant Summary collapse

PATH_PATTERN =

Pattern for valid paths

'[^\s](?:.*[^\s]+)?'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attributes

#attribute_editor, included, #update_attributes

Methods included from Hooks

included, #invoke_hook, #with_hooks

Methods included from Util

#check, #decode64, #deep_copy, #encode64, #escape, #escape_html, #escape_javascript, included, #md5, #no_cache?, #sha256, #titlecase, #truncate, #unescape, #unescape_backslash, #unescape_html, #valid_xml_chars?, #yaml_dump, #yaml_load, #yaml_load_file

Constructor Details

#initialize(path, tree_version = nil, etag = nil, parent = nil) ⇒ Page

Returns a new instance of Page.



42
43
44
45
# File 'lib/olelo/page.rb', line 42

def initialize(path, tree_version = nil, etag = nil, parent = nil)
  @path, @etag, @tree_version, @parent = path.to_s.cleanpath.freeze, etag, tree_version, parent
  Page.check_path(@path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



40
41
42
# File 'lib/olelo/page.rb', line 40

def path
  @path
end

#tree_versionObject (readonly)

Returns the value of attribute tree_version.



40
41
42
# File 'lib/olelo/page.rb', line 40

def tree_version
  @tree_version
end

Class Method Details

.commit(comment) ⇒ Object



59
60
61
62
63
# File 'lib/olelo/page.rb', line 59

def self.commit(comment)
  tree_version = repository.commit(comment)
  current_transaction.each {|proc| proc.call(tree_version) }
  current_transaction.clear
end

.current_transactionObject



55
56
57
# File 'lib/olelo/page.rb', line 55

def self.current_transaction
  Thread.current[:olelo_tx] || raise('No transaction running')
end

.default_mimeObject



226
227
228
229
# File 'lib/olelo/page.rb', line 226

def self.default_mime
  mime = Config['mime'].find {|m| m.include? '/'}
  mime ? MimeMagic.new(mime) : nil
end

.find(path, tree_version = nil) ⇒ Object

Throws exceptions if access denied, returns nil if not found



66
67
68
69
70
71
72
73
74
# File 'lib/olelo/page.rb', line 66

def self.find(path, tree_version = nil)
  path = path.to_s.cleanpath
  check_path(path)
  tree_version = repository.get_version(tree_version) unless Version === tree_version
  if tree_version
    etag = repository.path_etag(path, tree_version)
    Page.new(path, tree_version, etag) if etag
  end
end

.find!(path, tree_version = nil) ⇒ Object

Throws if not found



77
78
79
# File 'lib/olelo/page.rb', line 77

def self.find!(path, tree_version = nil)
  find(path, tree_version) || raise(NotFound, path)
end

.transaction(&block) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/olelo/page.rb', line 47

def self.transaction(&block)
  raise 'Transaction already running' if Thread.current[:olelo_tx]
  Thread.current[:olelo_tx] = []
  repository.transaction(&block)
ensure
  Thread.current[:olelo_tx] = nil
end

Instance Method Details

#attributesObject



165
166
167
# File 'lib/olelo/page.rb', line 165

def attributes
  @attributes ||= deep_copy(saved_attributes)
end

#attributes=(a) ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/olelo/page.rb', line 173

def attributes=(a)
  a ||= {}
  if attributes != a
    @attributes = a
    @mime = nil
  end
  raise :invalid_mime_type.t if attributes['mime'] && attributes['mime'] != mime.to_s
end

#childrenObject



215
216
217
218
219
220
221
222
223
224
# File 'lib/olelo/page.rb', line 215

def children
  @children ||=
    if new?
      []
    else
      repository.get_children(path, tree_version).sort.map do |name|
        Page.new(path/name, tree_version, nil, self)
      end
    end
end

#contentObject



186
187
188
# File 'lib/olelo/page.rb', line 186

def content
  @content ||= saved_content
end

#content=(c) ⇒ Object



190
191
192
193
194
195
# File 'lib/olelo/page.rb', line 190

def content=(c)
  if content != c
    @mime = nil
    @content = c
  end
end

#deleteObject



135
136
137
138
139
140
# File 'lib/olelo/page.rb', line 135

def delete
  raise 'Page is not head' unless head?
  raise 'Page is new' if new?
  with_hooks(:delete) { repository.delete(path) }
  after_commit {|tree_version| update(path, nil) }
end

#diff(from, to) ⇒ Object



142
143
144
145
# File 'lib/olelo/page.rb', line 142

def diff(from, to)
  raise 'Page is new' if new?
  repository.diff(path, from, to)
end

#editable?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/olelo/page.rb', line 90

def editable?
  mime.text? || mime == EMPTY_MIME || mime == DIRECTORY_MIME
end

#etagObject



94
95
96
97
98
99
# File 'lib/olelo/page.rb', line 94

def etag
  unless new?
    @etag ||= repository.path_etag(path, tree_version)
    "#{Olelo::VERSION}-#{head? ? 1 : 0}-#{@etag}"
  end
end

#extensionObject



160
161
162
163
# File 'lib/olelo/page.rb', line 160

def extension
  i = path.index('.')
  i ? path[i+1..-1] : ''
end

#head?Boolean

Head version

Returns:

  • (Boolean)


82
83
84
# File 'lib/olelo/page.rb', line 82

def head?
  new? || tree_version.head?
end

#history(skip, limit) ⇒ Object



116
117
118
119
# File 'lib/olelo/page.rb', line 116

def history(skip, limit)
  raise 'Page is new' if new?
  repository.get_history(path, skip, limit)
end

#mimeObject



211
212
213
# File 'lib/olelo/page.rb', line 211

def mime
  @mime ||= detect_mime
end

#modified?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/olelo/page.rb', line 197

def modified?
  content != saved_content || attributes != saved_attributes
end

#move(destination) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/olelo/page.rb', line 125

def move(destination)
  raise 'Page is not head' unless head?
  raise 'Page is new' if new?
  destination = destination.to_s.cleanpath
  Page.check_path(destination)
  raise :already_exists.t(page: destination) if Page.find(destination)
  with_hooks(:move, destination) { repository.move(path, destination) }
  after_commit {|tree_version| update(destination, tree_version) }
end

#nameObject



151
152
153
154
# File 'lib/olelo/page.rb', line 151

def name
  i = path.rindex('/')
  i ? path[i+1..-1] : path
end

#new?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/olelo/page.rb', line 147

def new?
  !tree_version
end

#next_versionObject



101
102
103
104
# File 'lib/olelo/page.rb', line 101

def next_version
  init_versions
  @next_version
end

#parentObject



121
122
123
# File 'lib/olelo/page.rb', line 121

def parent
  @parent ||= Page.find(path/'..', tree_version) || Page.new(path/'..', tree_version) if !root?
end

#previous_versionObject



106
107
108
109
# File 'lib/olelo/page.rb', line 106

def previous_version
  init_versions
  @previous_version
end

#root?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/olelo/page.rb', line 86

def root?
  path.empty?
end

#saveObject



201
202
203
204
205
206
207
208
209
# File 'lib/olelo/page.rb', line 201

def save
  raise 'Page is not head' unless head?
  raise :already_exists.t(page: path) if new? && Page.find(path)
  with_hooks(:save) do
    repository.set_content(path, content)
    repository.set_attributes(path, attributes)
  end
  after_commit {|tree_version| update(path, tree_version) }
end

#saved_attributesObject



169
170
171
# File 'lib/olelo/page.rb', line 169

def saved_attributes
  @saved_attributes ||= new? ? {} : repository.get_attributes(path, tree_version)
end

#saved_contentObject



182
183
184
# File 'lib/olelo/page.rb', line 182

def saved_content
  @saved_content ||= new? ? '' : repository.get_content(path, tree_version)
end

#titleObject



156
157
158
# File 'lib/olelo/page.rb', line 156

def title
  attributes['title'] || (root? ? :root.t : name)
end

#versionObject



111
112
113
114
# File 'lib/olelo/page.rb', line 111

def version
  init_versions
  @version
end