Class: Bard::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/bard/config.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_name = nil, path: nil, source: nil) ⇒ Config

Returns a new instance of Config.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bard/config.rb', line 28

def initialize(project_name = nil, path: nil, source: nil)
  @project_name = project_name
  @targets = {}
  load_defaults if project_name

  if path && File.exist?(path)
    source = File.read(path)
  end
  if source
    instance_eval(source, path)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

A bard.rb may use DSL contributed by plugins. When the owning plugin is loaded it defines a real method that wins over this; otherwise we tolerate the declaration as a plain attribute so the file still parses — notably server-side, where bard-cli is absent.



61
62
63
64
65
66
67
68
# File 'lib/bard/config.rb', line 61

def method_missing(name, *args, &block)
  return super if self.class.strict
  if args.empty? && block.nil?
    (@attributes ||= {})[name]
  else
    (@attributes ||= {})[name] = args.length == 1 ? args.first : args
  end
end

Class Attribute Details

.default_targetsObject

Returns the value of attribute default_targets.



8
9
10
# File 'lib/bard/config.rb', line 8

def default_targets
  @default_targets
end

.strictObject

Returns the value of attribute strict.



9
10
11
# File 'lib/bard/config.rb', line 9

def strict
  @strict
end

Instance Attribute Details

#project_nameObject (readonly)

Returns the value of attribute project_name.



26
27
28
# File 'lib/bard/config.rb', line 26

def project_name
  @project_name
end

#targetsObject (readonly)

Returns the value of attribute targets.



26
27
28
# File 'lib/bard/config.rb', line 26

def targets
  @targets
end

Class Method Details

.currentObject



12
13
14
# File 'lib/bard/config.rb', line 12

def self.current
  new(detect_project_name, path: "bard.rb")
end

.detect_project_nameObject



16
17
18
19
20
21
22
23
24
# File 'lib/bard/config.rb', line 16

def self.detect_project_name
  git_common_dir = `git rev-parse --git-common-dir 2>/dev/null`.chomp
  dirname = if $?.success? && !git_common_dir.empty?
    File.dirname(File.expand_path(git_common_dir))
  else
    Dir.getwd
  end
  File.basename(dirname)
end

Instance Method Details

#[](key) ⇒ Object



54
55
56
# File 'lib/bard/config.rb', line 54

def [](key)
  @targets[key.to_sym]
end

#remove_target(key) ⇒ Object



41
42
43
# File 'lib/bard/config.rb', line 41

def remove_target(key)
  @targets.delete(key.to_sym)
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/bard/config.rb', line 70

def respond_to_missing?(name, include_private = false)
  !self.class.strict || super
end

#target(key, &block) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/bard/config.rb', line 45

def target(key, &block)
  key = key.to_sym
  unless @targets[key].is_a?(Target)
    @targets[key] = Target.new(key, self)
  end
  @targets[key].instance_eval(&block) if block
  @targets[key]
end