procemon 0.1.5 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5
1
+ 0.2.0
@@ -0,0 +1,43 @@
1
+ require_relative "../lib/procemon.rb"
2
+
3
+
4
+
5
+ test= Proc.new do |*params|
6
+
7
+ puts "some awsome code here"
8
+
9
+ end
10
+
11
+ def hello_this sup,yo,lazy=nil
12
+
13
+ puts "this is the Body!"
14
+
15
+ end
16
+
17
+
18
+ method_source= method(:hello_this).source
19
+ #Proc.new{ |sup,yo,lazy=nil,*params|
20
+ #
21
+ # puts "this is the Body!"
22
+ #
23
+ #}
24
+
25
+
26
+ proc_source= test.source
27
+ #Proc.new{ |*params|
28
+ #
29
+ # puts "some awsome code here"
30
+ #
31
+ #}
32
+
33
+ # example for terminal run
34
+ #puts method_source
35
+ #puts method_source.body,"---------"
36
+ #puts method_source.params,"---------"
37
+ #puts method_source.parameters.inspect,"---------"
38
+ #puts method_source.params.inspect,"---------"
39
+
40
+ merged_proc= ( method_source.body + proc_source.body ).build(*(method_source.params+proc_source.params))
41
+ puts merged_proc
42
+
43
+
@@ -2,16 +2,15 @@ class Method
2
2
 
3
3
  # creatue a raw eval-able process source, so you can set
4
4
  # the right bindings using the .to_proc call from String methods
5
- @@source_cache= Hash.new
6
5
  def source
7
6
 
8
7
  # defaults
9
8
  begin
10
- return_string= String.new
9
+ return_string= ProcSource.new
11
10
  block= 0
12
11
  end
13
- unless @@source_cache[self.object_id].nil?
14
- return @@source_cache[self.object_id]
12
+ unless Proc.source_cache[self.object_id].nil?
13
+ return Proc.source_cache[self.object_id]
15
14
  else
16
15
 
17
16
  File.open(File.expand_path(self.source_location[0])
@@ -21,23 +20,31 @@ class Method
21
20
  break if block == 0
22
21
  end
23
22
 
23
+ begin
24
+
25
+ args_to_replace = return_string.split("\n")[0].match(/\s*def\s*\S*\s*(.*)/).captures[0]
26
+
27
+ if args_to_replace != String.new
28
+ return_string.sub!(args_to_replace,"|#{args_to_replace}|")
29
+ end
30
+
31
+ rescue TypeError,NilError
32
+ end
33
+
24
34
  return_string.sub!(/\s*\bdef\s*[\w\S]*/,'Proc.new{')
25
35
  return_string.sub!(/}[^}]*$/,"}")
26
36
 
27
- return_string.sub!(
28
- return_string.scan(/\w*\s*,\s*\w*/)[0],
29
- '|'+return_string.scan(/\w*\s*,\s*\w*/)[0]+'|'
30
- )
31
37
 
32
38
  if !return_string.include?('Proc.new')
33
39
  return_string.sub!(/^[^{]*(?!={)/,'Proc.new')
34
40
  end
35
41
 
36
- @@source_cache[self.object_id]= return_string
42
+ Proc.source_cache[self.object_id]= return_string
37
43
 
38
44
  return return_string
39
45
  end
40
46
  end
47
+
41
48
  alias :source_string :source
42
49
  alias :proc_source :source
43
50
 
@@ -0,0 +1,40 @@
1
+ class Proc
2
+
3
+ # create a raw eval-able process source, so you can set
4
+ # the right bindings using the .to_proc call from String methods
5
+ class << self
6
+ attr_accessor :source_cache
7
+ end
8
+ Proc.source_cache= Hash.new
9
+ def source
10
+ # defaults
11
+ begin
12
+ return_string= ProcSource.new
13
+ block= 0
14
+ end
15
+
16
+ unless Proc.source_cache[self.object_id].nil?
17
+ return Proc.source_cache[self.object_id]
18
+ else
19
+ File.open(File.expand_path(self.source_location[0])
20
+ ).each_line_from self.source_location[1] do |line|
21
+ block += line.source_formater_for_line_sub
22
+ return_string.concat(line)
23
+ break if block == 0
24
+ end
25
+
26
+ return_string.sub!(/^[\w\W]*Proc.new\s*{/,'Proc.new{')
27
+ return_string.sub!(/}[^}]*$/,"}")
28
+
29
+ if !return_string.include?('Proc.new')
30
+ return_string.sub!(/^[^{]*(?!={)/,'Proc.new')
31
+ end
32
+
33
+ Proc.source_cache[self.object_id]= return_string
34
+
35
+ return return_string
36
+ end
37
+ end
38
+ alias :source_string :source
39
+
40
+ end
@@ -0,0 +1,93 @@
1
+ class ProcSource < String
2
+
3
+ def self.build(source_code_to_be_wrappered,*params_obj_array)
4
+ self.new(source_code_to_be_wrappered).wrapper_around!(*params_obj_array)
5
+ end
6
+
7
+ def wrapper_around!(*params)
8
+ if !params.nil?
9
+ params= params.join(',')
10
+ params.prepend(' |')
11
+ params.concat('|')
12
+ end
13
+ self.prepend("Proc.new{#{params}\n")
14
+ self.concat("\n}")
15
+ end
16
+
17
+ # create process object from valid process string
18
+ def to_proc(binding=nil)
19
+ begin
20
+
21
+ if !self.include?('Proc.new') && !self.include?('lambda')
22
+ raise ArgumentError, "string obj is not a valid process source"
23
+ end
24
+
25
+ return_proc= nil
26
+ if binding.nil?
27
+ return_proc= eval(self)
28
+ else
29
+ return_proc= eval(self,binding)
30
+ end
31
+
32
+ # do cache to proc!
33
+ begin
34
+ puts Proc.source_cache.inspect
35
+
36
+ #Proc.source_cache[return_proc.object_id]= self
37
+ end
38
+
39
+ eval(self)
40
+ end
41
+ end
42
+
43
+ def body
44
+ body= ProcSourceBody.new(self.dup.to_s)
45
+ body.sub!(body.split("\n")[0].scan(/\s*Proc.new\s*{/)[0],String.new)
46
+ replace2= body.split("\n")[0].scan(/^\s*{?\s*(.*)/)[0][0]
47
+ body.sub!(replace2,String.new) if replace2 != String.new
48
+ body.gsub!(/^$\n/, String.new)
49
+ body[body.length-1]= String.new
50
+ return body
51
+ end
52
+
53
+ def params
54
+ #SystemStackError
55
+ params= self.dup
56
+ params.sub!(params.split("\n")[0].scan(/\s*Proc.new\s*{/)[0],String.new)
57
+ params.sub!(' ','')
58
+ params= params.split("\n")[0].scan(/^\s*{?\s*(.*)/)[0][0].gsub!('|','')
59
+ if params.nil?
60
+ return nil
61
+ end
62
+ return ProcSourceParams[*params.split(',')]
63
+ end
64
+
65
+ def parameters
66
+
67
+ return_array= Array.new
68
+ params= self.params
69
+ params.each do |one_raw_parameter|
70
+ case true
71
+ when one_raw_parameter.include?('=')
72
+ begin
73
+ return_array.push Array.new.push(:opt).push(
74
+ one_raw_parameter.split('=')[0].to_sym)
75
+ end
76
+ when one_raw_parameter[0] == '*'
77
+ begin
78
+ one_raw_parameter[0]= ''
79
+ return_array.push Array.new.push(:rest).push(
80
+ one_raw_parameter.to_sym)
81
+ end
82
+ else
83
+ begin
84
+ return_array.push Array.new.push(:req).push(
85
+ one_raw_parameter.to_sym)
86
+ end
87
+ end
88
+ end
89
+
90
+ return return_array
91
+ end
92
+
93
+ end
@@ -0,0 +1,12 @@
1
+ class ProcSourceBody < String
2
+
3
+ def +(oth_str)
4
+ ProcSourceBody.new(self.to_s + oth_str.to_s)
5
+ end
6
+
7
+ # build proc source
8
+ def build(*params)
9
+ ProcSource.build(self.to_s,*params)
10
+ end
11
+
12
+ end
@@ -0,0 +1,35 @@
1
+ class ProcSourceParams < Array
2
+
3
+ # merge two proc params obj
4
+ # if multiple rest obj found
5
+ # it will remove and make an *args obj as last element
6
+ # if they are not equal ,
7
+ # else it makes it the last element only
8
+ def +(oth_ary)
9
+
10
+ merged_array= (Array[*self]+Array[*oth_ary])
11
+
12
+ rest_state= nil
13
+ rest_elements= Array.new
14
+ merged_array.dup.each do |element|
15
+ if element.include? '*'
16
+ merged_array.delete(element)
17
+ rest_state ||= true
18
+ rest_elements.push(element)
19
+ end
20
+ end
21
+
22
+ rest_elements.uniq!
23
+ if rest_elements.count == 1 && !rest_elements.empty?
24
+ merged_array.push(rest_elements[0])
25
+ rest_state= nil
26
+ end
27
+
28
+ unless rest_state.nil?
29
+ merged_array.push('*args')
30
+ end
31
+
32
+ return ProcSourceParams[*merged_array]
33
+ end
34
+
35
+ end
@@ -0,0 +1,14 @@
1
+ class String
2
+
3
+ # this is a helper to create source strings from procs
4
+ def source_formater_for_line_sub
5
+ self.gsub!(';',"\n")
6
+ self.gsub!(/\bdo\b/,'{')
7
+ self.gsub!(/\bend\b/,'}')
8
+
9
+ self.frequency(/{/)+
10
+ self.frequency(/def/)-
11
+ self.frequency(/}/)
12
+ end
13
+
14
+ end
@@ -1,5 +1,6 @@
1
1
  # duck typing
2
2
  class String
3
+
3
4
  def duck
4
5
 
5
6
  begin
@@ -0,0 +1,21 @@
1
+ class Object
2
+
3
+ def must_be class_name
4
+
5
+ if class_name.class == Class
6
+ begin
7
+ if self.class != class_name
8
+ raise ArgumentError, "invalid parameter given: #{self}"
9
+ end
10
+ end
11
+ else
12
+ begin
13
+ if self != class_name
14
+ raise ArgumentError, "invalid parameter given: #{self}"
15
+ end
16
+ end
17
+ end
18
+ return nil
19
+ end unless method_defined? :must_be
20
+
21
+ end
@@ -0,0 +1,15 @@
1
+ class Object
2
+
3
+ def boolean?
4
+ !!self == self
5
+ end
6
+
7
+ def true?
8
+ self == true
9
+ end
10
+
11
+ def false?
12
+ self == false
13
+ end
14
+
15
+ end
@@ -9,40 +9,6 @@ class Proc
9
9
  Proc.new { |*args| self[*other[*args]] }
10
10
  end unless method_defined? :*
11
11
 
12
- # create a raw eval-able process source, so you can set
13
- # the right bindings using the .to_proc call from String methods
14
- @@source_cache= Hash.new
15
- def source
16
- # defaults
17
- begin
18
- return_string= String.new
19
- block= 0
20
- end
21
-
22
- unless @@source_cache[self.object_id].nil?
23
- return @@source_cache[self.object_id]
24
- else
25
- File.open(File.expand_path(self.source_location[0])
26
- ).each_line_from self.source_location[1] do |line|
27
- block += line.source_formater_for_line_sub
28
- return_string.concat(line)
29
- break if block == 0
30
- end
31
-
32
- return_string.sub!(/^[\w\W]*Proc.new\s*{/,'Proc.new{')
33
- return_string.sub!(/}[^}]*$/,"}")
34
-
35
- if !return_string.include?('Proc.new')
36
- return_string.sub!(/^[^{]*(?!={)/,'Proc.new')
37
- end
38
-
39
- @@source_cache[self.object_id]= return_string
40
-
41
- return return_string
42
- end
43
- end
44
- alias :source_string :source
45
-
46
12
  def call_with_binding(bind, *args)
47
13
  Bindless.new([bind]).run_proc(self, *args)
48
14
  end
@@ -52,7 +18,8 @@ class Proc
52
18
  p = self
53
19
  Object.class_eval do
54
20
  define_method :a_temp_method_name, &p
55
- m = instance_method :a_temp_method_name; remove_method :a_temp_method_name
21
+ m = instance_method :a_temp_method_name
22
+ remove_method :a_temp_method_name
56
23
  end
57
24
  m.bind(obj).call(*args)
58
25
  end
@@ -75,33 +75,4 @@ class String
75
75
  self.scan(/#{str}/).count
76
76
  end
77
77
 
78
- # create process object from valid process string
79
- def to_proc(binding=nil)
80
- begin
81
-
82
- if !self.include?('Proc.new') && !self.include?('lambda')
83
- raise ArgumentError, "string obj is not a valid process source"
84
- end
85
-
86
- if binding.nil?
87
- return eval(self)
88
- else
89
- return eval(self,binding)
90
- end
91
-
92
- end
93
- end
94
-
95
- # this is a helper to create source strings from procs
96
- def source_formater_for_line_sub
97
- self.gsub!(';',"\n")
98
- self.gsub!(/\bdo\b/,'{')
99
- self.gsub!(/\bend\b/,'}')
100
-
101
- self.frequency(/{/)+
102
- self.frequency(/def/)-
103
- self.frequency(/}/)
104
- end
105
-
106
-
107
78
  end
data/test/test.rb CHANGED
@@ -1,11 +0,0 @@
1
- require_relative "../lib/procemon.rb"
2
- require_relative "lab"
3
-
4
- #hello_world!
5
- #puts Dir.pwd.concat(Proc.new{
6
- # "hy"
7
- #}.source_location[0])
8
-
9
- puts method(:hello_world!).source
10
-
11
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: procemon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-20 00:00:00.000000000 Z
12
+ date: 2013-12-22 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! 'This is a collection of my Ruby Procs in the adventure of becoming
15
15
  the best! In short this provides extra tools in Application configs, argumens processing,daemonise,
@@ -27,6 +27,7 @@ files:
27
27
  - README.md
28
28
  - Rakefile
29
29
  - VERSION
30
+ - examples/fun_with_procs_and_methods.rb
30
31
  - examples/how_to_inject_with_extra_process_a_method.rb
31
32
  - files.rb
32
33
  - lib/procemon.rb
@@ -41,19 +42,26 @@ files:
41
42
  - lib/procemon/function/meta/inject_methods.rb
42
43
  - lib/procemon/function/name.rb
43
44
  - lib/procemon/function/port.rb
45
+ - lib/procemon/function/proc_source/method.rb
46
+ - lib/procemon/function/proc_source/proc.rb
47
+ - lib/procemon/function/proc_source/proc_source.rb
48
+ - lib/procemon/function/proc_source/proc_source_body.rb
49
+ - lib/procemon/function/proc_source/proc_source_params.rb
50
+ - lib/procemon/function/proc_source/string.rb
44
51
  - lib/procemon/function/require.rb
45
52
  - lib/procemon/function/sender.rb
46
53
  - lib/procemon/function/str2duck.rb
47
54
  - lib/procemon/function/systemu.rb
48
55
  - lib/procemon/function/tmp_dir.rb
56
+ - lib/procemon/function/validation.rb
49
57
  - lib/procemon/mpatch/array.rb
58
+ - lib/procemon/mpatch/boolean.rb
50
59
  - lib/procemon/mpatch/class.rb
51
60
  - lib/procemon/mpatch/exception.rb
52
61
  - lib/procemon/mpatch/file.rb
53
62
  - lib/procemon/mpatch/hash.rb
54
63
  - lib/procemon/mpatch/integer.rb
55
64
  - lib/procemon/mpatch/kernel.rb
56
- - lib/procemon/mpatch/method.rb
57
65
  - lib/procemon/mpatch/module.rb
58
66
  - lib/procemon/mpatch/object.rb
59
67
  - lib/procemon/mpatch/proc.rb