sourcerer 0.5.2 → 0.6.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.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- YTRmMTIwMDg5YjMzODUyMWYwOWJlMDg0MTI1MTdkZWVlZWRmN2JjMw==
5
- data.tar.gz: !binary |-
6
- MmQ1ZDgwZjcxNWEyZjIzYTNmOTI0NmYwMzljYWUzMDY2MWQ4Yjc5MA==
2
+ SHA1:
3
+ metadata.gz: 444e4835573de25b1219563310d1e80fd6342458
4
+ data.tar.gz: 8dc550d20dd3c1f9a12b2c913ad64a9a44a8339a
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- YzdjNzVlYmM1Zjc3YTEyY2NmM2IzNDMyYjJjMDk0ZmE4NjRmMWQ0NGJmMDdi
10
- MjlkYzFkNzU4ZDhkZjQwMzNjNjg3N2MxYTNkYzRkZmExOWY5MTgxMTA0NWU5
11
- YWRmZjlkMzg3YWIzNDYxMDJlMjA3OWFjMWJiODExNDIyOWJkNTM=
12
- data.tar.gz: !binary |-
13
- YzcxNmE2ZmUyMjE2ZWRmOGI5MjNmNjUyMzljZmNmZGZmMjlmNjI5NmQ2YThl
14
- MjIzMmQ0YzY1YjY3Y2M2ZDU5YzdjYTM1N2M3NDJmOTI5NGYzMGIxZDQ0MTQy
15
- MzA5ZDgyNzg3MGU3NjYxNjZmNjAzMjY2OTYzODViMDMxMGU1OGY=
6
+ metadata.gz: 65a5891322114fb8c6cc8e8cbbe4d2b11eff6d6a3f2e77a84990d6961f5b311876834183874e4974988c76d6725e1bf3503a5b0b8c8efe5e56560fd8ace0a937
7
+ data.tar.gz: adea9759f5886b3e00df60f4e014925df78ece5a25a436d7aa47d70efe1ac1c3ac85251b87921ae9fe4f99b63a16f9b5001f5bbf8176bc3e6bcbee1ee19cb9d6
data/README.md CHANGED
@@ -1,4 +1,83 @@
1
1
  sourcerer
2
2
  =========
3
3
 
4
- Source code reader to make eval able proc source codes from methods , unbound methods, and processes
4
+ Source code reader to make eval able proc source codes from methods , unbound methods, and processes (+ lambda)
5
+
6
+ it will always return a proc code like "Proc.new { super_code_here }"
7
+ check examples how easy to get source code.
8
+
9
+ ### Install
10
+
11
+ bash:
12
+
13
+ $ gem install sourcerer
14
+
15
+ Gemfile:
16
+
17
+ ```ruby
18
+
19
+ gem 'sourcerer'
20
+
21
+ ```
22
+
23
+ ### Example
24
+
25
+ ```ruby
26
+
27
+ require "sourcerer"
28
+
29
+ #> input
30
+ def test var, opts={}, *args, &block
31
+ puts var
32
+ if true
33
+
34
+ end
35
+ end
36
+
37
+ asdf= lambda{
38
+
39
+ puts "hy"
40
+
41
+ }
42
+
43
+ asd = Proc.new { |var, opts={}, *args, &block|
44
+
45
+ puts "WHAAAAAAAAAT?"
46
+
47
+ puts opts.inspect
48
+
49
+ }
50
+
51
+ puts asdf.source
52
+ puts asd.source
53
+ puts method(:test).source
54
+
55
+ #> output
56
+
57
+ # Proc.new {
58
+ #
59
+ # puts "hy"
60
+ #
61
+ # }
62
+ #
63
+ # Proc.new { |var, opts={}, *args, &block|
64
+ #
65
+ # puts "WHAAAAAAAAAT?"
66
+ #
67
+ # puts opts.inspect
68
+ #
69
+ # }
70
+ #
71
+ # Proc.new { |var, opts={}, *args, &block|
72
+ # puts var
73
+ # if true
74
+ #
75
+ # end
76
+ # }
77
+ #
78
+
79
+ ```
80
+
81
+ ### after words
82
+
83
+ if you find any bug please report to me :)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.2
1
+ 0.6.0
@@ -7,8 +7,7 @@ test= Proc.new do |sym,options={},*params,&block|
7
7
 
8
8
  end # Proc
9
9
 
10
- #puts method(:hello_this).source
11
- #puts test.source
10
+ puts test.source
12
11
 
13
12
  class HelloWorld
14
13
 
@@ -26,8 +25,4 @@ class HelloWorld
26
25
 
27
26
  end
28
27
 
29
- require 'debugger'
30
- #debugger
31
-
32
- #TODO finish up method reader
33
28
  puts HelloWorld.method(:hello).source
data/examples/test.rb ADDED
@@ -0,0 +1,16 @@
1
+ require_relative "../lib/sourcerer.rb"
2
+
3
+ simple_process_definition= Proc.new{ |variable|
4
+
5
+ if variable.class == String
6
+ puts "OMG it's a string"
7
+ else
8
+ puts "meh..."
9
+ end
10
+
11
+ }
12
+
13
+
14
+ simple_process_definition.source
15
+
16
+ puts $TEST
data/files.rb CHANGED
@@ -3,7 +3,10 @@ begin
3
3
 
4
4
  files_to_be_loaded = %w[version.rb]
5
5
 
6
- SpecFiles= Array.new
6
+ module Sourcerer
7
+ SpecFiles= Array.new
8
+ end
9
+
7
10
 
8
11
  Dir[File.expand_path(File.join(File.dirname(__FILE__),"**","*"))].sort.uniq.each do |one_file_name|
9
12
  one_file_name = File.expand_path one_file_name
@@ -11,11 +14,14 @@ begin
11
14
 
12
15
  if !one_file_name.include?("pkg")
13
16
  if !File.directory? file_name
14
- SpecFiles.push file_name
17
+
18
+ Sourcerer::SpecFiles.push file_name
19
+
15
20
  STDOUT.puts file_name if $DEBUG
16
21
  if files_to_be_loaded.include? one_file_name.split(File::SEPARATOR).last
17
22
  load one_file_name
18
23
  end
24
+
19
25
  end
20
26
  end
21
27
 
data/lib/sourcerer.rb CHANGED
@@ -1,17 +1,6 @@
1
1
  #encoding: UTF-8
2
- module Sourcerer
3
2
 
4
- require File.join(File.dirname(__FILE__),"sourcerer","helpers")
5
- require File.join(File.dirname(__FILE__),"sourcerer","cache")
6
-
7
- require File.join(File.dirname(__FILE__),"sourcerer","source_code")
8
- require File.join(File.dirname(__FILE__),"sourcerer","file")
9
-
10
- require File.join(File.dirname(__FILE__),"sourcerer","proc_source")
11
- require File.join(File.dirname(__FILE__),"sourcerer","proc_source_body")
12
- require File.join(File.dirname(__FILE__),"sourcerer","proc_source_params")
13
-
14
- require File.join(File.dirname(__FILE__),"sourcerer","method")
15
- require File.join(File.dirname(__FILE__),"sourcerer","proc")
16
-
17
- end
3
+ require "sourcerer/helpers"
4
+ require "sourcerer/cache"
5
+ require "sourcerer/source_code"
6
+ require "sourcerer/functions"
@@ -6,8 +6,6 @@ module Sourcerer
6
6
  include Singleton
7
7
  @@memory= []
8
8
 
9
-
10
-
11
9
  end
12
10
 
13
11
  end
@@ -0,0 +1,67 @@
1
+ module Sourcerer
2
+
3
+ EXT= Module.new
4
+ module EXT::SourceLocation
5
+
6
+ #> return a proc source code
7
+ def source
8
+
9
+ var= self.source_location.map{|obj| obj.class <= String ? File.absolute_path(obj) : obj }
10
+
11
+ file_obj= File.open(var[0],"r")
12
+ file_data= [] #> [*File.open(var[0],"r")]
13
+ (var[1] - 1).times{ file_obj.gets }
14
+
15
+ tags= 0
16
+ loop {
17
+
18
+ file_data.push(file_obj.gets.chomp)
19
+ new_string_line= file_data.last
20
+
21
+ tags += ::Sourcerer::Helpers.scan_count_by new_string_line, /\{/,:def,:do,:if,:unless,:loop,:while,:until
22
+ tags -= ::Sourcerer::Helpers.scan_count_by new_string_line, /\}/,:end
23
+
24
+ break if tags <= 0
25
+ break if file_data.last.nil?
26
+
27
+ }
28
+
29
+ self_obj= file_data.join("\n")
30
+ self_obj.gsub!(";","\n") unless %W[ ' " ].map!{|str| self_obj.include?(str) }.include?(true)
31
+
32
+ first_line= self_obj
33
+ case true
34
+
35
+ when first_line.include?('Proc')
36
+ self_obj.sub!(/^[\w =]*Proc.new\s*{ */,'Proc.new { ')
37
+
38
+ when first_line.include?('lambda')
39
+ self_obj.sub!(/^[\w =]*lambda\s*{ */,'Proc.new { ')
40
+
41
+ when first_line.include?('def'),first_line.include?('Method')
42
+ the_params= self_obj.scan(/ *def *[\w\.]*[\( ] *(.*)/)[0][0]
43
+ self_obj.sub!(
44
+ self_obj.split("\n")[0],
45
+ "Proc.new { |#{the_params}|"
46
+ )
47
+
48
+ replace_obj= self_obj.split("\n")
49
+ var= replace_obj.last.reverse.sub( /\bdne\b/,"-AAAAAAAAAAAA-").reverse.sub("-AAAAAAAAAAAA-","}")
50
+ replace_obj.pop
51
+ replace_obj.push(var)
52
+ self_obj.replace replace_obj.join("\n")
53
+
54
+
55
+ end
56
+
57
+
58
+ return ::Sourcerer::SourceCode.new(self_obj)
59
+
60
+ end
61
+
62
+ end
63
+
64
+
65
+ end
66
+
67
+ [Method,UnboundMethod,Proc].each{ |cls| cls.__send__(:include,Sourcerer::EXT::SourceLocation) }
@@ -1,44 +1,18 @@
1
1
  module Sourcerer
2
- class << self
2
+ module Helpers
3
+ class << self
3
4
 
4
- def raw_source obj
5
+ def scan_count_by str,*args
5
6
 
6
- # default values
7
- begin
8
- block = 0
9
- return_string = ProcSource.new
10
- end
11
-
12
- case RUBY_VERSION
13
-
14
- when "1.9.3"
15
- begin
16
-
17
- if obj.source_location.nil?
18
- puts "WARN: Native Ruby 1.9.3 can't return sourceless code sources"
19
- return "Proc.new { }"
20
- end
21
-
22
- SourceFile.open(File.expand_path(obj.source_location[0])
23
- ).each_line_from obj.source_location[1] do |line|
24
- line = SourceCode.new(line)
25
- block += line.source_formater_for_line_sub
26
- return_string.concat(line)
27
- break if block == 0
28
- end
29
-
30
- return return_string
31
-
32
- end
7
+ counter= 0
8
+ args.each do |sym|
9
+ sym= /\b#{sym}\b/ unless sym.class <= Regexp
10
+ counter += str.scan(sym).count
11
+ end
12
+ return counter
33
13
 
34
14
  end
35
15
 
36
- return nil
37
16
  end
38
-
39
- def cache
40
- Cache
41
- end
42
-
43
17
  end
44
18
  end
@@ -1,51 +1,7 @@
1
1
  module Sourcerer
2
2
  class SourceCode < String
3
3
 
4
- # return the number how often the str is with in the self
5
- # by default with \b regex border
6
- def frequency(str)
7
- begin
8
- if str.class == String
9
- str= '\b'+str+'\b'
10
- end
11
- end
12
- self.scan(/#{str}/).count
13
- end
14
4
 
15
- # this is a helper to create source strings from procs
16
- def source_formater_for_line_sub trim_comment_out= true
17
5
 
18
- if trim_comment_out == true
19
- trim_comment= self.match(/^.*#/)
20
- unless trim_comment.nil?
21
- self.replace trim_comment[0][0..(trim_comment[0].length-2)].concat("\n")
22
- end
23
- end
24
-
25
- self.frequency(/{/)+
26
- self.frequency(/def/)-
27
- self.frequency(/}/)
28
-
29
- end
30
-
31
- def convert_from_proc!
32
-
33
- self.sub!(/^[\w =]*Proc.new\s*{ */,'Proc.new { ')
34
-
35
- return self
36
- end
37
-
38
- def convert_from_method!
39
-
40
- the_params= self.scan(/ *def *[\w\.]*[\( ] *(.*)/)[0][0]
41
-
42
- self.sub!(
43
- self.split("\n")[0],
44
- "Proc.new { |#{the_params}|"
45
- )
46
-
47
- return self
48
-
49
- end
50
6
  end
51
7
  end
data/sourcerer.gemspec CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
14
14
  spec.homepage = "https://github.com/adamluzsi/sourcerer"
15
15
  spec.license = "MIT"
16
16
 
17
- spec.files = SpecFiles
17
+ spec.files = Sourcerer::SpecFiles
18
18
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
20
  spec.require_paths = ["lib"]
data/test/test.rb CHANGED
@@ -0,0 +1,65 @@
1
+
2
+ require "sourcerer"
3
+
4
+ def test var, opts={}, *args, &block
5
+ puts var
6
+ if true
7
+
8
+ end
9
+ end
10
+
11
+ asdf= lambda{
12
+
13
+ puts "hy"
14
+
15
+ }
16
+
17
+ asd = Proc.new { |var, opts={}, *args, &block|
18
+
19
+ puts "WHAAAAAAAAAT?"
20
+
21
+ puts opts.inspect
22
+
23
+ }
24
+
25
+ puts asdf.source
26
+ puts asd.source
27
+ puts method(:test).source
28
+
29
+
30
+ #> output
31
+ test= Proc.new do |sym,options={},*params,&block|
32
+
33
+ puts "some awsome code here" # comment
34
+ puts "yo"
35
+
36
+ end # Proc
37
+
38
+ puts test.source
39
+ # Proc.new { |sym,options={},*params,&block|
40
+ #
41
+ # puts "some awsome code here"
42
+ # puts "yo"
43
+ #
44
+ # }
45
+
46
+
47
+ class HelloWorld
48
+
49
+ #> TODO: comment remove from args
50
+ def self.hello sym,sym2= "#{sym}", options={},*params,&block
51
+
52
+ [1,2,3].each do |do_|
53
+
54
+ puts do_ # comment
55
+
56
+ end
57
+
58
+ puts "some code" # some comment
59
+
60
+ end
61
+
62
+ end
63
+
64
+ puts HelloWorld.method(:hello).source
65
+
metadata CHANGED
@@ -1,18 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sourcerer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Luzsi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-27 00:00:00.000000000 Z
11
+ date: 2014-04-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: ! ' DSL for for simple to use proc source generating from methods, unbound
13
+ description: " DSL for for simple to use proc source generating from methods, unbound
14
14
  methods and of course Proc/lambda. It will allow you to play with the source or
15
- even fuse two source code to make a new one and generate a proc from that. '
15
+ even fuse two source code to make a new one and generate a proc from that. "
16
16
  email:
17
17
  - adamluzsi@gmail.com
18
18
  executables: []
@@ -27,17 +27,12 @@ files:
27
27
  - VERSION
28
28
  - examples/fun_with_procs_and_methods.rb
29
29
  - examples/simple_test.rb
30
+ - examples/test.rb
30
31
  - files.rb
31
- - lib/source.rb
32
32
  - lib/sourcerer.rb
33
33
  - lib/sourcerer/cache.rb
34
- - lib/sourcerer/file.rb
34
+ - lib/sourcerer/functions.rb
35
35
  - lib/sourcerer/helpers.rb
36
- - lib/sourcerer/method.rb
37
- - lib/sourcerer/proc.rb
38
- - lib/sourcerer/proc_source.rb
39
- - lib/sourcerer/proc_source_body.rb
40
- - lib/sourcerer/proc_source_params.rb
41
36
  - lib/sourcerer/source_code.rb
42
37
  - sourcerer.gemspec
43
38
  - test/test.rb
@@ -51,20 +46,19 @@ require_paths:
51
46
  - lib
52
47
  required_ruby_version: !ruby/object:Gem::Requirement
53
48
  requirements:
54
- - - ! '>='
49
+ - - ">="
55
50
  - !ruby/object:Gem::Version
56
51
  version: '0'
57
52
  required_rubygems_version: !ruby/object:Gem::Requirement
58
53
  requirements:
59
- - - ! '>='
54
+ - - ">="
60
55
  - !ruby/object:Gem::Version
61
56
  version: '0'
62
57
  requirements: []
63
58
  rubyforge_project:
64
- rubygems_version: 2.2.1
59
+ rubygems_version: 2.2.2
65
60
  signing_key:
66
61
  specification_version: 4
67
62
  summary: Simple source extractor Based on standard CRuby
68
63
  test_files:
69
64
  - test/test.rb
70
- has_rdoc:
data/lib/source.rb DELETED
@@ -1,4 +0,0 @@
1
- #encoding: UTF-8
2
- module Sourcerer
3
- require 'sourcerer'
4
- end
@@ -1,27 +0,0 @@
1
- class SourceFile < File
2
-
3
- # start read the file object on each line
4
- # optionable an integer value to start read line at
5
- # compatible with mac (i am linux user, so not tested)
6
- def each_line_from(start_at=1,&block)
7
- unless [Integer,Fixnum,Bignum].include?(start_at.class)
8
- raise ArgumentError, "invalid line index"
9
- end
10
- begin
11
-
12
- line_num= 1
13
- text= self.read
14
- text.gsub!(/\r\n?/, "\n")
15
- text.gsub!(';',"\n")
16
- text.gsub!(/\bdo\b/,'{')
17
- text.gsub!(/\bend\b/,'}')
18
- text.each_line do |*line|
19
- if line_num >= start_at
20
- block.call *line
21
- end
22
- line_num += 1
23
- end
24
- end
25
- end
26
-
27
- end
@@ -1,23 +0,0 @@
1
- module Sourcerer
2
- module MethodDSL
3
-
4
- # creatue a raw eval-able process source, so you can set
5
- # the right bindings using the .to_proc call from String methods
6
- def source
7
-
8
- return_string= Sourcerer.raw_source(self).convert_from_method!
9
- return return_string
10
-
11
- end
12
- alias :source_string :source
13
-
14
- end
15
- end
16
-
17
- class Method
18
- include Sourcerer::MethodDSL
19
- end
20
-
21
- class UnboundMethod
22
- include Sourcerer::MethodDSL
23
- end
@@ -1,13 +0,0 @@
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
- def source # trim_comment= true
6
-
7
- return_string= Sourcerer.raw_source(self).convert_from_proc!
8
- return return_string
9
-
10
- end
11
- alias :source_string :source
12
-
13
- end
@@ -1,102 +0,0 @@
1
- module Sourcerer
2
- class ProcSource < SourceCode
3
-
4
- class << self
5
- attr_accessor :source_cache
6
- attr_accessor :eval_keys
7
- end
8
- self.source_cache= Hash.new
9
- self.eval_keys= Array.new
10
-
11
- def self.build(source_code_to_be_wrappered,*params_obj_array)
12
- self.new(source_code_to_be_wrappered).wrapper_around!(*params_obj_array)
13
- end
14
-
15
- def wrapper_around!(*params)
16
- if !params.nil?
17
- params= params.join(',')
18
- params.prepend(' |')
19
- params.concat('|')
20
- end
21
- self.prepend("Proc.new{#{params}\n")
22
- self.concat("\n}")
23
- end
24
-
25
- # create process object from valid process string
26
- def to_proc(binding=nil)
27
- begin
28
-
29
- unless self.split("\n")[0].include?('Proc.new')
30
- raise ArgumentError, "string obj is not a valid process source"
31
- end
32
-
33
- return_proc= nil
34
- if binding.nil?
35
- return_proc= eval(self)
36
- else
37
- return_proc= eval(self,binding)
38
- end
39
-
40
- if ProcSource.eval_keys.count > 1000
41
- ProcSource.eval_keys.each {|e| ProcSource.source_cache.delete(e) }
42
- ProcSource.eval_keys.clear
43
- end
44
- ProcSource.source_cache[return_proc.inspect]= self
45
- ProcSource.eval_keys.push return_proc.inspect
46
-
47
- return return_proc
48
- end
49
- end
50
-
51
- def body
52
-
53
- body= ProcSourceBody.new(self.dup.to_s)
54
- body.sub!(/.*Proc\.new *{ *(\|.*\|)?/,String.new)
55
- body.gsub!(/^$\n/, String.new)
56
- body.sub!(/\s*}\s*$/,String.new)
57
-
58
- return body
59
- end
60
-
61
- def params
62
- #SystemStackError
63
- params= self.dup
64
- params.sub!(params.split("\n")[0].scan(/\s*Proc.new\s*{/)[0],String.new)
65
- params.sub!(' ','')
66
- params= params.split("\n")[0].scan(/^\s*{?\s*(.*)/)[0][0].gsub!('|','')
67
- if params.nil?
68
- return nil
69
- end
70
- return ProcSourceParams[*params.split(',')]
71
- end
72
-
73
- def parameters
74
-
75
- return_array= Array.new
76
- params= self.params
77
- params.each do |one_raw_parameter|
78
- case true
79
- when one_raw_parameter.include?('=')
80
- begin
81
- return_array.push Array.new.push(:opt).push(
82
- one_raw_parameter.split('=')[0].to_sym)
83
- end
84
- when one_raw_parameter[0] == '*'
85
- begin
86
- one_raw_parameter[0]= ''
87
- return_array.push Array.new.push(:rest).push(
88
- one_raw_parameter.to_sym)
89
- end
90
- else
91
- begin
92
- return_array.push Array.new.push(:req).push(
93
- one_raw_parameter.to_sym)
94
- end
95
- end
96
- end
97
-
98
- return return_array
99
- end
100
-
101
- end
102
- end
@@ -1,14 +0,0 @@
1
- module Sourcerer
2
- class ProcSourceBody < SourceCode
3
-
4
- def +(oth_str)
5
- ProcSourceBody.new(self.dup.concat("\n").concat(oth_str.to_s))
6
- end
7
-
8
- # build proc source
9
- def build(*params)
10
- ProcSource.build(self.to_s,*params)
11
- end
12
-
13
- end
14
- end
@@ -1,37 +0,0 @@
1
- module Sourcerer
2
- class ProcSourceParams < Array
3
-
4
- # merge two proc params obj
5
- # if multiple rest obj found
6
- # it will remove and make an *args obj as last element
7
- # if they are not equal ,
8
- # else it makes it the last element only
9
- def +(oth_ary)
10
-
11
- merged_array= (Array[*self]+Array[*oth_ary])
12
-
13
- rest_state= nil
14
- rest_elements= Array.new
15
- merged_array.dup.each do |element|
16
- if element.include? '*'
17
- merged_array.delete(element)
18
- rest_state ||= true
19
- rest_elements.push(element)
20
- end
21
- end
22
-
23
- rest_elements.uniq!
24
- if rest_elements.count == 1 && !rest_elements.empty?
25
- merged_array.push(rest_elements[0])
26
- rest_state= nil
27
- end
28
-
29
- unless rest_state.nil?
30
- merged_array.push('*args')
31
- end
32
-
33
- return ProcSourceParams[*merged_array]
34
- end
35
-
36
- end
37
- end