Class: OpenDsl::Context

Inherits:
Object
  • Object
show all
Includes:
StringHelpers
Defined in:
lib/open_dsl/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StringHelpers

#attribute_name, #constant_or_constant_name?, #plural?

Constructor Details

#initialize(const_name, &blk) ⇒ Context

Returns a new instance of Context.



6
7
8
9
10
# File 'lib/open_dsl/context.rb', line 6

def initialize(const_name, &blk)
  @stack = EvalStack.new(self)
  @toplevel_object = constant_or_constant_name?(const_name) ? new_instance(const_name) : mark_as_created_by_open_dsl(Array.new)
  @stack.eval_and_keep(@toplevel_object, &blk)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blk) ⇒ Object (protected)



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/open_dsl/context.rb', line 14

def method_missing(name, *args, &blk)
  # TODO: detect an internal error?

  if constant_or_constant_name?(name)
    assign_constant(name, &blk)
  elsif blk
    if constant_or_constant_name?(args.first)
      assign_constant_to_explicit_attribute(name, args.first, &blk)
    else
      if plural?(name)
        assign_collection(name, &blk)
      else
        assign_attribute_with_block(name, &blk)
      end
    end
  else
    assign_attribute(name, *args)
  end
end

Instance Attribute Details

#toplevel_objectObject (readonly)

Returns the value of attribute toplevel_object.



4
5
6
# File 'lib/open_dsl/context.rb', line 4

def toplevel_object
  @toplevel_object
end