Class: Luban::Deployment::Package::DependencySet

Inherits:
Object
  • Object
show all
Defined in:
lib/luban/deployment/cli/package/dependency_set.rb

Instance Method Summary collapse

Constructor Details

#initializeDependencySet

Returns a new instance of DependencySet.



5
6
7
8
# File 'lib/luban/deployment/cli/package/dependency_set.rb', line 5

def initialize
  @dependencies = {} 
  @ctx = {}
end

Instance Method Details

#after_install(&blk) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/luban/deployment/cli/package/dependency_set.rb', line 25

def after_install(&blk)
  if @ctx[:version_requirement].nil?
    raise RuntimeError, 'Please call #apply_to prior to #after_install.'
  end
  @ctx[:type] = :after_install
  instance_eval(&blk) if block_given?
end

#after_install_dependencies_for(version) ⇒ Object



65
66
67
# File 'lib/luban/deployment/cli/package/dependency_set.rb', line 65

def after_install_dependencies_for(version)
  dependencies_for(version, type: :after_install)
end

#apply_to(version_requirement, &blk) ⇒ Object



10
11
12
13
14
15
# File 'lib/luban/deployment/cli/package/dependency_set.rb', line 10

def apply_to(version_requirement, &blk)
  @ctx = { version_requirement: version_requirement }
  instance_eval(&blk) if block_given?
ensure
  @ctx = {}
end

#before_install(&blk) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/luban/deployment/cli/package/dependency_set.rb', line 17

def before_install(&blk)
  if @ctx[:version_requirement].nil?
    raise RuntimeError, 'Please call #apply_to prior to #before_install.'
  end
  @ctx[:type] = :before_install
  instance_eval(&blk) if block_given?
end

#before_install_dependencies_for(version) ⇒ Object



61
62
63
# File 'lib/luban/deployment/cli/package/dependency_set.rb', line 61

def before_install_dependencies_for(version)
  dependencies_for(version, type: :before_install)
end

#depend_on(name, version:, **opts) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/luban/deployment/cli/package/dependency_set.rb', line 33

def depend_on(name, version:, **opts)
  if @ctx[:type].nil?
    raise RuntimeError, 'Please call #before_install or #after_install prior to #depend_on.'
  end
  requirement = @ctx[:version_requirement]
  type = @ctx[:type]
  dependency = Dependency.new(requirement, type, name, version, **opts)
  unless @dependencies.has_key?(requirement)
    @dependencies[requirement] = { before_install: [],
                                   after_install: [] }
  end
  @dependencies[requirement][type] << dependency
end

#dependencies_for(version, type: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/luban/deployment/cli/package/dependency_set.rb', line 47

def dependencies_for(version, type: nil)
  types = *type
  types = DependencyTypes if types.empty?
  deps = { before_install: [],
           after_install: [] }
  @dependencies.each_pair do |r, d|
    types.each do |t|
      next if d[t].empty?
      deps[t] |= d[t] if d[t].first.applicable_to?(version)
    end
  end
  deps
end