Class: Moon::Option

Inherits:
Object show all
Defined in:
lib/moon/packages/std/option.rb

Overview

Option is a container object for optional values, its main operation is map.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Option

Returns a new instance of Option.

Parameters:

  • value (Object)

    initial value


11
12
13
# File 'lib/moon/packages/std/option.rb', line 11

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject

Returns:


8
9
10
# File 'lib/moon/packages/std/option.rb', line 8

def value
  @value
end

Instance Method Details

#blank?Boolean

Determines if the value is valid or not, if the value is nil, then it is blank, everything else is none blank.

Returns:


39
40
41
# File 'lib/moon/packages/std/option.rb', line 39

def blank?
  @value == nil
end

#map(method_name, *args, &block) ⇒ Void #map {|value| ... } ⇒ Void

If method_name is given, calls the function with the provided args and block and replaces the value with the result. Otherwise, map will take a block and replace the value with the result of the block call.

Overloads:

  • #map(method_name, *args, &block) ⇒ Void

    Parameters:

    • method_name (Symbol)

      method to call on value

  • #map {|value| ... } ⇒ Void

    Yield Parameters:

    • value (Object)

      current value

    Yield Returns:

    • (Object)

      new_value value to replace with

Returns:

  • (Void)

26
27
28
29
30
31
32
33
# File 'lib/moon/packages/std/option.rb', line 26

def map(method_name = nil, *args, &block)
  return if blank?
  if method_name
    @value = @value.public_send(method_name, *args, &block)
  else
    @value = block.call @value
  end
end

#presenceObject?

Returns the value unless the blank?

Returns:


46
47
48
# File 'lib/moon/packages/std/option.rb', line 46

def presence
  blank? ? nil : @value
end