gollum_rails 0.0.2.8 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +6 -4
  3. data/Gemfile.lock +151 -151
  4. data/HISTORY.md +43 -15
  5. data/LICENSE +661 -0
  6. data/README.md +75 -185
  7. data/Rakefile +170 -160
  8. data/examples/rails/initializer.rb +0 -0
  9. data/gollum_rails.gemspec +72 -57
  10. data/lib/gollum_rails.rb +35 -18
  11. data/lib/gollum_rails/adapters/activemodel.rb +46 -0
  12. data/lib/gollum_rails/adapters/activemodel/boolean.rb +15 -0
  13. data/lib/gollum_rails/adapters/activemodel/callback.rb +61 -0
  14. data/lib/gollum_rails/adapters/activemodel/error.rb +27 -0
  15. data/lib/gollum_rails/adapters/activemodel/naming.rb +42 -0
  16. data/lib/gollum_rails/adapters/activemodel/validation.rb +188 -0
  17. data/lib/gollum_rails/adapters/gollum.rb +61 -0
  18. data/lib/gollum_rails/adapters/gollum/.gitkeep +0 -0
  19. data/lib/gollum_rails/adapters/gollum/committer.rb +9 -0
  20. data/lib/gollum_rails/adapters/gollum/error.rb +19 -0
  21. data/lib/gollum_rails/adapters/gollum/page.rb +177 -0
  22. data/lib/gollum_rails/adapters/gollum/wiki.rb +43 -0
  23. data/lib/gollum_rails/initializer.rb +8 -0
  24. data/lib/gollum_rails/{hash.rb → modules/hash.rb} +33 -20
  25. data/lib/gollum_rails/modules/loader.rb +5 -0
  26. data/lib/gollum_rails/page.rb +283 -406
  27. data/lib/gollum_rails/setup.rb +68 -0
  28. metadata +56 -99
  29. data/LICENSE.md +0 -7
  30. data/lib/generators/gollum_rails/model/model_generator.rb +0 -10
  31. data/lib/gollum_rails/config.rb +0 -36
  32. data/lib/gollum_rails/dependency_injector.rb +0 -24
  33. data/lib/gollum_rails/engine.rb +0 -12
  34. data/lib/gollum_rails/file.rb +0 -9
  35. data/lib/gollum_rails/messages.yml +0 -8
  36. data/lib/gollum_rails/validations.rb +0 -10
  37. data/lib/gollum_rails/versions.rb +0 -79
  38. data/lib/gollum_rails/wiki.rb +0 -36
@@ -0,0 +1,61 @@
1
+ # ~*~ encoding: utf-8 ~*~
2
+ module GollumRails
3
+ module Adapters
4
+ # Gollum Wiki connector classes
5
+ #
6
+ # TODO:
7
+ # * implement
8
+ #
9
+ # FIXME:
10
+ # currently nothing implemented
11
+ #
12
+ module Gollum
13
+ # connector version
14
+ VERSION="0.0.0"
15
+
16
+ # Gollum connector class, keeping defaults!
17
+ #
18
+ class Connector
19
+ class << self
20
+ # Sets the page class used by all instances
21
+ attr_writer :page_class
22
+
23
+ # Sets the wiki class used by all instances
24
+ attr_writer :wiki_class
25
+
26
+ # Sets the committer
27
+ attr_writer :committer_class
28
+
29
+ # Gets the Globally used Page class or use a new one if not defined
30
+ #
31
+ #
32
+ # Returns the internal page class or a fresh ::Gollum::Page
33
+ def page_class
34
+ @page_class || Page
35
+ end
36
+
37
+ # Gets the Globally used Page class or use a new one if not defined
38
+ #
39
+ #
40
+ # Returns the internal page class or a fresh ::Gollum::Page
41
+ def wiki_class
42
+ @wiki_class || Wiki
43
+ end
44
+
45
+ # Gets the current committer or using anon
46
+ #
47
+ #
48
+ def committer_class
49
+ @committer_class || Committer
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ require File.expand_path '../gollum/error', __FILE__
58
+ require File.expand_path '../gollum/wiki', __FILE__
59
+ require File.expand_path '../gollum/committer', __FILE__
60
+ require File.expand_path '../gollum/page', __FILE__
61
+
File without changes
@@ -0,0 +1,9 @@
1
+ module GollumRails
2
+ module Adapters
3
+ module Gollum
4
+ # TODO: doc
5
+ class Committer < ::Gollum::Committer
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ module GollumRails
2
+ module Adapters
3
+ module Gollum
4
+
5
+ # Gollum adapter Error handling class
6
+ #
7
+ # provides better errors
8
+ class Error < ::StandardError
9
+
10
+ # does the formatting of the Error message
11
+ #
12
+ #
13
+ def initialize(error_message, urgence)
14
+ super "#{urgence.upcase} :: #{error_message}"
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,177 @@
1
+ module GollumRails
2
+ module Adapters
3
+ module Gollum
4
+
5
+ # Main page class for the Gollum connector.
6
+ #
7
+ # It provides some awesome features for connecting gollum to gollum_rails such as:
8
+ # * new_page
9
+ # * find_page
10
+ # * delete_page
11
+ # * rename_page
12
+ # * move_page
13
+ # * first_page_commit
14
+ # * page_creation time
15
+ # * ...
16
+ #
17
+ class Page
18
+
19
+ Connector.page_class = self
20
+
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 = Wiki.class_variable_get(:@@wiki)
30
+ end
31
+
32
+
33
+ # creates a new Page
34
+ #
35
+ # name - String
36
+ # type - Symbol
37
+ # content - Text
38
+ # commit - Hash or instance of Committer
39
+ #
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
45
+ end
46
+
47
+ # updates an existing page
48
+ #
49
+ # new - Hash with changed data
50
+ # commit - Hash or instance of Committer
51
+ # old - also an instance of self
52
+ #
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
68
+ end
69
+
70
+ # deletes an existing page
71
+ #
72
+ # page - instance of self
73
+ # commit - Hash or instance of Committer
74
+ #
75
+ # Returns the commit id
76
+ def delete_page( commit={}, page = nil )
77
+ @wiki.delete_page (page||@page), commit
78
+ end
79
+
80
+ # renames an existing page
81
+ #
82
+ # page - instance of myself
83
+ # newname - new pagename
84
+ # commit - Hash or instance of Committer
85
+ #
86
+ # Returns the commit id
87
+ def rename_page( page, newname, commit={} )
88
+
89
+ end
90
+
91
+ # finds all versions of a page
92
+ #
93
+ # name - the pagename to search
94
+ #
95
+ # Returns the Gollum::Page class
96
+ def find_page(name)
97
+ @wiki.page ::Gollum::Page.cname(name)
98
+ end
99
+
100
+ # moves an existing page
101
+ #
102
+ # TODO:
103
+ # * implement
104
+ def move_page()
105
+ end
106
+
107
+ # gets page last edit date
108
+ #
109
+ # Returns an instance of Time
110
+ def page_last_edited_date
111
+ if @page
112
+ return @page.versions.first.authored_date
113
+ else
114
+ raise Error.new "page cannot be empty for #{__method__}", :high
115
+ end
116
+ end
117
+
118
+ # gets the latest commit
119
+ #
120
+ # Returns an instance of Grit::Commit
121
+ def page_last_commit
122
+ if @page
123
+ return @page.versions.first
124
+ else
125
+ raise Error.new "page cannot be empty for #{__method__}", :high
126
+ end
127
+ end
128
+
129
+ # gets the creation date of the page
130
+ #
131
+ # Returns an instance of Time
132
+ def page_created
133
+ if @page
134
+ return @page.versions.last.authored_date
135
+ else
136
+ raise Error.new "page cannot be empty for #{__method__}", :high
137
+ end
138
+
139
+ end
140
+
141
+ # gets the first page commit
142
+ #
143
+ # Returns an instance of Grit::Commit
144
+ def page_first_commit
145
+ if @page
146
+ return @page.versions.last
147
+ else
148
+ raise Error.new "page cannot be empty for #{__method__}", :high
149
+ end
150
+ end
151
+
152
+ # gets a specific commit version
153
+ #
154
+ # Returns an instance of Grit::Commit
155
+ def page_commit(id)
156
+ if @page
157
+ return @page.versions.each{|v| return v if v.id == id}
158
+ else
159
+ raise Error.new "page cannot be empty for #{__method__}", :high
160
+ end
161
+ end
162
+
163
+ # gets a specific commit time
164
+ #
165
+ # Returns an instance of Time
166
+ def page_commit_date(id)
167
+ if @page
168
+ return @page.versions.each{|v| return v.authored_date if v.id == id}
169
+ else
170
+ raise Error.new "page cannot be empty for #{__method__}", :high
171
+ end
172
+ end
173
+
174
+ end
175
+ end
176
+ end
177
+ end
@@ -0,0 +1,43 @@
1
+ require 'grit'
2
+ module GollumRails
3
+ module Adapters
4
+ module Gollum
5
+
6
+ # TODO: doc
7
+ class Wiki
8
+
9
+ Connector.wiki_class = self
10
+
11
+ # Gets / Sets the git path or object
12
+ attr_accessor :git
13
+
14
+ # Static callers
15
+ class << self
16
+ # Gets / Sets the wiki instance
17
+ attr_accessor :wiki
18
+ end
19
+
20
+ # Initializes the class
21
+ #
22
+ # location - String or Grit::Repo
23
+ def initialize(location)
24
+ gollum = ::Gollum::Wiki
25
+ @git = location
26
+ if location.is_a? ::String
27
+ @@wiki = gollum.new @git
28
+ else
29
+ @@wiki = gollum.new @git.path
30
+ end
31
+ end
32
+
33
+ # Static call from within any other class
34
+ #
35
+ # Returns a new instance of this class
36
+ def self.wiki(location)
37
+ Wiki.new(location)
38
+ end
39
+
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,8 @@
1
+ module GollumRails
2
+
3
+
4
+ # setup
5
+ def setup(&block)
6
+ end
7
+
8
+ end
@@ -1,20 +1,33 @@
1
- # Public: Extend Hash class by a method missing call
2
- class ::Hash
3
-
4
- # Public: Converts a method . into an Hash element
5
- #
6
- # Examples
7
- # hash = {a: "b", b: "c", c: "d"}
8
- # hash.a
9
- # # => "b"
10
- # hash.b
11
- # # => "c"
12
- #
13
- # Returns an instance of Hash if the name is the key of a new hash
14
- # otherwise it will return the value of the key
15
- def method_missing(name)
16
- return self[name] if key? name
17
- self.each { |k,v| return v if k.to_s.to_sym == name }
18
- super.method_missing name
19
- end
20
- end
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
@@ -1,406 +1,283 @@
1
- # ~*~ encoding: utf-8 ~*~
2
- require File.expand_path('../hash', __FILE__)
3
- require File.expand_path('../versions', __FILE__)
4
-
5
- module GollumRails
6
- class Page
7
- include ActiveModel::Conversion
8
- include ActiveModel::Validations
9
- extend ActiveModel::Naming
10
-
11
- # Public: Gets/Sets the name of the document
12
- attr_accessor :name
13
-
14
- # Public: Gets/Sets the content of the document
15
- attr_accessor :content
16
-
17
- # Public: Gets/Sets the format of the document
18
- #
19
- # Examples
20
- # Page.format = :creole
21
- # #or
22
- # Page.format = :markdown
23
- #
24
- # Possible formats are
25
- # - :asciidoc
26
- # - :creole
27
- # - :markdown
28
- # - :org
29
- # - :pod
30
- # - :rdoc
31
- # - :rst
32
- # - :tex
33
- # - :wiki
34
- attr_accessor :format
35
-
36
- # Public: Gets/Sets the commiters credentials
37
- #
38
- # commit - The commit Hash details:
39
- # :message - The String commit message.
40
- # :name - The String author full name.
41
- # :email - The String email address.
42
- # :parent - Optional Grit::Commit parent to this update.
43
- # :tree - Optional String SHA of the tree to create the
44
- # index from.
45
- # :committer - Optional Gollum::Committer instance. If provided,
46
- # assume that this operation is part of batch of
47
- # updates and the commit happens later.
48
- #
49
- # Examples
50
- # commit = {
51
- # message: 'page created',
52
- # name: 'Florian Kasper',
53
- # email: 'nirnanaaa@khnetworks.com'
54
- # }
55
- #
56
- attr_accessor :commit
57
-
58
- #########
59
- # READERs
60
- #########
61
-
62
- # Public: Gets the options
63
- attr_reader :options
64
-
65
- # Public: Gets the persistance of objects by save(), update(), delete() methods
66
- attr_reader :persisted
67
-
68
- # Public: Gets the error messages
69
- attr_reader :error
70
-
71
- # Public: Gets the instance of Gollum::Wiki
72
- attr_reader :wiki
73
-
74
- # Public: Gets ?!
75
- attr_reader :class
76
- # Public: Initializes a new Page instance
77
- #
78
- # attributes - A hash of attributes. See example
79
- # options - Will be merged with the configuration
80
- #
81
- # Examples
82
- #
83
- # GollumRails::Page.new({name: '', content: '', format: '', commit: {}})
84
- #
85
- #
86
- # Explanation
87
- #
88
- # name must be a string.
89
- # content should be a text/String
90
- # format must be eighter :markdown, :latex, :rdoc, ...
91
- # commit must be a hash for example:
92
- #
93
- # Raises RuntimeError if the wiki was not initialized
94
- # Raises RuntimeError if no configuration was provided
95
- #
96
- # Returns an instance of this class
97
- def initialize(attributes = {}, options = {})
98
- wiki = DependencyInjector.wiki
99
- config = DependencyInjector.config
100
- if wiki && wiki.is_a?(Wiki) && wiki_loaded?(wiki.wiki)
101
- @wiki = wiki
102
- else
103
- #must be hardcoded, cause no options are loaded
104
- raise RuntimeError, "No wiki loaded"
105
- end
106
- if config && config.is_a?(Hash)
107
- @options = config
108
- options.each{|k,v| @options[k] = v}
109
- else
110
- raise RuntimeError, "No configuration provided"
111
- end
112
- if !Validations.is_boolean?(@persisted)
113
- @persisted = false
114
- end
115
- if !@error
116
- @error = nil
117
- end
118
- attributes.each do |name, value|
119
- send("#{name}=", value)
120
- end
121
-
122
- end
123
-
124
- # Public: Checks if the given Instance is an Instance of the Gollum Wiki
125
- #
126
- # wiki - An instance of a class
127
- #
128
- # Examples
129
- # wiki_loaded?(Gollum::Wiki)
130
- # # => true
131
- #
132
- # Returns if the given instance is an instance of Gollum::Wiki
133
- def wiki_loaded?(wiki)
134
- wiki.is_a?(Gollum::Wiki)
135
- end
136
-
137
- # Public: Gets the @error message
138
- #
139
- # Examples:
140
- # puts get_error_message
141
- # # => 'An Error Occured'
142
- #
143
- # Returns an Error message
144
- def get_error_message
145
- @error
146
- end
147
-
148
- # Public: saves this instance
149
- def save
150
- if valid?
151
- begin
152
- commit = wiki.wiki.write_page(@name, @format, @content, @commit)
153
- rescue Gollum::DuplicatePageError => e
154
- @error = e
155
- return false
156
- end
157
- end
158
- if commit and commit.is_a? String
159
- return commit
160
- else
161
- return false
162
- end
163
- end
164
-
165
- # Public: rewrite for save() method with raising exceptions as well
166
- def save!
167
- saves = save
168
- if @error
169
- raise RuntimeError, @error
170
- else
171
- return saves
172
- end
173
-
174
- end
175
-
176
- # Public: Updates an existing page
177
- # usage:
178
- #
179
- #
180
- # wiki = GollumRails::Wiki.new(PATH)
181
- #
182
- # page = GollumRails::Page.new
183
- # cnt = page.find(PAGENAME)
184
- #
185
- # commit = {
186
- # :message => "production test update",
187
- # :name => 'Florian Kasper',
188
- # :email => 'nirnanaaa@khnetworks.com'
189
- # }
190
- # update = page.update("content", commit)
191
-
192
- def update(content, commit, name=nil, format=nil)
193
- if !name.nil?
194
- @name = name
195
- end
196
- if !format.nil?
197
- @format = format
198
- end
199
- if commit.nil? || content.nil?
200
- @error = @options.messages.commit_not_empty_and_content_not_empty
201
- return false
202
- end
203
- commit = @wiki.wiki.update_page(@page, @name, @format, content, commit)
204
- if commit.is_a?(String)
205
- @persisted = true
206
- return commit
207
- else
208
- @persisted = false
209
- return nil
210
- end
211
- end
212
-
213
- #Public: alias for update with exceptions
214
- def update!(content, commit, name=nil, format=nil)
215
- updates = update(content, commit, name=nil, format=nil)
216
- if @error
217
- raise RuntimeError, @error
218
- else
219
- return updates
220
- end
221
- end
222
-
223
- #Public: Deletes page fetched by find()
224
- def delete(commit)
225
- if commit.nil?
226
- @error = @options.messages.commit_must_be_given
227
- return false
228
- end
229
- return @wiki.wiki.delete_page(@page, commit)
230
- end
231
-
232
- #Public: alias for delete with exceptions
233
- def delete!(commit)
234
- deletes = delete(commit)
235
- if @error
236
- raise RuntimeError, @error
237
- else
238
- return deletes
239
- end
240
- end
241
-
242
- #Public: For outputting all pages
243
- def all
244
-
245
- end
246
-
247
- # Public: Renders a preview (usable e.g. with ajax)
248
- #
249
- #
250
- # Returns rendered HTML
251
- def preview(name = nil, content = nil, format = :markdown)
252
- if !name or name == nil
253
- name = @name
254
- end
255
- if !content or content == nil
256
- content = @content
257
- end
258
- return @wiki.wiki.preview_page(name, content, format).formatted_data
259
- end
260
-
261
- # Public: Validates class variables
262
- #
263
- #
264
- # Returns either true or false
265
- def valid?
266
- if !@name || @name.nil?
267
- @error = @options.messages.name_not_set_or_nil
268
- return false
269
- end
270
- if !@commit || !@commit.is_a?(Hash)
271
- @error = @options.messages.commit_must_be_given
272
- return false
273
- end
274
- if !@format
275
- @error = @options.messages.format_not_set
276
- return false
277
- end
278
-
279
- return true
280
- end
281
-
282
- # Public: Gets an instance of Gollum::Page
283
- #
284
- # name - Search string
285
- #
286
- # Returns nil if no result was found or no name was given
287
- attr_reader :page
288
-
289
- # Public: Finds a page based on given search string
290
- #
291
- # Be careful: At the moment you must initialize the class by .new
292
- #
293
- # Examples
294
- # page = Page.new attributes
295
- # page.find('static')
296
- #
297
- # Returns either nil or an instance of Gollum::Page
298
- def find(name = nil)
299
- if !name.nil?
300
- page = @wiki.wiki.page(name)
301
- if page.nil?
302
- @error = @options.messages.no_page_found
303
- return nil
304
- end
305
-
306
- #need a better solution. Thats fu***** bull*****
307
- @page = page
308
- @name = page.name
309
- @format = page.format
310
-
311
- return page
312
- else
313
- @error = @options.messages.name_not_set_or_nil
314
- return nil
315
- end
316
- end
317
-
318
- # Public: Checks if the object is already persisted
319
- #
320
- # Examples
321
- # page = Page.new
322
- # page.persisted?
323
- # # => false
324
- # page.save
325
- # page.persisted?
326
- # # => false
327
- #
328
- # @commit = {
329
- # :message => "test creation of page",
330
- # :name => 'Florian Kasper',
331
- # :email => 'nirnanaaa@khnetworks.com'
332
- # }
333
- # attributes = {
334
- # name: 'TestPage',
335
- # content: 'content',
336
- # format: :markdown,
337
- # commit: @commit
338
- # }
339
- # page.save
340
- # page.persisted?
341
- # # => true
342
- #
343
- # Returns a Boolean(false|true)
344
- def persisted?
345
- @persisted
346
- end
347
-
348
- # Public: Magic method ( static )
349
- #
350
- # name - The functions name
351
- # args - Pointer of arguments
352
- #
353
- # Static into non static converter
354
- def self.method_missing(name, *args)
355
- klass = self.new
356
- return klass.find(args) if name.to_s == 'find'
357
- end
358
-
359
- ################################################
360
- ######### P A G E # L O A D E D ################
361
- ################################################
362
-
363
- # Public: A simple wrapper for Gollum::Page.raw_data
364
- #
365
- # Page needs to be loaded!
366
- #
367
- # Returns raw data
368
- def raw_data
369
- if @page
370
- @page.raw_data
371
- else
372
- @error = @options.messages.no_page_fetched
373
- return false
374
- end
375
- end
376
-
377
- # Public: A simple wrapper for Gollum::Page.formatted_data
378
- #
379
- # Page needs to be loaded!
380
- #
381
- # Returns formatted html
382
- def formatted_data
383
- if @page
384
- @page.formatted_data
385
- else
386
- @error = @options.messages.no_page_fetched
387
- return false
388
- end
389
- end
390
-
391
- # Public: Active Record like
392
- #
393
- #
394
- #
395
- # see Active Model documentation
396
- def versions
397
- if @page && @page.is_a?(Gollum::Page) #&& (persisted? || found?)
398
- return GollumRails::Versions.new(@page)
399
- else
400
- @error = @options.messages.no_page_fetched
401
- return false
402
- end
403
- end
404
-
405
- end
406
- end
1
+ module GollumRails
2
+
3
+ # Main class, used to interact with rails.
4
+ #
5
+ # Methods, which are available:
6
+ # * find
7
+ # * update_attributes
8
+ # * find_by_*
9
+ # * create
10
+ # * new
11
+ # * save
12
+ # * delete
13
+ # * find_or_initialize_by_naname
14
+ #
15
+ class Page < Adapters::ActiveModel::Callback
16
+ include ::ActiveModel::Conversion
17
+ extend ::ActiveModel::Naming
18
+
19
+
20
+ # static
21
+ class << self
22
+ # Gets / Sets the gollum page
23
+ #
24
+ attr_accessor :gollum_page
25
+
26
+ # Sets the validator
27
+ attr_writer :validator
28
+
29
+ end
30
+ # Initializes a new Page
31
+ #
32
+ # attrs - Hash of attributes
33
+ #
34
+ # commit must be given to perform any page action!
35
+ def initialize(attrs = {})
36
+ attrs.each{|k,v| self.instance_variable_set("@#{k}", v)}
37
+ attrs.each{|k,v| self.class.validator.instance_variable_set("@#{k}", v)}
38
+ end
39
+
40
+ #########
41
+ # Setters
42
+ #########
43
+
44
+ # Sets the wiki instance
45
+ attr_writer :wiki
46
+
47
+ # Sets the pages name
48
+ attr_writer :name
49
+
50
+ # Sets the contents content
51
+ attr_writer :content
52
+
53
+ # Sets the commit Hash
54
+ attr_writer :commit
55
+
56
+ # Sets the format
57
+ attr_writer :format
58
+
59
+ # Sets the page
60
+ attr_writer :page
61
+
62
+ #########
63
+ # Getters
64
+ #########
65
+
66
+ # Gets the wiki instance
67
+ def wiki
68
+ @wiki || Adapters::Gollum::Connector.wiki_class.class_variable_get(:@@wiki)
69
+ end
70
+
71
+ # Gets the pages' name
72
+ def name
73
+ @name || @gollum_page.name
74
+ end
75
+
76
+ # Gets the raw content of the current page
77
+ def content
78
+ @content || @gollum_page.raw_content
79
+ end
80
+
81
+ # Need to implement the Committer connector (forgot it completely)
82
+ # Gets the commit Hash from current object
83
+ def commit
84
+ @commit
85
+ end
86
+
87
+ # Gets the pages format
88
+ def format
89
+ (@format || @gollum_page.format).to_sym
90
+ end
91
+
92
+ # Gets the validator
93
+ def self.validator
94
+ @@validator ||= Adapters::ActiveModel::Validation.new
95
+ end
96
+
97
+ # Gets the page class
98
+ def page
99
+ @page ||= Adapters::Gollum::Connector.page_class.new
100
+ end
101
+
102
+ # Statically page getter
103
+ #
104
+ # DEPRECATED! Do not use
105
+ def self.page
106
+ Adapters::Gollum::Connector.page_class
107
+ end
108
+
109
+ #############
110
+ # activemodel
111
+ #############
112
+
113
+ # Handles the connection betweet plain activemodel and Gollum
114
+ # Saves current page in GIT wiki
115
+ # If another page with the same name is existing, gollum_rails
116
+ # will detect it and returns that page instead.
117
+ #
118
+ # Examples:
119
+ #
120
+ # obj = GollumRails::Page.new <params>
121
+ # @article = obj.save
122
+ # # => Gollum::Page
123
+ #
124
+ # @article.name
125
+ # whatever name you have entered OR the name of the previous
126
+ # created page
127
+ #
128
+ #
129
+ # TODO:
130
+ # * overriding for creation(duplicates)
131
+ # * do not alias save! on save
132
+ #
133
+ # Returns an instance of Gollum::Page or false
134
+ def save
135
+ begin
136
+ page.instance_variable_set("@page", page.new_page(name, content, format, commit)) if valid?
137
+ return page.page||false
138
+ rescue ::Gollum::DuplicatePageError => e
139
+ page.instance_variable_set "@page",page.find_page(name)
140
+ return page.page
141
+ end
142
+ end
143
+
144
+ # aliasing save
145
+ #
146
+ # TODO:
147
+ # * implement full method!
148
+ alias_method :save!, :save
149
+
150
+ # first creates an instance of itself and executes the save function.
151
+ #
152
+ # hash - Hash containing the page data
153
+ #
154
+ #
155
+ # Returns an instance of Gollum::Page or false
156
+ def self.create(hash)
157
+ page = Page.new hash
158
+ page.save
159
+ end
160
+
161
+ # calls `create` on current class. If returned value is nil an exception will be thrown
162
+ #
163
+ # hash - Hash containing the page data
164
+ #
165
+ # TODO:
166
+ # * much testing
167
+ #
168
+ # Returns an instance of Gollum::Page
169
+ def self.create!(hash)
170
+ action = self.create(hash)
171
+ if action.nil? or action.is_a? Adapters::ActiveModel::Boolean
172
+ raise GollumInternalError, "Page is nil"
173
+ end
174
+ action
175
+ end
176
+
177
+ # Updates an existing page (or created)
178
+ #
179
+ # hash - Hash containing the attributes, you want to update
180
+ # commit - optional. If given this commit will be used instead of that one, used
181
+ # to initialize the instance
182
+ #
183
+ #
184
+ # Returns an instance of Gollum::Page
185
+ def update_attributes(hash, commit=nil)
186
+ page.update_page hash, get_right_commit(commit)
187
+ end
188
+
189
+ # Deletes current page (also available static. See below)
190
+ #
191
+ # commit - optional. If given this commit will be used instead of that one, used
192
+ # to initialize the instance
193
+ #
194
+ # Returns the commit id of the current action as String
195
+ def delete(commit=nil)
196
+ page.delete_page get_right_commit(commit)
197
+ end
198
+
199
+ # Previews the page - Mostly used if you want to see what you do before saving
200
+ #
201
+ # This is an extremely performant method!
202
+ # 1 rendering attempt take depending on the content about 0.001 (simple markdown)
203
+ # upto 0.004 (1000 chars markdown) seconds, which is quite good
204
+ #
205
+ #
206
+ # format - Specify the format you want to render with see {self.format_supported?}
207
+ # for formats
208
+ #
209
+ # Returns a String
210
+ def preview(format=:markdown)
211
+ preview = wiki.preview_page @name, @content, format
212
+ preview.formatted_data
213
+ end
214
+
215
+ # Finds a page based on the name and specified version
216
+ #
217
+ # name - the name of the page
218
+ #
219
+ # Return an instance of Gollum::Page
220
+ def self.find(name)
221
+ page = GollumRails::Adapters::Gollum::Page.new
222
+ page.find_page name
223
+ end
224
+
225
+ # todo
226
+ def valid?
227
+ self.class.validate self,true
228
+ end
229
+
230
+ # todo
231
+ def self.validate(context=nil,check=false,&block)
232
+ if block
233
+ @@gollum_page = block
234
+ end
235
+ if check
236
+ @@gollum_page.call context.class.validator
237
+ end
238
+ end
239
+
240
+ # todo
241
+ def self.register_validations_for(*args)
242
+ end
243
+
244
+
245
+ # module templates following:
246
+
247
+ # empty
248
+ #def self.method_missing(name, *args)
249
+ #
250
+ #end
251
+
252
+ # empty
253
+ #def method_missing(name, *args)
254
+ #end
255
+
256
+ # Finds an existing page or creates it
257
+ #
258
+ # Returns self
259
+ def self.find_or_initialize_by_name(name, commit)
260
+ result_for_find = self.find(name)
261
+ self.create({:format => :markdown, :name => name, :content => ".", :commit => commit })
262
+ end
263
+
264
+ def self.format_supported?(format)
265
+ supported = ['ascii', 'github-markdown','markdown', 'creole', 'org', 'pod', 'rdoc']
266
+ format.in?(supported)
267
+ end
268
+
269
+ private
270
+
271
+ # Get the right commit out of 2 commits
272
+ #
273
+ # commit_local - local commit Hash
274
+ #
275
+ # Returns local_commit > class_commit
276
+ def get_right_commit(commit_local)
277
+ com = commit if commit_local.nil?
278
+ com = commit_local if !commit_local.nil?
279
+ return com
280
+ end
281
+
282
+ end
283
+ end