Class: Vidalia::ObjectDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/vidalia/dsl.rb,
lib/vidalia/object_definition.rb

Constant Summary collapse

@@defined_method_names =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &block) ⇒ ObjectDefinition

Create an Object Definition

Under the covers, the ObjectDefinition will create an associated Object definition. Any instantiated Object under the parent Interface will be copied from this master copy.

Options

Takes two parameters: Takes a hash as input where the current options are:

name

(required) specifies the name of the Object

parent

(required) specifies the name of the parent Interface

Takes a block to be executed at initialization time

Example

$$$ Example needed $$$


27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vidalia/object_definition.rb', line 27

def initialize(opts = {}, &block)
  o = {
    :name => nil,
    :parent => nil
  }.merge(opts)

  Vidalia::checkvar(o[:name],String,self.class.ancestors,"name")
  Vidalia::checkvar(o[:parent],Vidalia::InterfaceDefinition,self.class.ancestors,"parent")

  o[:parent] = o[:parent].interface
  @object = Vidalia::Object.new(o,&block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth_id, &block) ⇒ Object



42
43
44
45
46
47
# File 'lib/vidalia/dsl.rb', line 42

def method_missing(meth_id, &block)
  @object.add_method(name: meth_id.to_s, &block)
  # TODO: Honestly not sure why I need this and why add_method doesn't
  # take care of it
  Vidalia::Object.define_method_for_object_class meth_id.to_s
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



5
6
7
# File 'lib/vidalia/object_definition.rb', line 5

def children
  @children
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/vidalia/object_definition.rb', line 5

def name
  @name
end

#objectObject (readonly)

Returns the value of attribute object.



5
6
7
# File 'lib/vidalia/object_definition.rb', line 5

def object
  @object
end

#parentObject (readonly)

Returns the value of attribute parent.



5
6
7
# File 'lib/vidalia/object_definition.rb', line 5

def parent
  @parent
end

Instance Method Details

#add_method(opts = {}, &block) ⇒ Object

Define a method to act on a defined Object

Define a method that any Vidalia::Object can run and save the method code in the Vidalia::Object represented with this definition

Options

Takes a hash as input where the current options are:

name

specifies the name of the method

Takes a block that defines the code to run for that method

Example

$$$ Example needed $$$


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/vidalia/object_definition.rb', line 56

def add_method(opts = {},&block)
  o = {
    :name => nil
  }.merge(opts)

  name = o[:name]
  Vidalia::checkvar(name,String,self.class.ancestors,"name")

  # Save the method code in the Vidalia::Object represented with this definition
  @object.add_method(o,&block)

  # Define a method that any Vidalia::Object can run
  unless @@defined_method_names[name]
    @@defined_method_names[name] = true
    Vidalia::Object.define_method_for_object_class(name)
  end

end

#element(name, &block) ⇒ Object



49
50
51
52
53
# File 'lib/vidalia/dsl.rb', line 49

def element(name, &block)
  elem_def = Vidalia::ElementDefinition.new(name: name, parent: self)
  elem_def.instance_eval &block if block
  elem_def.element
end

#init(&block) ⇒ Object



38
39
40
# File 'lib/vidalia/dsl.rb', line 38

def init(&block)
  @object.init_block = block
end