Class: Nuklear::DSL
- Inherits:
-
Object
- Object
- Nuklear::DSL
- Defined in:
- lib/nuklear/dsl.rb
Class Method Summary collapse
-
.prepare! ⇒ Object
Scans all descendants of Nuklear::UI::Base and defines DSL method names based on each component name.
Instance Method Summary collapse
- #add_component(klass, *args, **kw, &block) ⇒ Object
-
#initialize(ctx, &block) ⇒ DSL
constructor
A new instance of DSL.
- #method_missing(name, *args, **kw, &block) ⇒ Object
- #respond_to?(name) ⇒ Boolean
Constructor Details
#initialize(ctx, &block) ⇒ DSL
Returns a new instance of DSL.
5 6 7 8 |
# File 'lib/nuklear/dsl.rb', line 5 def initialize(ctx, &block) @ctx = ctx instance_eval(&block) if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, **kw, &block) ⇒ Object
24 25 26 |
# File 'lib/nuklear/dsl.rb', line 24 def method_missing(name, *args, **kw, &block) @ctx.send(name, *args, **kw, &block) end |
Class Method Details
.prepare! ⇒ Object
Scans all descendants of Nuklear::UI::Base and defines DSL method names based on each component name. This happens once automatically at startup but if you are having trouble with a new custom UI component, then you can call this method to make sure it’s been defined.
36 37 38 39 40 41 42 |
# File 'lib/nuklear/dsl.rb', line 36 def self.prepare! Nuklear::UI::Base.descendants.each do |descendant| define_method descendant.dsl_method_name do |*args, **kw, &block| add_component(descendant, *args, **kw, &block) end end end |
Instance Method Details
#add_component(klass, *args, **kw, &block) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/nuklear/dsl.rb', line 10 def add_component(klass, *args, **kw, &block) # if klass initialize accepts a block, pass the block into that. The # caller must call .dsl on the result if they want a nested DSL. if block_given? && klass.instance_method(:initialize).parameters.last[0] == :block instance = klass.new(*args, **kw, &block) @ctx << instance else # assume block is for a nested DSL instance = klass.new(*args, **kw) @ctx << instance instance.dsl(&block) if block_given? end instance end |
#respond_to?(name) ⇒ Boolean
28 29 30 |
# File 'lib/nuklear/dsl.rb', line 28 def respond_to?(name) super || @ctx.respond_to?(name) end |