Skip to content
This repository has been archived by the owner on May 9, 2019. It is now read-only.

Commit

Permalink
added new setup features
Browse files Browse the repository at this point in the history
  • Loading branch information
nirnanaaa committed Feb 28, 2014
1 parent 4cac1f7 commit 2b6ab96
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 44 deletions.
70 changes: 28 additions & 42 deletions lib/gollum_rails/setup.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'gollum_rails/setup/error'
module GollumRails

# Setup functionality for Rails initializer
Expand All @@ -13,76 +14,61 @@ module GollumRails
# end
#
#
class Setup
module Setup
autoload :Options, 'gollum_rails/setup/options'
include Options

class << self

attr_accessor :wiki_path

attr_writer :wiki_options

attr_accessor :repository

attr_accessor :startup

# Gets / Sets the init options
attr_accessor :options


def wiki_options
return {} unless @wiki_options.kind_of? Hash
@wiki_options ||= {}
end
# Wiki startup options
def options=(options)
@options = options
end

# defines block builder for Rails initializer.
# executes public methods inside own class
#
def build(&block)
block.call self
if @repository == :application
def build(new_attributes = nil, &block)
if block_given?
yield self
else
if !new_attributes.respond_to?(:stringify_keys)
raise ArgumentError, "When assigning attributes, you must pass a hash as an argument."
end
attributes = new_attributes.stringify_keys
attributes.each do |k, v|
begin
public_send("#{k}=", v)
rescue NoMethodError
end
end

end
if self.repository == :application
raise GollumRailsSetupError, "Rails configuration is not defined.
Are you in a Rails app?" if Rails.application.nil?

initialize_wiki Rails.application.config.wiki_repository
else
raise GollumRailsSetupError, "Git repository does not exist.
Was the specified pathname correct?" unless Pathname.new(@repository).exist?
initialize_wiki @repository
Was the specified pathname correct?" unless Pathname.new(self.repository).exist?
initialize_wiki self.repository
end
end

#######
private
#######

# Checks if provided path is present and valid
#
# Example
# path_valid? '/'
# # =>true
#
# path_valid? nil
# # =>false

def path_valid?(path)
return path.exist? if path.is_a?(Pathname)
return !(path.nil? || path.empty? || ! path.is_a?(String))
end

def initialize_wiki(path)
if path_valid? path
@wiki_path = path.to_s
@wiki_options = options
self.wiki_path = path.to_s
self.wiki_options = options
true
else
raise GollumRailsSetupError, 'Repistory path is empty or invalid!'
raise GollumRailsSetupError, 'Path to repository is empty or invalid!'
end

end

end
end
class GollumRailsSetupError < ArgumentError; end
end
5 changes: 5 additions & 0 deletions lib/gollum_rails/setup/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module GollumRails
module Setup
class GollumRailsSetupError < ArgumentError; end
end
end
16 changes: 16 additions & 0 deletions lib/gollum_rails/setup/options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module GollumRails
module Setup
module Options
extend ActiveSupport::Concern

module ClassMethods
attr_accessor :wiki_options
attr_accessor :repository
attr_accessor :startup
attr_accessor :options
attr_accessor :wiki_path
end

end
end
end
9 changes: 7 additions & 2 deletions spec/gollum_rails/setup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,20 @@ class Bla < GollumRails::Page
config.repository = :application

end
}.to raise_error GollumRails::GollumRailsSetupError
}.to raise_error GollumRails::Setup::GollumRailsSetupError

end
it "should throw an error if a pathname was supplied that does not exist" do
expect{
GollumRails::Setup.build do |setup|
setup.repository = Pathname.new('/nonexistingdirectoryshouldbenonexisting')
end
}.to raise_error GollumRails::GollumRailsSetupError
}.to raise_error GollumRails::Setup::GollumRailsSetupError
end
it "should also initialize without a block given" do
expect{
GollumRails::Setup.build(repository: '.')
}.not_to raise_error
end


Expand Down

0 comments on commit 2b6ab96

Please sign in to comment.