gollum_rails 1.0.6 → 1.4.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,11 +20,7 @@ module GollumRails
20
20
  attr_writer :page_class
21
21
 
22
22
  # Sets the wiki class used by all instances
23
- attr_writer :wiki_path
24
-
25
- # Sets the wiki options
26
- attr_writer :wiki_options
27
-
23
+ attr_writer :wiki_class
28
24
 
29
25
  # Sets the applications status
30
26
  attr_writer :enabled
@@ -42,18 +38,13 @@ module GollumRails
42
38
  def page_class
43
39
  @page_class || Page
44
40
  end
45
-
46
- # Gets the wiki options
47
- def wiki_options
48
- @wiki_options || {}
49
- end
50
41
 
51
42
  # Gets the Globally used Page class or use a new one if not defined
52
43
  #
53
44
  #
54
45
  # Returns the internal page class or a fresh ::Gollum::Page
55
- def wiki_path
56
- @wiki_path || ""
46
+ def wiki_class
47
+ @wiki_class || Wiki
57
48
  end
58
49
 
59
50
  end
@@ -18,99 +18,63 @@ module GollumRails
18
18
 
19
19
  Connector.page_class = self
20
20
 
21
- class << self
22
-
23
- # == Parses a given filepath e.g. '/test/page'
24
- # Name is page
25
- # path is /test
26
- #
27
- # Returns a Hash
28
- def parse_path(name)
29
- path = '/'
30
- name = name.to_s
31
- if name.include?('/')
32
- name = name[1..-1] if name[0] == "/"
33
- content = name.split('/')
34
- name = content.pop
35
- path = '/'+content.join('/')
36
- end
37
- { path: path, name: name }
38
- end
39
-
40
- # finds all versions of a page
41
- #
42
- # name - the pagename to search
43
- # wiki - instance of Gollum::Wiki
44
- # version - optional - The pages version
45
- #
46
- # Returns the Gollum::Page class
47
- def find_page(name, wiki, version=nil)
48
- wiki.clear_cache
49
- path_data = parse_path(name)
50
- wiki.paged(path_data[:name], path_data[:path], exact = true, version)
51
- end
21
+ # Gets / Sets current page
22
+ attr_accessor :page
23
+
24
+ # Gets / Sets the wiki
25
+ attr_accessor :wiki
26
+
27
+ # Initializer
28
+ def initialize
29
+ @wiki = Connector.wiki_class
52
30
  end
53
31
 
54
- # == Creates a new page
32
+
33
+ # creates a new Page
55
34
  #
56
- # name - The name of the page
57
- # content - The content of the page
58
- # wiki - An instance of Gollum::Wiki
59
- # type - A filetype as symbol (optional)
60
- # commit - Commit Hash
35
+ # name - String
36
+ # type - Symbol
37
+ # content - Text
38
+ # commit - Hash or instance of Committer
61
39
  #
62
- # Returns the page
63
- def new_page( name, content, wiki, type=:markdown, commit={} )
64
- path_data = self.class.parse_path(name)
65
- wiki.write_page( path_data[:name], type, content, commit, path_data[:path].gsub!(/^\//, "").gsub!(/(\/)+$/,'') || "" )
66
- wiki.clear_cache
67
- self.class.find_page( name, wiki )
40
+ # Returns the commit id
41
+ def new_page( name, content,type=:markdown, commit={} )
42
+ @wiki.write_page name.to_s, type, content, commit if name
43
+ @page = @wiki.page name
44
+ @page
68
45
  end
69
46
 
70
- # == Updates an existing page
71
- #
72
- # page - An instance of Gollum::Page
73
- # wiki - An instance of Gollum::Wiki
74
- # content - New content
75
- # commit - Commit Hash
76
- # name - A new String (optional)
77
- # format - A filetype as symbol (optional)
78
- #
79
- # Returns the page
80
- def update_page( page, wiki, content=nil, commit={}, name=nil, format=nil)
81
- wiki.clear_cache
82
- return page if page.nil?
83
- name ||= page.name
84
- format = (format || page.format).to_sym
85
- content ||= page.raw_data
86
-
87
- wiki.update_page(page,name,format,content.to_s,commit) unless ((!content||page.raw_data == content) && page.format == format)
88
-
89
- self.class.find_page( mixin(page.url_path, name), wiki )
90
- end
91
-
92
- # == Preview page
47
+ # updates an existing page
93
48
  #
94
- # wiki - An instance of Gollum::Wiki
95
- # content - New content
96
- # name - A String
97
- # format - A filetype as symbol (optional)
49
+ # new - Hash with changed data
50
+ # commit - Hash or instance of Committer
51
+ # old - also an instance of self
98
52
  #
99
- def preview_page(wiki, name, content, format=:markdown)
100
- page = wiki.preview_page(name,content,format)
101
- page.formatted_data
53
+ # Returns the commit id
54
+ def update_page( new, commit={}, old=nil)
55
+ if new.is_a?(Hash)
56
+ commit_id = @wiki.update_page (old||@page),
57
+ new[:name]||@page.name,
58
+ new[:format]||@page.format,
59
+ new[:content]||@page.raw_data,
60
+ commit
61
+ else
62
+ raise Error.new "commit must be a Hash. #{new.class} given", :crit
63
+ end
64
+
65
+ # this is very ugly. Shouldn't gollum return the new page?
66
+ @page = @page.find(new[:name]||@page.name, commit_id)
67
+ @page
102
68
  end
103
69
 
104
- # == Deletes an existing page
70
+ # deletes an existing page
105
71
  #
106
- # page - Gollum::Page
107
- # wiki - Gollum::Wiki
108
- # commit - Commit Hash
72
+ # page - instance of self
73
+ # commit - Hash or instance of Committer
109
74
  #
110
75
  # Returns the commit id
111
- def delete_page( page,wiki,commit={} )
112
- wiki.clear_cache
113
- wiki.delete_page(page, commit)
76
+ def delete_page( commit={}, page = nil )
77
+ @wiki.delete_page (page||@page), commit
114
78
  end
115
79
 
116
80
  # renames an existing page
@@ -130,8 +94,7 @@ module GollumRails
130
94
  #
131
95
  # Returns the Gollum::Page class
132
96
  def find_page(name)
133
-
134
- self.class.find_page(name)
97
+ @wiki.page ::Gollum::Page.cname(name)
135
98
  end
136
99
 
137
100
  # moves an existing page
@@ -207,16 +170,6 @@ module GollumRails
207
170
  raise Error.new "page cannot be empty for #{__method__}", :high
208
171
  end
209
172
  end
210
-
211
- private
212
-
213
- # replaces old filename with new
214
- def mixin(old_url, new_name)
215
- url = old_url.split("/")
216
- url.pop
217
- url << new_name
218
- url.join("/")
219
- end
220
173
 
221
174
  end
222
175
  end
@@ -6,28 +6,27 @@ module GollumRails
6
6
  # TODO: doc
7
7
  class Wiki
8
8
 
9
+ # Gets / Sets the git path or object
10
+ attr_accessor :git
11
+
9
12
  # Initializes the class
10
13
  #
11
14
  # location - String or Grit::Repo
12
- def initialize(location, options={})
13
-
14
- Connector.wiki_options = options
15
-
16
- if location.is_a?(::String)
17
- con = location
15
+ def initialize(location)
16
+ @git = location
17
+ if location.is_a? ::String
18
+ con = ::Gollum::Wiki.new @git
18
19
  else
19
- con = location.path
20
+ con= ::Gollum::Wiki.new @git.path
20
21
  end
21
-
22
- Connector.wiki_path = con
23
-
22
+ Connector.wiki_class = con
24
23
  end
25
24
 
26
25
  # Static call from within any other class
27
26
  #
28
27
  # Returns a new instance of this class
29
- def self.wiki(location, options={})
30
- Wiki.new(location, options)
28
+ def self.wiki(location)
29
+ Wiki.new(location)
31
30
  end
32
31
 
33
32
  # Forwards unknown methods to Gollum::Wiki
@@ -0,0 +1,33 @@
1
+ # See http://ruby-doc.org/core-2.0/Hash.html for further information
2
+ #
3
+ # Extended methods:
4
+ # * object support
5
+ # * setter
6
+ # * hash setter
7
+ # * isset
8
+ #
9
+ #
10
+ # TODO
11
+ # * implement is? method
12
+ # * improve testing
13
+ #
14
+ class ::Hash
15
+
16
+
17
+ # Public: Converts a method . into an Hash element
18
+ #
19
+ # Examples
20
+ # hash = {a: "b", b: "c", c: "d"}
21
+ # hash.a
22
+ # # => "b"
23
+ # # hash.b
24
+ # # => "c"
25
+ #
26
+ # Returns an instance of Hash if the name is the key of a new hash
27
+ def method_missing(name, *args)
28
+ return self[name] if key? name
29
+ self.each { |k,v| return v if k.to_s.to_sym == name }
30
+ super.method_missing name
31
+ end
32
+
33
+ end
@@ -0,0 +1,5 @@
1
+ module GollumRails
2
+ ::Dir.glob(::File.expand_path(::File.dirname(__FILE__)) + '/*.rb') do |file|
3
+ require file if file != __FILE__
4
+ end
5
+ end
@@ -12,34 +12,42 @@ module GollumRails
12
12
  # * new
13
13
  # * save
14
14
  # * delete
15
- # * find_or_initialize_by_name
15
+ # * find_or_initialize_by_naname
16
16
  #
17
17
  class Page
18
- extend ActiveModel::Naming
19
- extend ActiveModel::Callbacks
20
-
21
- include ActiveModel::Conversion
22
- include ActiveModel::Validations
18
+ extend ::ActiveModel::Callbacks
19
+ include ::ActiveModel::Validations
20
+ include ::ActiveModel::Conversion
21
+ extend ::ActiveModel::Naming
23
22
 
24
- define_model_callbacks :save, :update, :delete, :initialize, :create, :commit
25
23
 
26
-
24
+ # Callback for save
25
+ define_model_callbacks :save
26
+
27
+ # Callback for update
28
+ define_model_callbacks :update
29
+
30
+ # Callback for delete
31
+ define_model_callbacks :delete
32
+
27
33
  # static
28
34
  class << self
29
-
35
+ # Gets / Sets the gollum page
36
+ #
37
+ attr_accessor :gollum_page
38
+
39
+ # Sets the validator
40
+ attr_writer :validator
41
+
30
42
  # Finds an existing page or creates it
31
43
  #
32
44
  # name - The name
33
45
  # commit - Hash
34
46
  #
35
47
  # Returns self
36
- def find_or_initialize_by_name(name, commit={})
37
- result_for_find = find(name)
38
- unless result_for_find.nil?
39
- result_for_find
40
- else
41
- new(:format => :markdown, :name => name, :content => ".", :commit => commit)
42
- end
48
+ def find_or_initialize_by_name(name, commit)
49
+ result_for_find = self.find(name)
50
+ self.create({:format => :markdown, :name => name, :content => ".", :commit => commit })
43
51
  end
44
52
 
45
53
  # Checks if the fileformat is supported
@@ -48,88 +56,67 @@ module GollumRails
48
56
  #
49
57
  # Returns true or false
50
58
  def format_supported?(format)
51
- Gollum::Markup.formats.include?(format.to_sym)
59
+ supported = ['ascii', 'github-markdown','markdown', 'creole', 'org', 'pod', 'rdoc']
60
+ format.in?(supported)
52
61
  end
53
62
 
54
63
  # first creates an instance of itself and executes the save function.
55
64
  #
56
- # data - Hash containing the page data
65
+ # hash - Hash containing the page data
57
66
  #
58
67
  #
59
68
  # Returns an instance of Gollum::Page or false
60
- def create(data)
61
- page = Page.new(data)
69
+ def create(hash)
70
+ page = Page.new hash
62
71
  page.save
63
72
  end
64
73
 
65
74
 
66
75
  # calls `create` on current class. If returned value is nil an exception will be thrown
67
76
  #
68
- # data - Hash of Data containing all necessary stuff
69
- # TODO write this stuff
77
+ # hash - Hash containing the page data
70
78
  #
71
79
  #
72
80
  # Returns an instance of Gollum::Page
73
- def create!(data)
74
- page = Page.new(data)
75
- page.save!
81
+ def create!(hash)
82
+ action = self.create(hash)
83
+ action
76
84
  end
77
85
 
78
86
  # Finds a page based on the name and specified version
79
87
  #
80
88
  # name - the name of the page
81
- # version - optional - The pages version
82
89
  #
83
90
  # Return an instance of Gollum::Page
84
- def find(name, version=nil)
85
- page = Adapters::Gollum::Page.find_page(name, wiki, version)
86
-
87
- return new( :gollum_page => page ) unless page.nil?
88
- return nil
89
-
91
+ def find(name)
92
+ page = GollumRails::Adapters::Gollum::Page.new
93
+ page.find_page name
90
94
  end
91
95
 
92
96
  # Gets all pages in the wiki
93
97
  def all
94
- wiki.pages
98
+ self.wiki.pages
95
99
  end
96
-
97
100
  alias_method :find_all, :all
98
101
 
99
102
  # Gets the wiki instance
100
103
  def wiki
101
- @wiki ||= ::Gollum::Wiki.new(Adapters::Gollum::Connector.wiki_path, Adapters::Gollum::Connector.wiki_options)
102
- end
103
-
104
- # TODO: implement more of this (format, etc)
105
- #
106
- def method_missing(name, *args)
107
- if name =~ /^find_by_(name)$/
108
- self.find(args.first)
109
- else
110
- raise NoMethodError, "undefined method `#{name}` for #{self}:#{self.class}"
111
- end
104
+ @wiki || Adapters::Gollum::Connector.wiki_class
112
105
  end
113
106
 
114
107
  end
115
108
 
116
- # Gets / Sets the gollum page
117
- #
118
- attr_accessor :gollum_page
119
-
109
+
120
110
  # Initializes a new Page
121
111
  #
122
112
  # attrs - Hash of attributes
123
113
  #
124
114
  # commit must be given to perform any page action!
125
115
  def initialize(attrs = {})
126
- run_callbacks :initialize do
127
- if Adapters::Gollum::Connector.enabled
128
- attrs.each{|k,v| self.public_send("#{k}=",v)} if attrs
129
- update_attrs if attrs[:gollum_page]
130
- else
131
- raise GollumInternalError, 'gollum_rails is not enabled!'
132
- end
116
+ if Adapters::Gollum::Connector.enabled
117
+ attrs.each{|k,v| self.public_send("#{k}=",v)} if attrs
118
+ else
119
+ raise GollumInternalError, 'gollum_rails is not enabled!'
133
120
  end
134
121
  end
135
122
 
@@ -139,10 +126,10 @@ module GollumRails
139
126
 
140
127
 
141
128
  # Gets / Sets the pages name
142
- attr_writer :name
129
+ attr_accessor :name
143
130
 
144
131
  # Gets / Sets the contents content
145
- attr_writer :content
132
+ attr_accessor :content
146
133
 
147
134
  # Gets / Sets the commit Hash
148
135
  attr_accessor :commit
@@ -158,24 +145,14 @@ module GollumRails
158
145
 
159
146
  # Gets the pages format
160
147
  def format
161
- (@format || @gollum_page.format).to_sym
148
+ @format.to_sym
162
149
  end
163
150
 
164
- def name
165
- @name ||= @gollum_page.name
166
- end
167
-
168
- def content
169
- @content ||= @gollum_page.content
170
- end
171
151
 
172
152
  # Gets the page class
173
153
  def page
174
- Adapters::Gollum::Page.new
154
+ @page ||= Adapters::Gollum::Connector.page_class.new
175
155
  end
176
-
177
- # Gollum Page
178
- attr_accessor :gollum_page
179
156
 
180
157
  #############
181
158
  # activemodel
@@ -204,77 +181,58 @@ module GollumRails
204
181
  # Returns an instance of Gollum::Page or false
205
182
  def save
206
183
  run_callbacks :save do
207
- return nil unless valid?
184
+ return false unless valid?
208
185
  begin
209
- @gollum_page = page.new_page(name,content,wiki,format,commit)
186
+ page.new_page(name,content,format,commit)
210
187
  rescue ::Gollum::DuplicatePageError => e
211
- @gollum_page = Adapters::Gollum::Page.find_page(name, wiki)
188
+ page.page = page.find_page(name)
212
189
  end
213
- return self
190
+ return page.page
214
191
  end
215
192
  end
216
193
 
217
- # == Save without exception handling
218
- # raises errors everytime something is wrong
194
+ # aliasing save
219
195
  #
220
- def save!
221
- run_callbacks :save do
222
- raise StandardError, "record is not valid" unless valid?
223
- raise StandardError, "commit could not be empty" if commit == {}
224
- @gollum_page = page.new_page(name,content,wiki,format,commit)
225
- return self
226
- end
227
- end
196
+ # TODO:
197
+ # * implement full method!
198
+ alias_method :save!, :save
228
199
 
229
- # == Updates an existing page (or created)
200
+ # Updates an existing page (or created)
230
201
  #
231
- # content - optional. If given the content of the gollum_page will be updated
232
- # name - optional. If given the name of gollum_page will be updated
233
- # format - optional. Updates the format. Uses markdown as default
202
+ # hash - Hash containing the attributes, you want to update
234
203
  # commit - optional. If given this commit will be used instead of that one, used
235
204
  # to initialize the instance
236
205
  #
237
206
  #
238
- # Returns an instance of GollumRails::Page
239
- def update_attributes(content=nil,name=nil,format=:markdown, commit=nil)
207
+ # Returns an instance of Gollum::Page
208
+ def update_attributes(hash, commit=nil)
240
209
  run_callbacks :update do
241
- @gollum_page = page.update_page(gollum_page, wiki, content, get_right_commit(commit), name, format)
242
- end
243
- end
244
-
245
- # == Deletes current page (also available static. See below)
246
- #
247
- # commit - optional. If given this commit will be used instead of that one, used
248
- # to initialize the instance
249
- #
250
- # Returns the commit id of the current action as String
251
- def destroy(commit=nil)
252
- run_callbacks :delete do
253
- page.delete_page(gollum_page, wiki, get_right_commit(commit))
210
+ page.update_page hash, get_right_commit(commit)
254
211
  end
255
212
  end
256
213
 
257
- # == Deletes current page (also available static. See below)
214
+ # Deletes current page (also available static. See below)
258
215
  #
259
216
  # commit - optional. If given this commit will be used instead of that one, used
260
217
  # to initialize the instance
261
218
  #
262
219
  # Returns the commit id of the current action as String
263
220
  def delete(commit=nil)
264
- destroy(commit)
221
+ run_callbacks :delete do
222
+ page.delete_page get_right_commit(commit)
223
+ end
265
224
  end
266
225
 
267
226
  # checks if entry already has been saved
268
227
  #
269
228
  #
270
229
  def persisted?
271
- return true if gollum_page
230
+ return true if page.page.instance_of?(::Gollum::Page)
272
231
  return false
273
232
  end
274
-
275
- # == Previews the page - Mostly used if you want to see what you do before saving
233
+ # Previews the page - Mostly used if you want to see what you do before saving
276
234
  #
277
- # This is an extremely fast method!
235
+ # This is an extremely performant method!
278
236
  # 1 rendering attempt take depending on the content about 0.001 (simple markdown)
279
237
  # upto 0.004 (1000 chars markdown) seconds, which is quite good
280
238
  #
@@ -284,91 +242,16 @@ module GollumRails
284
242
  #
285
243
  # Returns a String
286
244
  def preview(format=:markdown)
287
- page.preview_page( wiki, name, content, format )
245
+ preview = self.class.wiki.preview_page @name, @content, format
246
+ preview.formatted_data
288
247
  end
289
248
 
290
- # == Gets the url for current page from Gollum::Page
291
- #
292
- # Returns a String
293
- def url
294
- gollum_page.url_path
295
- end
296
-
297
- # == Gets the title for current Gollum::Page
298
- #
299
- # Returns a String
300
- def title
301
- gollum_page.title
302
- end
303
-
304
- # == Gets formatted_data for current Gollum::Page
305
- #
306
- # Returns a String
307
- def html_data
308
- gollum_page.formatted_data
309
- end
310
-
311
-
312
- # == Gets raw_data for current Gollum::Page
313
- #
314
- # Returns a String
315
- def raw_data
316
- gollum_page.raw_data
317
- end
318
-
319
- # == Gets the history of current gollum_page
320
- #
321
- # Returns an Array
322
- def history
323
- return nil unless persisted?
324
- gollum_page.versions
325
- end
326
-
327
- # == Gets the last modified by Gollum::Committer
328
- #
329
- # Returns a String
330
- def last_changed_by
331
- "%s <%s>" % [history.last.author.name, history.last.author.email]
332
- end
333
-
334
- # == Compare 2 Commits.
335
- #
336
- # sha1 - SHA1
337
- # sha2 - SHA1
338
- def compare_commits(sha1,sha2=nil)
339
- Page.wiki.full_reverse_diff_for(@gollum_page,sha1,sha2)
340
- end
341
-
342
- # == The pages filename, based on the name and the format
343
- #
344
- # Returns a String
345
- def filename
346
- Page.wiki.page_file_name(@name, @format)
347
- end
348
-
349
- # == Checks if current page is a subpage
350
- def sub_page?
351
- return nil unless persisted?
352
- @gollum_page.sub_page
353
- end
354
-
355
- # == Gets the version of current commit
356
- #
357
- def current_version(long=false)
358
- return nil unless persisted?
359
- unless long
360
- @gollum_page.version_short
361
- else
362
- @gollum_page.version.to_s
363
- end
364
-
365
- end
366
-
249
+
367
250
  #######
368
251
  private
369
252
  #######
370
253
 
371
- # == Gets the right commit out of 2 commits
254
+ # Get the right commit out of 2 commits
372
255
  #
373
256
  # commit_local - local commit Hash
374
257
  #
@@ -378,19 +261,6 @@ module GollumRails
378
261
  com = commit_local if !commit_local.nil?
379
262
  return com
380
263
  end
381
-
382
- # == Updates local attributes from gollum_page class
383
- #
384
- def update_attrs
385
- @name = gollum_page.name
386
- @content= gollum_page.raw_data
387
- @format = gollum_page.format
388
- end
389
-
390
- # == To static
391
- def wiki
392
- self.class.wiki
393
- end
394
-
264
+
395
265
  end
396
266
  end