Class: ProcSource

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/proc_extensions/proc_source.rb

Constant Summary collapse

SOURCIFY_ERRORS =
[
  Sourcify::MultipleMatchingProcsPerLineError,
  Sourcify::CannotParseEvalCodeError,
  Sourcify::CannotHandleCreatedOnTheFlyProcError
]

Instance Method Summary collapse

Constructor Details

#initialize(proc = nil, &block) ⇒ ProcSource

Returns a new instance of ProcSource.



13
14
15
16
17
18
19
20
# File 'lib/proc_extensions/proc_source.rb', line 13

def initialize(proc = nil, &block)
  if proc
    fail ArgumentError, 'cannot pass both an argument and a block' if block
    fail ArgumentError, 'argument must be a Proc'                  unless proc.is_a?(Proc)
  end

  @proc = proc || block
end

Instance Method Details

#==(other) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/proc_extensions/proc_source.rb', line 22

def ==(other)
  case
  when other.proc == proc
    true
  when other.arity != arity || other.lambda? != lambda?
    false
  else
    other.sexp == sexp
  end
rescue *SOURCIFY_ERRORS
  false
end

#match(other) ⇒ Object Also known as: =~



35
36
37
38
39
# File 'lib/proc_extensions/proc_source.rb', line 35

def match(other)
  self == other || source_equal(other)
rescue *SOURCIFY_ERRORS
  false
end

#raw_sourceObject



43
44
45
# File 'lib/proc_extensions/proc_source.rb', line 43

def raw_source
  extract_source(:to_raw_source)
end

#sourceObject



47
48
49
# File 'lib/proc_extensions/proc_source.rb', line 47

def source
  extract_source(:to_source)
end

#to_sObject Also known as: inspect



51
52
53
54
55
# File 'lib/proc_extensions/proc_source.rb', line 51

def to_s
  source
rescue *SOURCIFY_ERRORS
  proc.to_s
end