Class: DSLCompose::DSL::DSLMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/dsl_compose/dsl/dsl_method.rb,
lib/dsl_compose/dsl/dsl_method/interpreter.rb

Defined Under Namespace

Classes: DescriptionAlreadyExistsError, Interpreter, InvalidDescriptionError, InvalidNameError, MethodNameIsReservedError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, unique, required, &block) ⇒ DSLMethod

Create a new DSLMethod object with the provided name and class.

‘name` must be a symbol. `unique` is a boolean which determines if this DSLMethod can only be called once witihn the DSL. `required` is a boolean which determines if this DSLMethod must be called at least once within the DSL. `block` contains the instructions to further configure this DSLMethod



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dsl_compose/dsl/dsl_method.rb', line 36

def initialize name, unique, required, &block
  @arguments = Arguments.new

  if name.is_a? Symbol

    # don't allow methods to override existing internal methods
    if Class.respond_to? name
      raise MethodNameIsReservedError, "This method #{name} would override an existing internal method"
    end

    @name = name
  else
    raise InvalidNameError, "The method name `#{name}` is invalid, it must be of type symbol"
  end

  @unique = unique ? true : false
  @required = required ? true : false

  # If a block was provided, then we evaluate it using a seperate
  # interpreter class. We do this because the interpreter class contains
  # no other methods or variables, if it was evaluated in the context of
  # this class then the block would have access to all of the methods defined
  # in here.
  if block
    Interpreter.new(self).instance_eval(&block)
  end
rescue => e
  raise e, "Error while defining method #{name}\n#{e.message}", e.backtrace
end

Instance Attribute Details

#argumentsObject (readonly)

an object which represents the argument configuration



28
29
30
# File 'lib/dsl_compose/dsl/dsl_method.rb', line 28

def arguments
  @arguments
end

#descriptionObject (readonly)

An otional description of this DSLMethod, if provided then it must be a string. The description accepts markdown and is used when generating documentation.



26
27
28
# File 'lib/dsl_compose/dsl/dsl_method.rb', line 26

def description
  @description
end

#nameObject (readonly)

The name of this DSLMethod.



19
20
21
# File 'lib/dsl_compose/dsl/dsl_method.rb', line 19

def name
  @name
end

#requiredObject (readonly)

if required, then this DSLMethod must be called at least once within the DSL.



23
24
25
# File 'lib/dsl_compose/dsl/dsl_method.rb', line 23

def required
  @required
end

#uniqueObject (readonly)

if unique, then this DSLMethod can only be called once within the DSL.



21
22
23
# File 'lib/dsl_compose/dsl/dsl_method.rb', line 21

def unique
  @unique
end

Instance Method Details

#has_description?Boolean

Returns ‘true` if this DSL has a description, else false.

Returns:

  • (Boolean)


83
84
85
# File 'lib/dsl_compose/dsl/dsl_method.rb', line 83

def has_description?
  @description.nil? == false
end

#optional?Boolean

returns true if this DSLMethod is flagged as optional, otherwise returns false.

Returns:

  • (Boolean)


98
99
100
# File 'lib/dsl_compose/dsl/dsl_method.rb', line 98

def optional?
  @required == false
end

#required?Boolean

returns true if this DSLMethod is flagged as required, otherwise returns false.

Returns:

  • (Boolean)


93
94
95
# File 'lib/dsl_compose/dsl/dsl_method.rb', line 93

def required?
  @required == true
end

#set_description(description) ⇒ Object

Set the description for this DSLMethod to the provided value.

‘description` must be a string with a length greater than 0. The `description` can only be set once per DSLMethod



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dsl_compose/dsl/dsl_method.rb', line 70

def set_description description
  unless description.is_a?(String) && description.strip.length > 0
    raise InvalidDescriptionError, "The DSL method description `#{description}` is invalid, it must be of type string and have length greater than 0"
  end

  if has_description?
    raise DescriptionAlreadyExistsError, "The description has already been set"
  end

  @description = description.strip
end

#unique?Boolean

returns true if this DSLMethod is flagged as unique, otherwise returns false.

Returns:

  • (Boolean)


88
89
90
# File 'lib/dsl_compose/dsl/dsl_method.rb', line 88

def unique?
  @unique == true
end