Module: Kind::Try

Extended by:
Try
Included in:
Try
Defined in:
lib/kind/try.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](*args) ⇒ Object



31
32
33
34
35
# File 'lib/kind/try.rb', line 31

def self.[](*args)
  method_name = args.shift

  ->(object) { call!(object, method_name, args) }
end

Instance Method Details

#call(object, *input) {|result| ... } ⇒ Object

Yields:

  • (result)


17
18
19
20
21
22
23
24
25
# File 'lib/kind/try.rb', line 17

def call(object, *input)
  args = input.size == 1 && input[0].kind_of?(::Array) ? input[0] : input

  result = call!(object, args.shift, args)

  return result unless block_given?

  yield(result) unless KIND.nil_or_undefined?(result)
end

#call!(object, method_name, args = Empty::ARRAY) ⇒ Object

:nodoc



11
12
13
14
15
# File 'lib/kind/try.rb', line 11

def call!(object, method_name, args = Empty::ARRAY) # :nodoc
  return if KIND.nil_or_undefined?(object)

  resolve(object, method_name, args)
end

#presence(*args, &block) ⇒ Object



27
28
29
# File 'lib/kind/try.rb', line 27

def presence(*args, &block)
  Presence.(call(*args, &block))
end