Class: Bard::Config
- Inherits:
-
Object
- Object
- Bard::Config
- Defined in:
- lib/bard/config.rb
Class Attribute Summary collapse
-
.default_targets ⇒ Object
Returns the value of attribute default_targets.
-
.strict ⇒ Object
Returns the value of attribute strict.
Instance Attribute Summary collapse
-
#project_name ⇒ Object
readonly
Returns the value of attribute project_name.
-
#targets ⇒ Object
readonly
Returns the value of attribute targets.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#initialize(project_name = nil, path: nil, source: nil) ⇒ Config
constructor
A new instance of Config.
-
#method_missing(name, *args, &block) ⇒ Object
A bard.rb may use DSL contributed by plugins.
- #remove_target(key) ⇒ Object
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
- #target(key, &block) ⇒ Object
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_targets ⇒ Object
Returns the value of attribute default_targets.
8 9 10 |
# File 'lib/bard/config.rb', line 8 def default_targets @default_targets end |
.strict ⇒ Object
Returns the value of attribute strict.
9 10 11 |
# File 'lib/bard/config.rb', line 9 def strict @strict end |
Instance Attribute Details
#project_name ⇒ Object (readonly)
Returns the value of attribute project_name.
26 27 28 |
# File 'lib/bard/config.rb', line 26 def project_name @project_name end |
#targets ⇒ Object (readonly)
Returns the value of attribute targets.
26 27 28 |
# File 'lib/bard/config.rb', line 26 def targets @targets end |
Class Method Details
.current ⇒ Object
12 13 14 |
# File 'lib/bard/config.rb', line 12 def self.current new(detect_project_name, path: "bard.rb") end |
.detect_project_name ⇒ Object
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.(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
70 71 72 |
# File 'lib/bard/config.rb', line 70 def respond_to_missing?(name, include_private = false) !self.class.strict || super end |