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.



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

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

Instance Method Details

#add(options, save = true) ⇒ Object



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

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

#add_environment(environment, save = true) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/canals/repository.rb', line 42

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] = environment.to_hash
  save! if save
end

#each(&block) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

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

#environment(name = nil) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/canals/repository.rb', line 60

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

#environmentsObject



69
70
71
# File 'lib/canals/repository.rb', line 69

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

#get(name) ⇒ Object



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

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

#save!Object



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

def save!
  FileUtils.mkdir_p(repo_file.dirname)
  File.open(repo_file, 'w') do |file|
    file.write(Psych.dump(@repo))
  end
end