Class: Canals::Repository

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/canals/repository.rb

Constant Summary collapse

ENVIRONMENTS =
:environments
TUNNELS =
:tunnels

Instance Method Summary collapse

Constructor Details

#initialize(root = nil) ⇒ Repository

Returns a new instance of Repository.



15
16
17
18
# File 'lib/canals/repository.rb', line 15

def initialize(root = nil)
  @root = root
  @repo = load_repository(repo_file)
end

Instance Method Details

#add(options, save = true) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/canals/repository.rb', line 30

def add(options, save=true)
  @repo[TUNNELS][options.name.to_sym] = options.to_hash
  if options.env_name.nil? && !options.env.nil? && options.env.is_default?
    @repo[TUNNELS][options.name.to_sym][:env] = options.env.name
  end
  save! if save
end

#add_environment(environment, save = true) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/canals/repository.rb', line 53

def add_environment(environment, save=true)
  if environment.is_default?
    @repo[ENVIRONMENTS].each { |name, env| env.delete(:default) }
  end
  if @repo[ENVIRONMENTS].empty?
    environment.default = true
  end
  @repo[ENVIRONMENTS][environment.name.to_sym] = environment.to_hash
  save! if save
end

#delete(name, save = true) ⇒ Object



38
39
40
41
# File 'lib/canals/repository.rb', line 38

def delete(name, save=true)
  @repo[TUNNELS].delete(name)
  save! if save
end

#each(&block) ⇒ Object



22
23
24
# File 'lib/canals/repository.rb', line 22

def each(&block)
  @repo[TUNNELS].map{ |n, r| Canals::CanalOptions.new(r) }.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/canals/repository.rb', line 26

def empty?
  @repo[TUNNELS].empty?
end

#environment(name = nil) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/canals/repository.rb', line 69

def environment(name=nil)
  if name.nil?
    args = @repo[ENVIRONMENTS].select{ |n,e| e[:default] }.values[0]
  else
    args = @repo[ENVIRONMENTS][name.to_sym]
  end
  Canals::Environment.new(args) if !args.nil?
end

#environmentsObject



78
79
80
# File 'lib/canals/repository.rb', line 78

def environments
  @repo[ENVIRONMENTS].map { |n, e| Canals::Environment.new(e) }
end

#get(name) ⇒ Object



43
44
45
46
47
# File 'lib/canals/repository.rb', line 43

def get(name)
  name = name.to_sym
  return nil if !@repo[:tunnels].has_key? name
  CanalOptions.new(@repo[:tunnels][name])
end

#has?(name) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/canals/repository.rb', line 49

def has?(name)
  return @repo[:tunnels].has_key? name.to_sym
end

#save!Object



64
65
66
67
# File 'lib/canals/repository.rb', line 64

def save!
  FileUtils.mkdir_p(repo_file.dirname)
  Canals::Tools::YAML.dump_file(repo_file, @repo)
end