method_source-expressions 0.0.1
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/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +25 -0
- data/Rakefile +1 -0
- data/lib/method_source-expressions.rb +12 -0
- data/lib/method_source-expressions/expression.rb +39 -0
- data/lib/method_source-expressions/expression_collection.rb +36 -0
- data/lib/method_source-expressions/proc_extensions.rb +40 -0
- data/lib/method_source-expressions/version.rb +5 -0
- data/method_source-expressions.gemspec +21 -0
- data/spec/method_source-expressions/expression_collection_spec.rb +28 -0
- data/spec/method_source-expressions/expression_spec.rb +20 -0
- data/spec/method_source-expressions/proc_extensions_spec.rb +27 -0
- data/spec/spec_helper.rb +8 -0
- metadata +84 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Hashrocket Workstation
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# MethodSource::Expressions
|
2
|
+
|
3
|
+
Small extension to [method_source](https://github.com/banister/method_source). It adds `#expressions` to the Proc class. This method returns a hash of expressions defined inside a given Proc/Lambda. Each expression object knows its range of line numbers, its source, and its source_location.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'method_source-expressions'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install method_source-expressions
|
18
|
+
|
19
|
+
## Contributing
|
20
|
+
|
21
|
+
1. Fork it
|
22
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
23
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
24
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
25
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "method_source"
|
2
|
+
require "method_source-expressions/version"
|
3
|
+
require "method_source-expressions/expression"
|
4
|
+
require "method_source-expressions/proc_extensions"
|
5
|
+
require "method_source-expressions/expression_collection"
|
6
|
+
|
7
|
+
module MethodSource::Expressions
|
8
|
+
end
|
9
|
+
|
10
|
+
class Proc
|
11
|
+
include MethodSource::Expressions::ProcExtensions
|
12
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module MethodSource::Expressions
|
2
|
+
class Expression
|
3
|
+
attr_reader :source, :location
|
4
|
+
|
5
|
+
def initialize(options)
|
6
|
+
@source = options.fetch(:source)
|
7
|
+
@line_number = options.fetch(:line_number)
|
8
|
+
@location = options.fetch(:location)
|
9
|
+
end
|
10
|
+
|
11
|
+
def range
|
12
|
+
is_multi_line? ? multi_line_expression_range : line_number..line_number
|
13
|
+
end
|
14
|
+
|
15
|
+
def within(other_expression)
|
16
|
+
range.all? {|i| other_expression.range.include?(i) } &&
|
17
|
+
range != other_expression.range
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def line_number
|
23
|
+
@line_number.to_i
|
24
|
+
end
|
25
|
+
|
26
|
+
def is_multi_line?
|
27
|
+
expression_length > 1
|
28
|
+
end
|
29
|
+
|
30
|
+
def multi_line_expression_range
|
31
|
+
line_number..(expression_length + line_number - 1)
|
32
|
+
end
|
33
|
+
|
34
|
+
def expression_length
|
35
|
+
source.split("\n").length
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module MethodSource::Expressions
|
2
|
+
class ExpressionCollection
|
3
|
+
attr_reader :expressions
|
4
|
+
|
5
|
+
def initialize(expressions)
|
6
|
+
@top_node, *@expressions = expressions
|
7
|
+
end
|
8
|
+
|
9
|
+
def direct_descendents(collection=expressions)
|
10
|
+
child_expressions.map{ |child| expression_or_collection(child) }
|
11
|
+
end
|
12
|
+
|
13
|
+
def source
|
14
|
+
@top_node.source
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
attr_reader :top_node
|
19
|
+
|
20
|
+
def child_expressions
|
21
|
+
expressions.reject do |outer|
|
22
|
+
expressions.any? {|inner| outer.within(inner) }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def expression_or_collection(expression)
|
27
|
+
sub_children = expressions.select{|e| e.within(expression) }
|
28
|
+
|
29
|
+
if sub_children.length > 0
|
30
|
+
self.class.new([expression, *sub_children])
|
31
|
+
else
|
32
|
+
expression
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module MethodSource::Expressions
|
2
|
+
module ProcExtensions
|
3
|
+
def expressions
|
4
|
+
ec = MethodSource::Expressions::ExpressionCollection.new(collection)
|
5
|
+
ec.direct_descendents
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def collection
|
11
|
+
source_range.map do |line_number|
|
12
|
+
expression = create_expression(line_number)
|
13
|
+
expression if expression.source
|
14
|
+
end.compact
|
15
|
+
end
|
16
|
+
|
17
|
+
def source_range
|
18
|
+
starting_line..(starting_line + length)
|
19
|
+
end
|
20
|
+
|
21
|
+
def create_expression(line_number)
|
22
|
+
expression = soft_extract(line_number)
|
23
|
+
MethodSource::Expressions::Expression.new(source: expression, line_number: line_number, location: source_location.first)
|
24
|
+
end
|
25
|
+
|
26
|
+
def soft_extract(line_number)
|
27
|
+
MethodSource.expression_at(File.read(source_location.first),line_number).strip
|
28
|
+
rescue SyntaxError
|
29
|
+
false
|
30
|
+
end
|
31
|
+
|
32
|
+
def length
|
33
|
+
@length ||= source.split("\n").length
|
34
|
+
end
|
35
|
+
|
36
|
+
def starting_line
|
37
|
+
source_location[1]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'method_source-expressions/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "method_source-expressions"
|
8
|
+
gem.version = MethodSource::Expressions::VERSION
|
9
|
+
gem.authors = ["Jonathan Jackson"]
|
10
|
+
gem.email = ["dev@hashrocket.com"]
|
11
|
+
gem.description = %q{Ruby doesn't like you arbitrarily deciding when to execute expressions. Take control with this gem. It's a simple way to mix in additional behavior into Proc that will allow you to make custom DSL's that will change how ruby deals with expressions.}
|
12
|
+
gem.summary = %q{Expression parsing addition to the method_source gem}
|
13
|
+
gem.homepage = "https://github.com/rondale-sc/method_source-expressions"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency "method_source"
|
21
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper.rb'
|
2
|
+
|
3
|
+
describe MethodSource::Expressions::ExpressionCollection do
|
4
|
+
let(:blk) do
|
5
|
+
Proc.new do
|
6
|
+
puts "something"
|
7
|
+
Proc.new do
|
8
|
+
puts "otherthing"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "expressions_at_nesting" do
|
14
|
+
it "first item is" do
|
15
|
+
expect(blk.expressions.first.source).to eql("puts \"something\"")
|
16
|
+
end
|
17
|
+
|
18
|
+
it "last item is" do
|
19
|
+
expected_value ="Proc.new do\n puts \"otherthing\"\n end"
|
20
|
+
expect(blk.expressions.last.source).to eql(expected_value)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "can be used on its returned items" do
|
24
|
+
last_item = blk.expressions.last
|
25
|
+
expect(last_item.expressions.first.source).to eql('puts "otherthing"')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MethodSource::Expressions::Expression do
|
4
|
+
let(:options) { {source: "expr", line_number: "1", location: "/somepath"} }
|
5
|
+
subject(:expression) { described_class.new(options) }
|
6
|
+
|
7
|
+
it "exposes its source" do
|
8
|
+
expect(expression.source).to eq("expr")
|
9
|
+
end
|
10
|
+
|
11
|
+
it "exposes its line_number" do
|
12
|
+
expect(expression.location).to eq("/somepath")
|
13
|
+
end
|
14
|
+
|
15
|
+
context "#range" do
|
16
|
+
it "knows its range representation" do
|
17
|
+
expect(expression.range).to eq(1..1)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe MethodSource::Expressions::ProcExtensions do
|
4
|
+
|
5
|
+
let(:blk) do
|
6
|
+
Proc.new do
|
7
|
+
puts "something"
|
8
|
+
Proc.new do
|
9
|
+
puts "otherthing"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#expressions" do
|
15
|
+
it "returns a hash with values of expression objects" do
|
16
|
+
result = blk.expressions.all? do |v|
|
17
|
+
v.class == MethodSource::Expressions::Expression ||
|
18
|
+
v.class == MethodSource::Expressions::ExpressionCollection
|
19
|
+
end
|
20
|
+
expect(result).to be_true
|
21
|
+
end
|
22
|
+
|
23
|
+
it "returns expressions" do
|
24
|
+
expect(blk.expressions.first.source).to eq("puts \"something\"")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: method_source-expressions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jonathan Jackson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-08-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: method_source
|
16
|
+
prerelease: false
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
none: false
|
23
|
+
type: :runtime
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ! '>='
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '0'
|
29
|
+
none: false
|
30
|
+
description: Ruby doesn't like you arbitrarily deciding when to execute expressions. Take
|
31
|
+
control with this gem. It's a simple way to mix in additional behavior into Proc
|
32
|
+
that will allow you to make custom DSL's that will change how ruby deals with expressions.
|
33
|
+
email:
|
34
|
+
- dev@hashrocket.com
|
35
|
+
executables: []
|
36
|
+
extensions: []
|
37
|
+
extra_rdoc_files: []
|
38
|
+
files:
|
39
|
+
- .gitignore
|
40
|
+
- .rspec
|
41
|
+
- Gemfile
|
42
|
+
- LICENSE.txt
|
43
|
+
- README.md
|
44
|
+
- Rakefile
|
45
|
+
- lib/method_source-expressions.rb
|
46
|
+
- lib/method_source-expressions/expression.rb
|
47
|
+
- lib/method_source-expressions/expression_collection.rb
|
48
|
+
- lib/method_source-expressions/proc_extensions.rb
|
49
|
+
- lib/method_source-expressions/version.rb
|
50
|
+
- method_source-expressions.gemspec
|
51
|
+
- spec/method_source-expressions/expression_collection_spec.rb
|
52
|
+
- spec/method_source-expressions/expression_spec.rb
|
53
|
+
- spec/method_source-expressions/proc_extensions_spec.rb
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
homepage: https://github.com/rondale-sc/method_source-expressions
|
56
|
+
licenses: []
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
none: false
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
none: false
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.8.24
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: Expression parsing addition to the method_source gem
|
79
|
+
test_files:
|
80
|
+
- spec/method_source-expressions/expression_collection_spec.rb
|
81
|
+
- spec/method_source-expressions/expression_spec.rb
|
82
|
+
- spec/method_source-expressions/proc_extensions_spec.rb
|
83
|
+
- spec/spec_helper.rb
|
84
|
+
has_rdoc:
|