Class: Brauser::Value

Inherits:
Object
  • Object
show all
Defined in:
lib/brauser/value.rb

Overview

A defined entity, which supports comparison against a single or multiple values.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Value

Creates a new value

Parameters:

  • value (Object)

    The wrapped value.



18
19
20
# File 'lib/brauser/value.rb', line 18

def initialize(value)
  @value = value
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Delegates all the other values to the wrapped value.

Parameters:

  • method (Symbol)

    The method to call.

  • args (Array)

    The arguments to pass to the method.

  • block (Proc)

    The block to pass to the method.



35
36
37
# File 'lib/brauser/value.rb', line 35

def method_missing(method, *args, &block)
  @value.send(method, *args, &block)
end

Instance Attribute Details

#valueObject (readonly)

Returns The wrapped value.

Returns:

  • (Object)

    The wrapped value.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/brauser/value.rb', line 11

class Value
  attr_reader :value
  delegate :to_s, :inspect, to: :value

  # Creates a new value
  #
  # @param value [Object] The wrapped value.
  def initialize(value)
    @value = value
  end

  # Check if an object is equal to another object or if it is contained in a list of objects.
  #
  # @param other [Array|Object] The other object to match.
  # @return [Boolean] `true` if the current object is either equal or contained in the other object, `false` otherwise.
  def ==(other)
    other.is_a?(Array) ? other.include?(@value) : (@value == other)
  end

  # Delegates all the other values to the wrapped value.
  #
  # @param method [Symbol] The method to call.
  # @param args [Array] The arguments to pass to the method.
  # @param block [Proc] The block to pass to the method.
  def method_missing(method, *args, &block)
    @value.send(method, *args, &block)
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Check if an object is equal to another object or if it is contained in a list of objects.

Parameters:

  • other (Array|Object)

    The other object to match.

Returns:

  • (Boolean)

    true if the current object is either equal or contained in the other object, false otherwise.



26
27
28
# File 'lib/brauser/value.rb', line 26

def ==(other)
  other.is_a?(Array) ? other.include?(@value) : (@value == other)
end