Class: Parameters::InstanceParam

Inherits:
Param show all
Defined in:
lib/parameters/instance_param.rb

Instance Attribute Summary collapse

Attributes inherited from Param

#description, #name, #type

Instance Method Summary collapse

Methods inherited from Param

#coerce

Constructor Details

#initialize(object, name, type = nil, description = nil, value = nil) ⇒ InstanceParam

Creates a new InstanceParam object.

Parameters:

  • object (Object)

    The object containing the instance variable for the instance parameter.

  • name (Symbol, String)

    The name of the instance parameter.

  • type (Class, Array[Class]) (defaults to: nil)

    The enforced type of the instance parameter.

  • description (String, nil) (defaults to: nil)

    The description of the instance parameter.

  • value (Object) (defaults to: nil)

    The initial value for the instance parameter.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/parameters/instance_param.rb', line 30

def initialize(object,name,type=nil,description=nil,value=nil)
  super(name,type,description)

  @object = object

  if (self.value.nil? && value)
    self.value = case value
                 when Proc
                   if value.arity > 0
                     value.call(@object)
                   else
                     value.call()
                   end
                 else
                   begin
                     value.clone
                   rescue TypeError
                     value
                   end
                 end
  end
end

Instance Attribute Details

#objectObject (readonly)

Owning object



7
8
9
# File 'lib/parameters/instance_param.rb', line 7

def object
  @object
end

Instance Method Details

#inspectString

Inspects the instance parameter.

Returns:

  • (String)

    Inspection of the instance params value.



93
94
95
# File 'lib/parameters/instance_param.rb', line 93

def inspect
  "#<#{self.class}: #{value.inspect}>"
end

#to_sString

Returns Representation of the instance param.

Returns:

  • (String)

    Representation of the instance param.



78
79
80
81
82
83
84
85
# File 'lib/parameters/instance_param.rb', line 78

def to_s
  text = @name.to_s

  text << "\t[#{value.inspect}]" if value
  text << "\t#{@description}"    if @description

  return text
end

#valueObject

Returns The value of the instance param.

Returns:

  • The value of the instance param.



57
58
59
# File 'lib/parameters/instance_param.rb', line 57

def value
  @object.instance_variable_get(:"@#{@name}")
end

#value=(value) ⇒ Object

Sets the value of the instance param.

Parameters:

  • value (Object)

    The new value of the instance param.

Returns:

  • (Object)

    The new value of the instance param.



70
71
72
# File 'lib/parameters/instance_param.rb', line 70

def value=(value)
  @object.instance_variable_set(:"@#{@name}",coerce(value))
end