plock 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d5a704c97cf4dfbc0119fc10c4df6448bdae7c1b
4
+ data.tar.gz: 3c7879a0acf310be0dc27b467f1da2f4a0dd39f0
5
+ SHA512:
6
+ metadata.gz: 628c348bcd5506c7203f75ef4ce30fab2862aa79744fd52c8eb740e74cb91f6ead05c67d07b4f2cc8cb7216eff5bc5568f453570b2b5ef09a8cb714c0254b84a
7
+ data.tar.gz: 6db35537b4c6152b32a7a9aace878b74d85cfb1c383f291cf25272d8b503a70cd5406936dc9b0314de7c2d1c64444739add6bbd15c7ea9b2dea253114cac21ea
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in plock.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Yamamoto Yuji
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,30 @@
1
+ # Plock
2
+
3
+ p + block = plock. 'p { 1 + 1 }` prints "(1 + 1) #=> 2". That's all.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'plock'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install plock
18
+
19
+ ## Known Bugs
20
+ **Important Note:** Plock depends on [sourcify](https://github.com/ngty/sourcify) (so far), it has a big restriction:
21
+
22
+ - You can't use on REPLs such as pry, irb and so on: you have to use p/pp in a source code saved on the disk.
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module Plock
2
+ VERSION = "0.0.1"
3
+ end
data/lib/plock.rb ADDED
@@ -0,0 +1,78 @@
1
+ require 'sourcify'
2
+ require 'pp'
3
+
4
+ require "plock/version"
5
+
6
+ module Plock
7
+ module Format
8
+ PERCENT_B = '%b'.freeze
9
+ PERCENT_R = '%r'.freeze
10
+ DEFAULT_FORMAT = "#{PERCENT_B} #=> #{PERCENT_R}".freeze
11
+ end
12
+
13
+ class << self
14
+ attr_writer :output_format
15
+ def output_format
16
+ @output_format ||= self::Format::DEFAULT_FORMAT
17
+ end
18
+
19
+ def inspect_block( attached = :inspect_block, &block )
20
+ result = block.call
21
+ [ block.to_source( attached_to: attached, strip_enclosure: true ), result ]
22
+ end
23
+
24
+ def print_block_with formatter_method, attached, *args, &block
25
+ returned = []
26
+ returned.concat args
27
+
28
+ block_source, block_result = Plock.inspect_block( attached, &block )
29
+
30
+ puts self.__send__( formatter_method, block_source, block_result )
31
+
32
+ returned << block_result
33
+ returned.length > 1 ? returned : returned.first
34
+ end
35
+
36
+ def format_with block_source, block_result, inspect_method
37
+ result = self.output_format.dup
38
+ result.sub! self::Format::PERCENT_B, block_source
39
+ result.sub! self::Format::PERCENT_R, block_result.__send__( inspect_method )
40
+ return result
41
+ end
42
+
43
+ def format block_source, block_result
44
+ self.format_with block_source, block_result, :inspect
45
+ end
46
+
47
+ if Object.public_method_defined? :pretty_inspect
48
+ def pretty_format block_source, block_result
49
+ self.format_with block_source, block_result, :pretty_inspect
50
+ end
51
+ end
52
+ end
53
+
54
+ end
55
+
56
+ module Kernel # reopen
57
+ alias p_without_plock p
58
+ def p_with_plock *args, &block
59
+ if block_given?
60
+ Plock.print_block_with :format, :p, *args, &block
61
+ else
62
+ p_without_plock( *args )
63
+ end
64
+ end
65
+ alias p p_with_plock
66
+
67
+ if Kernel.private_method_defined? :pp
68
+ alias pp_without_plock pp
69
+ def pp_with_plock
70
+ if block_given?
71
+ Plock.print_block_with :pretty_format, :pp, *args, &block
72
+ else
73
+ pp_without_plock( *args )
74
+ end
75
+ end
76
+ alias pp pp_with_plock
77
+ end
78
+ end
data/plock.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'plock/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "plock"
8
+ spec.version = Plock::VERSION
9
+ spec.authors = ["Yamamoto Yuji"]
10
+ spec.email = ["whosekiteneverfly@gmail.com"]
11
+ spec.description = %q{'p { 1 + 1 }` prints "1 + 1 #=> 2". That's all.}
12
+ spec.summary = %q{'p { 1 + 1 }` prints "1 + 1 #=> 2". That's all.}
13
+ spec.homepage = "https://github.com/igrep/plock"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "sourcify"
25
+ end
@@ -0,0 +1,46 @@
1
+ require 'plock'
2
+ require 'stringio'
3
+
4
+ describe Plock do
5
+ describe '.inspect_block ' do
6
+ subject do
7
+ a = 1
8
+ described_class.inspect_block { a + 1 }
9
+ end
10
+ it { should eq ['(a + 1)', 2] }
11
+ end
12
+ describe '.format' do
13
+ context 'when Plock.output_format = "%b #=> %r"' do
14
+ before { described_class.output_format = "%b #=> %r" }
15
+ subject { described_class.format '(a + 1)', 2 }
16
+ it { should eq '(a + 1) #=> 2' }
17
+ it 'never changes the output_format desructively' do
18
+ described_class.output_format = '%b and %r SHOULD NOT BE CHANGED'
19
+ described_class.format 'block', 'result'
20
+ described_class.output_format.should eq '%b and %r SHOULD NOT BE CHANGED'
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ RSpec::Matchers.define :include_when_ignoring_space_difference do|expected|
27
+ match do|actual|
28
+ expected.gsub( /\s+/, '' ).include? actual.gsub( /\s+/, '' )
29
+ end
30
+ end
31
+
32
+ describe Kernel do
33
+ describe '#p' do
34
+ before( :all ) { $stdout = StringIO.new '', 'wb' } # replaces the stdin for testing the output of p
35
+ context 'when Plock.output_format = "%b #=> %r"' do
36
+ before( :all ) { Plock.output_format = "%b #=> %r" }
37
+ let( :no_percent ){ Plock.output_format.gsub( /%[br]/, '' ) }
38
+ subject { p { 1 + 1 } }
39
+ it { should be 2 }
40
+ it 'prints out the block and its result onto stdin' do
41
+ $stdout.string.should include_when_ignoring_space_difference "(1 + 1) #{no_percent} 2"
42
+ end
43
+ end
44
+ after( :all ) { $stdout = STDOUT } # restore default stdin
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: plock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yamamoto Yuji
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-06 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: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sourcify
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: '''p { 1 + 1 }` prints "1 + 1 #=> 2". That''s all.'
70
+ email:
71
+ - whosekiteneverfly@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/plock.rb
82
+ - lib/plock/version.rb
83
+ - plock.gemspec
84
+ - spec/plock_spec.rb
85
+ homepage: https://github.com/igrep/plock
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.0.0
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: '''p { 1 + 1 }` prints "1 + 1 #=> 2". That''s all.'
109
+ test_files:
110
+ - spec/plock_spec.rb