Module: Rutema::SpecificationElement

Included in:
Scenario, Specification, Step
Defined in:
lib/rutema/core/objectmodel.rb

Overview

Mix-in module allowing to add attributes to a class on the go

This allows to add attributes to classes on the go and makes the accessor methods appear automatically too.

It will add a #has_attribute? method too to query if an attribute is part of the object or not.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object

Method enabling to call object.attribute, object.attribute=, object.attribute? and object.has_attribute?

object.attribute and object.attribute? will throw NoMethodError if the attribute is not set on the object instance.

object.attribute= will set the attribute to the right operand and object.has_attribute? will return true or false according to the existence of the attribute.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rutema/core/objectmodel.rb', line 40

def method_missing(symbol, *args)
  @attributes ||= {}
  key = symbol.id2name.chomp("?").chomp("=").sub(/^has_/, "")
  @attributes[:"#{key}"] = args[0] if "#{key}=" == symbol.id2name
  if @attributes.key?(:"#{key}")
    return true if "has_#{key}?" == symbol.id2name

    return @attributes[:"#{key}"]
  else
    return false if "has_#{key}?" == symbol.id2name

    super
  end
end

Instance Method Details

#attribute(symbol, value) ⇒ Object

Add an attribute with a given value to the class instance

  • symbol - a symbol or a string (which will be converted to a symbol internally) which shall be the name of the new attribute
  • value - the initial value of the attribute

If symbol is neither a string nor a symbol this method will be a no-op.



23
24
25
26
27
28
29
# File 'lib/rutema/core/objectmodel.rb', line 23

def attribute(symbol, value)
  @attributes ||= {}
  case symbol
  when String then @attributes[:"#{symbol}"] = value
  when Symbol then @attributes[symbol] = value
  end
end

#respond_to?(symbol, include_all) ⇒ Boolean

Return true if the object responds to the given method or else false

Returns:

  • (Boolean)


61
62
63
64
65
66
67
# File 'lib/rutema/core/objectmodel.rb', line 61

def respond_to?(symbol, include_all)
  @attributes ||= {}
  key = symbol.id2name.chomp("?").chomp("=").sub(/^has_/, "")
  return true if @attributes.key?(:"#{key}")

  super
end

#respond_to_missing?(*_args) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/rutema/core/objectmodel.rb', line 55

def respond_to_missing?(*_args)
  true
end