Class: Build::Environment::Constructor
- Inherits:
-
Object
- Object
- Build::Environment::Constructor
- Defined in:
- lib/build/environment/constructor.rb
Overview
Represents a DSL proxy used to populate an environment using a block-based interface.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Retrieve the value of a key from the underlying environment.
-
#append(name) ⇒ Object
Convert the current value of a key to an array to allow appending.
-
#default(name) ⇒ Object
Mark the current value of a key as the default, wrapping it in a
Defaultstruct. -
#define(klass, name, &block) ⇒ Object
Associate a class and configuration block with a key as a
Definestruct. -
#hash(**options) ⇒ Object
Create an
OpenStructfrom the given keyword options. -
#initialize(environment, proxy = nil) ⇒ Constructor
constructor
Initialize the constructor with an environment and an optional proxy object.
-
#method_missing(name, *args, **options, &block) ⇒ Object
Dynamically set environment keys or delegate to the proxy object.
-
#parent ⇒ Object
Return the parent of the underlying environment.
-
#replace(name) ⇒ Object
Mark the current value of a key for replacement, wrapping it in a
Replacestruct. -
#respond_to(*args) ⇒ Object
Delegate
respond_toto the proxy if available. -
#respond_to?(name, include_private = false) ⇒ Boolean
Check whether the constructor responds to the given method name.
Constructor Details
#initialize(environment, proxy = nil) ⇒ Constructor
Initialize the constructor with an environment and an optional proxy object.
46 47 48 49 |
# File 'lib/build/environment/constructor.rb', line 46 def initialize(environment, proxy = nil) @environment = environment @proxy = proxy end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, **options, &block) ⇒ Object
Dynamically set environment keys or delegate to the proxy object.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/build/environment/constructor.rb', line 64 def method_missing(name, *args, **, &block) if .empty? if args.empty? and block_given? @environment[name] = block return name elsif !args.empty? if args.count == 1 @environment[name] = args.first else @environment[name] = args end return name end end if @proxy # This is a bit of a hack, but I'm not sure if there is a better way. if .empty? @proxy.send(name, *args, &block) else @proxy.send(name, *args, **, &block) end else super end end |
Instance Method Details
#[](key) ⇒ Object
Retrieve the value of a key from the underlying environment.
102 103 104 |
# File 'lib/build/environment/constructor.rb', line 102 def [] key @environment[key] end |
#append(name) ⇒ Object
Convert the current value of a key to an array to allow appending.
140 141 142 143 144 |
# File 'lib/build/environment/constructor.rb', line 140 def append(name) @environment[name] = Array(@environment[name]) return name end |
#default(name) ⇒ Object
Mark the current value of a key as the default, wrapping it in a Default struct.
122 123 124 125 126 |
# File 'lib/build/environment/constructor.rb', line 122 def default(name) @environment[name] = Default.new(@environment[name]) return name end |
#define(klass, name, &block) ⇒ Object
Associate a class and configuration block with a key as a Define struct.
151 152 153 154 155 |
# File 'lib/build/environment/constructor.rb', line 151 def define(klass, name, &block) @environment[name] = Define.new(klass, &block) return name end |
#hash(**options) ⇒ Object
Create an OpenStruct from the given keyword options.
115 116 117 |
# File 'lib/build/environment/constructor.rb', line 115 def hash(**) OpenStruct.new() end |
#parent ⇒ Object
Return the parent of the underlying environment.
108 109 110 |
# File 'lib/build/environment/constructor.rb', line 108 def parent @environment.parent end |
#replace(name) ⇒ Object
Mark the current value of a key for replacement, wrapping it in a Replace struct.
131 132 133 134 135 |
# File 'lib/build/environment/constructor.rb', line 131 def replace(name) @environment[name] = Replace.new(@environment[name]) return name end |
#respond_to(*args) ⇒ Object
Delegate respond_to to the proxy if available.
95 96 97 |
# File 'lib/build/environment/constructor.rb', line 95 def respond_to(*args) super or @proxy&.respond_to(*args) end |
#respond_to?(name, include_private = false) ⇒ Boolean
Check whether the constructor responds to the given method name.
55 56 57 |
# File 'lib/build/environment/constructor.rb', line 55 def respond_to?(name, include_private = false) @environment.include?(name) || @proxy&.respond_to?(name, include_private) || super end |