import_as 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 59817ca2a75b87a2d6adfa4d5899465b37436e009c33707a63822436d4a7faec
4
+ data.tar.gz: cb08fe7083bffe484ca3537bdee10ab4ea251fcb3d343f66ab8e2ce7efa10fa0
5
+ SHA512:
6
+ metadata.gz: 1a8ffd7b76832592b47bce9c6a1424ca8b60d6bee71ab448c24c0991e8e50da04d6752942d3c9c74e56e387eed65c426427fb8d2ede23e22176e07be1b9dfeb5
7
+ data.tar.gz: f24389ad132cd9786025ebe56c9dc636b88a121b3e5619831a25522fadc59be9a922961ee811122d50b1d9ba7c8d716a9d6dc201a6803f05698d3dbade4ea04e
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.0
7
+ before_install: gem install bundler -v 2.0.0.pre.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in import_as.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ import_as (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.8.0)
12
+ rspec-core (~> 3.8.0)
13
+ rspec-expectations (~> 3.8.0)
14
+ rspec-mocks (~> 3.8.0)
15
+ rspec-core (3.8.0)
16
+ rspec-support (~> 3.8.0)
17
+ rspec-expectations (3.8.2)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.8.0)
20
+ rspec-mocks (3.8.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-support (3.8.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 2.0.a)
30
+ import_as!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 2.0.0.pre.2
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # ImportAs
2
+
3
+ Add DSL: import { C as C2 }.from "./c.rb"
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ # Refinements version
11
+ gem "import_as"
12
+
13
+ # Core extension version
14
+
15
+ gem "import_as", require: "import_as/core_ext"
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install import_as
25
+
26
+ ## Usage
27
+
28
+ # c.rb
29
+ class C
30
+ def hi
31
+ puts :hi
32
+ end
33
+ end
34
+
35
+ # main.rb
36
+ require "import_as/core_ext"
37
+
38
+ import { C as C2 }.from "./c.rb"
39
+
40
+ C2.new.hi
41
+ # hi
42
+
43
+ ## Development
44
+
45
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
46
+
47
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hanachin/import_as.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "import_as"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/import_as.gemspec ADDED
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "import_as/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "import_as"
8
+ spec.version = ImportAs::VERSION
9
+ spec.authors = ["Seiei Miyagi"]
10
+ spec.email = ["hanachin@gmail.com"]
11
+
12
+ spec.summary = %q{Add DSL: import { C as C2 }.from "./c.rb"}
13
+ spec.homepage = "https://github.com/hanachin/import_as"
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 2.0.a"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
@@ -0,0 +1,3 @@
1
+ require "import_as"
2
+
3
+ extend ImportAs
@@ -0,0 +1,3 @@
1
+ module ImportAs
2
+ VERSION = "0.1.0"
3
+ end
data/lib/import_as.rb ADDED
@@ -0,0 +1,126 @@
1
+ require "import_as/version"
2
+ require "ripper"
3
+ require "tempfile"
4
+
5
+ module ImportAs
6
+ class Error < StandardError; end
7
+
8
+ class ConstRewriter
9
+ def initialize(const_hash)
10
+ @rewriter = Class.new(Ripper::Filter) {
11
+ private
12
+
13
+ define_method(:on_const) do |t, f|
14
+ f << const_hash.fetch(t.to_sym, t).to_s
15
+ end
16
+
17
+ def on_default(_, t, f)
18
+ f << t
19
+ end
20
+ }
21
+ end
22
+
23
+ def rewrite(ruby)
24
+ @rewriter.new(ruby).parse('')
25
+ end
26
+ end
27
+
28
+ class DSL
29
+ def initialize(&block)
30
+ @as = block
31
+ end
32
+
33
+ def from(path)
34
+ Tempfile.open(["import_as", ".rb"]) do |tf|
35
+ new_source = rewrite(File.read(path))
36
+
37
+ tf.write(new_source)
38
+ tf.flush
39
+ tf.close
40
+
41
+ load tf.path
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ using Module.new {
48
+ refine(Symbol) do
49
+ def const_id?
50
+ id = self
51
+ Module.new.module_eval { const_set(id, true) rescue false }
52
+ end
53
+ end
54
+
55
+ refine(RubyVM::AbstractSyntaxTree::Node) do
56
+ def array?
57
+ type == "NODE_ARRAY"
58
+ end
59
+
60
+ def const?
61
+ type == "NODE_CONST"
62
+ end
63
+
64
+ def fcall?
65
+ type == "NODE_FCALL"
66
+ end
67
+
68
+ def scope?
69
+ type == "NODE_SCOPE"
70
+ end
71
+
72
+ def const_pair
73
+ original_const_id, args = children
74
+
75
+ raise Error unless original_const_id.const_id?
76
+ raise Error unless args.array?
77
+
78
+ head, *rest = args.children
79
+
80
+ raise Error unless rest.size == 1 && rest[0].nil?
81
+ raise Error unless head.fcall?
82
+
83
+ as, args2 = head.children
84
+
85
+ raise Error unless as == :as
86
+
87
+ head2, *rest2 = args2.children
88
+
89
+ raise Error unless rest.size == 1 && rest[0].nil?
90
+ raise Error unless head2.const?
91
+ raise Error unless head2.children.size == 1
92
+
93
+ new_const_id = head2.children[0]
94
+
95
+ raise Error unless new_const_id.const_id?
96
+
97
+ [original_const_id, new_const_id]
98
+ end
99
+ end
100
+ }
101
+
102
+ def const_hash
103
+ root = RubyVM::AbstractSyntaxTree.of(@as)
104
+
105
+ raise Error unless root.scope?
106
+
107
+ _tbl, _args, body = root.children
108
+
109
+ raise Error unless body.fcall?
110
+
111
+ [body.const_pair].to_h
112
+ end
113
+
114
+ def rewrite(rb)
115
+ ConstRewriter.new(const_hash).rewrite(rb)
116
+ end
117
+ end
118
+
119
+ def import(&block)
120
+ DSL.new(&block)
121
+ end
122
+
123
+ refine(Object) do
124
+ include ImportAs
125
+ end
126
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: import_as
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Seiei Miyagi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.a
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.0.a
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - hanachin@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - import_as.gemspec
72
+ - lib/import_as.rb
73
+ - lib/import_as/core_ext.rb
74
+ - lib/import_as/version.rb
75
+ homepage: https://github.com/hanachin/import_as
76
+ licenses: []
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.7.8
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: 'Add DSL: import { C as C2 }.from "./c.rb"'
98
+ test_files: []