forked from nesquena/gitdocs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Share and Initializer from Configuration
The original motivation for this refactoring is to reduce the number of objects which are getting passed around making the Celluloid conversion easier. ActiveRecord objects (e.g., Share and Config) will be used from their class methods, more like Rails does. This will eliminate some object passing as well as deprecating some delegate code in the Configuration and Manager classes. This results in 3 classes instead of 1 and I hope will make each one easier to understand, and test. I have kept Configuration::Config to avoid changing the table names. This could be simplified in the future.
- Loading branch information
Showing
17 changed files
with
248 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
|
||
require 'gitdocs' | ||
|
||
Gitdocs::Initializer.initialize_all | ||
Gitdocs::Cli.start(ARGV) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,36 @@ | ||
# -*- encoding : utf-8 -*- | ||
|
||
require 'active_record' | ||
require 'grit' | ||
|
||
class Gitdocs::Configuration | ||
attr_reader :config_root | ||
|
||
def initialize(config_root = nil) | ||
@config_root = config_root || File.expand_path('.gitdocs', ENV['HOME']) | ||
FileUtils.mkdir_p(@config_root) | ||
ActiveRecord::Base.establish_connection( | ||
adapter: 'sqlite3', | ||
database: ENV['TEST'] ? ':memory:' : File.join(@config_root, 'config.db') | ||
) | ||
ActiveRecord::Migrator.migrate(File.expand_path('../migration', __FILE__)) | ||
import_old_shares unless ENV['TEST'] | ||
end | ||
|
||
class Share < ActiveRecord::Base | ||
#attr_accessible :polling_interval, :path, :notification, :branch_name, :remote_name, :sync_type | ||
end | ||
|
||
class Config < ActiveRecord::Base | ||
#attr_accessible :start_web_frontend, :web_frontend_port | ||
end | ||
|
||
# return [Boolean] | ||
def start_web_frontend | ||
global.start_web_frontend | ||
# @return [Boolean] | ||
def self.start_web_frontend | ||
Config.global.start_web_frontend | ||
end | ||
|
||
# @return [Integer] | ||
def web_frontend_port | ||
global.web_frontend_port | ||
end | ||
|
||
# @param [String] path | ||
# @param [Hash] opts | ||
def add_path(path, opts = nil) | ||
path = normalize_path(path) | ||
path_opts = { path: path } | ||
path_opts.merge!(opts) if opts | ||
Share.new(path_opts).save! | ||
def self.web_frontend_port | ||
Config.global.web_frontend_port | ||
end | ||
|
||
# @param [Hash] new_config | ||
# @option new_config [Hash] 'config' | ||
# @option new_config [Array<Hash>] 'share' | ||
def update_all(new_config) | ||
global.update_attributes(new_config['config']) | ||
new_config['share'].each do |index, share_config| | ||
# Skip the share update if there is no path specified. | ||
next unless share_config['path'] && !share_config['path'].empty? | ||
|
||
# Split the remote_branch into remote and branch | ||
remote_branch = share_config.delete('remote_branch') | ||
if remote_branch | ||
share_config['remote_name'], share_config['branch_name'] = remote_branch.split('/', 2) | ||
end | ||
shares[index.to_i].update_attributes(share_config) | ||
end | ||
def self.update(new_config) | ||
Config.global.update_attributes(new_config) | ||
end | ||
|
||
# @param [String] path of the share to remove | ||
def remove_path(path) | ||
path = normalize_path(path) | ||
Share.where(path: path).destroy_all | ||
end | ||
|
||
# @param [Integer] id of the share to remove | ||
# | ||
# @return [true] share was deleted | ||
# @return [false] share does not exist | ||
def remove_by_id(id) | ||
Share.find(id).destroy | ||
true | ||
rescue ActiveRecord::RecordNotFound | ||
false | ||
end | ||
|
||
def clear | ||
Share.destroy_all | ||
end | ||
|
||
def shares | ||
Share.all | ||
end | ||
|
||
############################################################################## | ||
|
||
private | ||
|
||
def global | ||
fail if Config.all.size > 1 | ||
Config.create! if Config.all.empty? | ||
Config.all.first | ||
end | ||
|
||
def normalize_path(path) | ||
File.expand_path(path, Dir.pwd) | ||
end | ||
|
||
def import_old_shares | ||
full_path = File.expand_path('paths', config_root) | ||
return unless File.exist?(full_path) | ||
# NOTE: This record has been kept as a subclass to avoid changing the | ||
# database table. There are other ways to achieve this, but this seemed most | ||
# clear for now. [2015-06-26 -- acant] | ||
class Config < ActiveRecord::Base | ||
# attr_accessible :start_web_frontend, :web_frontend_port | ||
|
||
File.read(full_path).split("\n").each do |path| | ||
Share.find_or_create_by_path(path) | ||
# @return [Gitdocs::Configuration::Config] | ||
def self.global | ||
fail if all.size > 1 | ||
create! if all.empty? | ||
all.first | ||
end | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# -*- encoding : utf-8 -*- | ||
|
||
require 'active_record' | ||
|
||
module Gitdocs | ||
class Initializer | ||
# @return [nil] | ||
def self.initialize_all | ||
initialize_database | ||
initialize_old_paths | ||
end | ||
|
||
# @return [nil] | ||
def self.initialize_database | ||
FileUtils.mkdir_p(root_dirname) | ||
ActiveRecord::Base.establish_connection( | ||
adapter: 'sqlite3', | ||
database: database | ||
) | ||
ActiveRecord::Migrator.migrate( | ||
File.expand_path('../migration', __FILE__) | ||
) | ||
end | ||
|
||
# @return [nil] | ||
def self.initialize_old_paths | ||
old_path_dirname = File.expand_path('paths', root_dirname) | ||
return unless File.exist?(old_path_dirname) | ||
|
||
File.read(old_path_dirname).split("\n").each do |path| | ||
begin | ||
Share.create_by_path!(path) | ||
rescue # rubocop:disable ExceptionHandling | ||
# Nothing to do, because we want the process to keep going. | ||
end | ||
end | ||
end | ||
|
||
# @return [String] | ||
def self.root_dirname | ||
@root_dirname ||= File.expand_path('.gitdocs', ENV['HOME']) | ||
end | ||
|
||
# @param [nil, String] value | ||
# @return [nil] | ||
def self.root_dirname=(value) | ||
return if value.nil? | ||
@root_dirname = value | ||
end | ||
|
||
# @return [String] | ||
def self.database | ||
@database ||= File.join(root_dirname, 'config.db') | ||
end | ||
|
||
# @param [nil, String] value | ||
# @return [nil] | ||
def self.database=(value) | ||
return if value.nil? | ||
@database = value | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.