Class: Sourcerer::ProcSource

Inherits:
SourceCode show all
Defined in:
lib/sourcerer/proc_source.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SourceCode

#convert_from_method!, #convert_from_proc!, #frequency, #source_formater_for_line_sub

Class Attribute Details

.eval_keysObject

Returns the value of attribute eval_keys.



6
7
8
# File 'lib/sourcerer/proc_source.rb', line 6

def eval_keys
  @eval_keys
end

.source_cacheObject

Returns the value of attribute source_cache.



5
6
7
# File 'lib/sourcerer/proc_source.rb', line 5

def source_cache
  @source_cache
end

Class Method Details

.build(source_code_to_be_wrappered, *params_obj_array) ⇒ Object



11
12
13
# File 'lib/sourcerer/proc_source.rb', line 11

def self.build(source_code_to_be_wrappered,*params_obj_array)
  self.new(source_code_to_be_wrappered).wrapper_around!(*params_obj_array)
end

Instance Method Details

#bodyObject



51
52
53
54
55
56
57
58
59
# File 'lib/sourcerer/proc_source.rb', line 51

def body

  body= ProcSourceBody.new(self.dup.to_s)
  body.sub!(/.*Proc\.new *{ *(\|.*\|)?/,String.new)
  body.gsub!(/^$\n/, String.new)
  body.sub!(/\s*}\s*$/,String.new)

  return body
end

#parametersObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/sourcerer/proc_source.rb', line 73

def parameters

  return_array= Array.new
  params= self.params
  params.each do |one_raw_parameter|
    case true
      when one_raw_parameter.include?('=')
        begin
          return_array.push Array.new.push(:opt).push(
                                one_raw_parameter.split('=')[0].to_sym)
        end
      when one_raw_parameter[0] == '*'
        begin
          one_raw_parameter[0]= ''
          return_array.push Array.new.push(:rest).push(
                                one_raw_parameter.to_sym)
        end
      else
        begin
          return_array.push Array.new.push(:req).push(
                                one_raw_parameter.to_sym)
        end
    end
  end

  return return_array
end

#paramsObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sourcerer/proc_source.rb', line 61

def params
  #SystemStackError
  params= self.dup
  params.sub!(params.split("\n")[0].scan(/\s*Proc.new\s*{/)[0],String.new)
  params.sub!(' ','')
  params= params.split("\n")[0].scan(/^\s*{?\s*(.*)/)[0][0].gsub!('|','')
  if params.nil?
    return nil
  end
  return ProcSourceParams[*params.split(',')]
end

#to_proc(binding = nil) ⇒ Object

create process object from valid process string



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sourcerer/proc_source.rb', line 26

def to_proc(binding=nil)
  begin

    unless self.split("\n")[0].include?('Proc.new')
      raise ArgumentError, "string obj is not a valid process source"
    end

    return_proc= nil
    if binding.nil?
      return_proc= eval(self)
    else
      return_proc= eval(self,binding)
    end

    if ProcSource.eval_keys.count > 1000
      ProcSource.eval_keys.each {|e| ProcSource.source_cache.delete(e) }
      ProcSource.eval_keys.clear
    end
    ProcSource.source_cache[return_proc.inspect]= self
    ProcSource.eval_keys.push return_proc.inspect

    return return_proc
  end
end

#wrapper_around!(*params) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/sourcerer/proc_source.rb', line 15

def wrapper_around!(*params)
  if !params.nil?
    params= params.join(',')
    params.prepend(' |')
    params.concat('|')
  end
  self.prepend("Proc.new{#{params}\n")
  self.concat("\n}")
end