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.



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

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.



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

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.



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

def default_targets
  @default_targets
end

.strictObject

Returns the value of attribute strict.



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

def strict
  @strict
end

Instance Attribute Details

#project_nameObject (readonly)

Returns the value of attribute project_name.



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

def project_name
  @project_name
end

#targetsObject (readonly)

Returns the value of attribute targets.



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

def targets
  @targets
end

Class Method Details

.currentObject



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

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

.detect_project_nameObject



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

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



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

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

#remove_target(key) ⇒ Object



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

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

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

Returns:

  • (Boolean)


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

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

#target(key, &block) ⇒ Object



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

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