Class: Gitdocs::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/gitdocs/configuration.rb

Defined Under Namespace

Classes: Config, Share

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_root = nil) ⇒ Configuration

Returns a new instance of Configuration.



9
10
11
12
13
14
15
16
17
18
# File 'lib/gitdocs/configuration.rb', line 9

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

Instance Attribute Details

#config_rootObject (readonly)

Returns the value of attribute config_root.



7
8
9
# File 'lib/gitdocs/configuration.rb', line 7

def config_root
  @config_root
end

Instance Method Details

#add_path(path, opts = nil) ⇒ Object

Parameters:

  • path (String)
  • opts (Hash) (defaults to: nil)


40
41
42
43
44
45
# File 'lib/gitdocs/configuration.rb', line 40

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!
end

#clearObject



82
83
84
# File 'lib/gitdocs/configuration.rb', line 82

def clear
  Share.destroy_all
end

#remove_by_id(id) ⇒ true, false

Parameters:

  • id (Integer)

    of the share to remove

Returns:

  • (true)

    share was deleted

  • (false)

    share does not exist



75
76
77
78
79
80
# File 'lib/gitdocs/configuration.rb', line 75

def remove_by_id(id)
  Share.find(id).destroy
  true
rescue ActiveRecord::RecordNotFound
  false
end

#remove_path(path) ⇒ Object

Parameters:

  • path (String)

    of the share to remove



66
67
68
69
# File 'lib/gitdocs/configuration.rb', line 66

def remove_path(path)
  path = normalize_path(path)
  Share.where(path: path).destroy_all
end

#sharesObject



86
87
88
# File 'lib/gitdocs/configuration.rb', line 86

def shares
  Share.all
end

#start_web_frontendObject

return [Boolean]



29
30
31
# File 'lib/gitdocs/configuration.rb', line 29

def start_web_frontend
  global.start_web_frontend
end

#update_all(new_config) ⇒ Object

Parameters:

  • new_config (Hash)

Options Hash (new_config):

  • 'config' (Hash)
  • 'share' (Array<Hash>)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gitdocs/configuration.rb', line 50

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
end

#web_frontend_portInteger

Returns:

  • (Integer)


34
35
36
# File 'lib/gitdocs/configuration.rb', line 34

def web_frontend_port
  global.web_frontend_port
end