pry-toys 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 900f8752de46194dc766690e9ce0d2b41b04d101
4
+ data.tar.gz: 4abd02f2b95396f826c09f7a1fbe55fcadacda46
5
+ SHA512:
6
+ metadata.gz: 1c5f19d59ff40ad58e27058d43130992362a5498185d8afebe3d37582179695bed1f4a3fd3cc11f1a3e1cec7ace51d95cbdb6826c344e2112ca779d92408739b
7
+ data.tar.gz: 72eadc78b51397de1544d3681dd7b64cfd842932ecd450b7204dedadd35dfbca1eadd1999a1754de48ac1c4d4ec09ec0e09922a51746ced3bb992cf5f6297009
@@ -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/.pryrc ADDED
@@ -0,0 +1,7 @@
1
+ Pry.config.hooks.add_hook(:before_session, :load_toys) do
2
+ require File.join(File.dirname(__FILE__), 'lib/pry-toys/toy.rb')
3
+ require File.join(File.dirname(__FILE__), 'lib/pry-toys/array.rb')
4
+ require File.join(File.dirname(__FILE__), 'lib/pry-toys/hash.rb')
5
+ require File.join(File.dirname(__FILE__), 'lib/pry-toys/string.rb')
6
+ end
7
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pry-toys.gemspec
4
+ gemspec
@@ -0,0 +1,6 @@
1
+ # More info at https://github.com/guard/guard#readme
2
+
3
+ guard :rspec do
4
+ watch(%r{lib/pry-toys/(.+).rb}) { |m| "spec/lib/pry-toys/#{m[1]}_spec.rb" }
5
+ watch(%r{spec/lib/pry-toys/(.+)_spec.rb}) { |m| "spec/lib/pry-toys/#{m[1]}_spec.rb" }
6
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 ariabov
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.
@@ -0,0 +1,96 @@
1
+ # Pry-Toys
2
+
3
+ The idea was to simplify the process of creating Ruby objects to play around with __pry__. Now, creating Hash object with 100 keys inside __pry__ is as simple as `Hash.toy(100)`. The idea was inspired by https://gist.github.com/lucapette/807492
4
+
5
+ ## Installation
6
+
7
+ You can either install gem individually or you can add it to your Gemfile. To add the gem directly, type in the following in your terminal line:
8
+
9
+ gem install pry-toys
10
+
11
+ Alternatively, if your project has a `Gemfile`, you can add __pry-toys__ there:
12
+
13
+ gem 'pry-toys'
14
+
15
+ Or if you would like to add the gem to specific group, you can do the following:
16
+
17
+ group :development, :test do
18
+ gem 'pry-toys'
19
+ end
20
+
21
+ This will ensure that the gem is only loaded in `development` or `test` environments, but not in `production`. Do not forget to run
22
+
23
+ bundle install
24
+
25
+ after adding the __pry-toys__ to Gemfile.
26
+
27
+ To make sure __pry-toys__ has been successfully added to __pry__, run the following in your terminal:
28
+
29
+ pry --installed-plugins
30
+
31
+ If __pry-toys__ gem installed correctly, you should see `toys` as one of the plugins in the list.
32
+
33
+ ## Usage
34
+
35
+ Here are some of the things you can do with __pry-toys__:
36
+
37
+ #### Arrays
38
+
39
+ With only size set
40
+
41
+ String.toy(3) # => [1, 2, 3]
42
+
43
+ With size & block
44
+
45
+ String.toy(3) {|i| i + 3} # => [3, 6, 9]
46
+
47
+ With size & one of the pre-determined objects, such as Integer (default), Float, String, Time, Date set
48
+
49
+ String.toy(3, Float) # => [1.0, 2.0, 3.0]
50
+ String.toy(3, String) # => ['tt', 'uuuu', 'cc']
51
+ String.toy(3, Time) # => [2014-02-22 08:49:49 -0800,
52
+ 2014-02-23 08:49:49 -0800,
53
+ 2014-02-24 08:49:49 -0800]
54
+ String.toy(3, Date) # => [#<Date: 2014-02-22 ((2456711j,0s,0n),+0s,2299161j)>,
55
+ #<Date: 2014-02-23 ((2456712j,0s,0n),+0s,2299161j)>,
56
+ #<Date: 2014-02-24 ((2456713j,0s,0n),+0s,2299161j)>]
57
+
58
+ With size & an unknown object (that responds to new)
59
+
60
+ String.toy(3, Object) # => [#<Object:0x007fa36e9b9128>,
61
+ #<Object:0x007fa36e9b9100>,
62
+ #<Object:0x007fa36e9b90d8>]
63
+
64
+ #### Hashes
65
+
66
+ With size set, you can make Hash as small or as big as you want:
67
+
68
+ Hash.toy(3) # => {a: 1, b: 2, c: 3}
69
+
70
+ Or as large as 100000 (or larger)
71
+
72
+ h = Hash.toy(100000)
73
+ h.keys.last # => "eqxd"
74
+ h.values.last # => 100000
75
+ h # => {a: 1, ...., eqxd: 100000}
76
+
77
+ #### Strings
78
+
79
+ Creates a string with certain number of words. In this case, words are random collection of letters (not actual words).
80
+
81
+ String.toy(3) # => "mmmmmmmm xxxx hhhhhh"
82
+
83
+
84
+ ## Contributing
85
+
86
+ 1) Form the project
87
+
88
+ 2) Make changes
89
+
90
+ 3) Test changes using the following task:
91
+
92
+ rake spec
93
+
94
+ 4) Commit changes. Please do not change `pry-toys.gemspec` on the version you are planning to submit for the pull request.
95
+
96
+ 5) Send a pull request
@@ -0,0 +1,4 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,4 @@
1
+ require 'pry-toys/toy'
2
+ require 'pry-toys/array'
3
+ require 'pry-toys/hash'
4
+ require 'pry-toys/string'
@@ -0,0 +1,63 @@
1
+ ##
2
+ # Reopening Array class to add toy method
3
+ Array.class_eval do
4
+
5
+ ##
6
+ # Creates an array of objects based on the input
7
+ #
8
+ # For example, it will work with the following inputs:
9
+ #
10
+ # == Size
11
+ #
12
+ # String.toy(3) # => [1, 2, 3]
13
+ #
14
+ # == Size & block
15
+ #
16
+ # String.toy(3) {|i| i + 3} # => [3, 6, 9]
17
+ #
18
+ # == Size & one of the pre-determined objects, such as
19
+ # Integer (default), Float, String, Time, Date
20
+ #
21
+ # String.toy(3, Float) # => [1.0, 2.0, 3.0]
22
+ # String.toy(3, String) # => ['tt', 'uuuu', 'cc']
23
+ # String.toy(3, Time) # => [2014-02-22 08:49:49 -0800,
24
+ # 2014-02-23 08:49:49 -0800,
25
+ # 2014-02-24 08:49:49 -0800]
26
+ # String.toy(3, Date) # => [#<Date: 2014-02-22 ((2456711j,0s,0n),+0s,2299161j)>,
27
+ # #<Date: 2014-02-23 ((2456712j,0s,0n),+0s,2299161j)>,
28
+ # #<Date: 2014-02-24 ((2456713j,0s,0n),+0s,2299161j)>]
29
+ #
30
+ # == Size & an unknown object (that responds to new)
31
+ #
32
+ # String.toy(3, Object) # => [#<Object:0x007fa36e9b9128>,
33
+ # #<Object:0x007fa36e9b9100>,
34
+ # #<Object:0x007fa36e9b90d8>]
35
+
36
+
37
+ def self.toy(n=10, type=Integer, &block)
38
+ return Array.new(n,&block) if block_given?
39
+ case type.to_s
40
+ when 'Integer'
41
+ Array.new(n) {|i| i+1}
42
+ when 'Float'
43
+ Array.new(n) {|i| i+1.0}
44
+ when 'String'
45
+ words = String.toy(n).split
46
+ Array.new(n) {|i| words[i]}
47
+ when 'Time'
48
+ time_now = Time.now
49
+ Array.new(n) {|i| time_now + (i * (60 * 60 * 24)) }
50
+ when 'Date'
51
+ date_now = Date.today
52
+ Array.new(n) {|i| date_now + (i * 1) }
53
+ else
54
+ Array.new(n) do
55
+ type.send(:new) rescue
56
+ raise NoNewMethodError, 'Please provide Object that responds to `new` call'
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+
63
+
@@ -0,0 +1,29 @@
1
+ ##
2
+ # Reopening Array class to add toy method
3
+ Hash.class_eval do
4
+
5
+ ##
6
+ # Creates a hash with letters of alphabet as keys
7
+ #
8
+ # For example, it can be as small as 3
9
+ #
10
+ # Hash.toy(3) # => {a: 1, b: 2, c: 3}
11
+ #
12
+ # Or as large as 100000 (or larger)
13
+ #
14
+ # h = Hash.toy(100000)
15
+ # h.keys.last # => "eqxd"
16
+ # h.values.last # => 100000
17
+ # h # => {a: 1, ...., eqxd: 100000}
18
+
19
+ def self.toy(n=10)
20
+ range = toy_size(n).to_a
21
+ Hash[Array.toy(n) {|m| range[m] }.zip(Array.toy(n))]
22
+ end
23
+
24
+ private
25
+
26
+ def self.toy_size(n)
27
+ Toy.hash_range_for(n)
28
+ end
29
+ end
@@ -0,0 +1,24 @@
1
+ ##
2
+ # Reopening String class to add toy method
3
+ String.class_eval do
4
+
5
+ ##
6
+ # Creates a string with certain number of words. In this case,
7
+ # words are random collection of letters (not actual words).
8
+ #
9
+ # For example
10
+ #
11
+ # String.toy(3) # => "mmmmmmmm xxxx hhhhhh"
12
+
13
+ def self.toy(n=25)
14
+ alphabet = ('a'..'z').to_a
15
+ phrase = String.new
16
+ i = 0
17
+ while i < n do
18
+ phrase << ' ' unless i == 0
19
+ phrase << alphabet[rand(alphabet.size)] * (rand(10) + 1)
20
+ i += 1
21
+ end
22
+ phrase
23
+ end
24
+ end
@@ -0,0 +1,16 @@
1
+ class Toy
2
+ class << self
3
+ def hash_range_for(n)
4
+ range = nil
5
+ i = 0
6
+ until range != nil do
7
+ range = ('a'..'z') if n <= 26
8
+ range = ('a'.."z#{'z'*i}") if n < (26 + (26 ** (i+1) )) && !range
9
+ i += 1
10
+ end
11
+ range
12
+ end
13
+ end
14
+ end
15
+
16
+ class NoNewMethodError < Exception; end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "pry-toys"
7
+ spec.version = '0.0.2'
8
+ spec.authors = ["ariabov"]
9
+ spec.email = ["alexariabov@gmail.com"]
10
+ spec.summary = "Toy objects for Pry"
11
+ spec.description = ""
12
+ spec.homepage = "http://github.com/ariabov/pry-toys"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.5'
21
+ spec.add_development_dependency 'rake'
22
+ spec.add_development_dependency 'rspec'
23
+ spec.add_runtime_dependency 'pry'
24
+ end
@@ -0,0 +1,120 @@
1
+ require 'spec_helper'
2
+
3
+ describe Array do
4
+ shared_examples "no nil values" do
5
+ it "does not contain `nil` value" do
6
+ expect(toy.include? nil).to be_false
7
+ end
8
+ end
9
+
10
+ let(:n) { 3 }
11
+
12
+ context "with only `n` size specified" do
13
+ let(:toy) { Array.toy(n) }
14
+
15
+ it 'returns Array' do
16
+ expect(toy.class).to eq Array
17
+ end
18
+
19
+ it 'returns `n` length' do
20
+ expect(toy.size).to eq n
21
+ end
22
+
23
+ it 'defaults to array of Integers' do
24
+ expect(toy).to eq [1, 2, 3]
25
+ end
26
+
27
+ include_examples "no nil values"
28
+ end
29
+
30
+ context "with both `n` size and `type` specified" do
31
+ context "when `type` is" do
32
+ context 'Integer' do
33
+ let(:toy) { Array.toy(n, Integer) }
34
+
35
+ it "returns array of Integer values" do
36
+ expect(toy).to eq [1, 2, 3]
37
+ end
38
+
39
+ include_examples "no nil values"
40
+ end
41
+
42
+ context "Float" do
43
+ let(:toy) { Array.toy(n, Float) }
44
+
45
+ it "returns array of Float values" do
46
+ expect(toy).to eq [1.0, 2.0, 3.0]
47
+ end
48
+
49
+ include_examples "no nil values"
50
+ end
51
+
52
+ context "String" do
53
+ let(:toy) { Array.toy(n, String) }
54
+
55
+ it "returns array of String values" do
56
+ expect(toy.first.class).to eq String
57
+ end
58
+
59
+ it "has `n` size of values in the array" do
60
+ expect(toy.size).to eq n
61
+ end
62
+
63
+ include_examples "no nil values"
64
+ end
65
+
66
+ context "Time" do
67
+ let(:toy) { Array.toy(3, Time) }
68
+ let(:day) { 60 * 60 * 24 }
69
+
70
+ it "returns array of Time objects" do
71
+ expect(toy.first).to be_an_instance_of Time
72
+ end
73
+
74
+ it "returned Time objects are 1 day apart" do
75
+ expect(toy[1]).to eq (toy[0] + day)
76
+ end
77
+ end
78
+
79
+ context "Date" do
80
+ let(:toy) { Array.toy(3, Date) }
81
+ let(:day) { 1 }
82
+
83
+ it "returns array of Date objects" do
84
+ expect(toy.first).to be_an_instance_of Date
85
+ end
86
+
87
+ it "returns Date object 1 day apart" do
88
+ expect(toy[1]).to eq (toy[0] + day)
89
+ end
90
+ end
91
+
92
+ context "Not predetermined class" do
93
+ it "works with any object" do
94
+ expect(Array.toy(3, BasicObject)).to_not be_empty
95
+ expect(Array.toy(3, Object)).to_not be_empty
96
+ end
97
+
98
+ it "returns array of same object type" do
99
+ expect(Array.toy(3, Object).first).to be_an_instance_of Object
100
+ end
101
+
102
+ it "has `n` size of values in the array" do
103
+ expect(Array.toy(3, Numeric).size).to eq 3
104
+ end
105
+
106
+ it "raises an exception if Object does not respond to `new`" do
107
+ expect {
108
+ Array.toy(3, Kernel)
109
+ }.to raise_error
110
+ end
111
+ end
112
+ end
113
+ end
114
+
115
+ context "with `n` size and block" do
116
+ it "returns array" do
117
+ expect(Array.toy(n) {|i| i*3}).to eq [0, 3, 6]
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hash do
4
+ let(:n) { 3 }
5
+ let(:toy) { Hash.toy(n) }
6
+
7
+ it 'has valid type' do
8
+ expect(Hash.toy.class).to eq Hash
9
+ end
10
+
11
+ it 'has `n` number key / value pairs' do
12
+ expect(Hash.toy(n).each_pair.count).to eq n
13
+ end
14
+
15
+ it 'able handle `n` of large sizes' do
16
+ expect(Hash.toy(10000).each_pair.count).to eq 10000
17
+ end
18
+
19
+ it 'uses letters as keys' do
20
+ expect(Hash.toy(n).keys).to eq %w|a b c|
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe String do
4
+ let(:n) { 500 }
5
+ let(:toy) { String.toy(n) }
6
+
7
+ it "has valid type" do
8
+ expect(toy.class).to eq String
9
+ end
10
+
11
+ it "has right number of words" do
12
+ expect(toy.split.size).to eq n
13
+ end
14
+
15
+ it "does not end with space" do
16
+ expect(toy[-1]).to_not eq ' '
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Toy do
4
+ context '#hash_range' do
5
+ it "returns range object" do
6
+ expect(Toy.hash_range_for(3).class).to eq Range
7
+ end
8
+
9
+ it 'works with various sizes' do
10
+ expect(Toy.hash_range_for(3)).to eq ('a'..'z')
11
+ expect(Toy.hash_range_for(700)).to eq ('a'..'zz')
12
+ expect(Toy.hash_range_for(1000)).to eq ('a'..'zzz')
13
+ expect(Toy.hash_range_for(20000)).to eq ('a'..'zzzz')
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ require "pry-toys"
2
+ require "rspec"
3
+
4
+ RSpec.configure do |config|
5
+ # Use color in STDOUT
6
+ config.color_enabled = true
7
+
8
+ # Use color not only in STDOUT but also in pagers and files
9
+ config.tty = true
10
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pry-toys
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - ariabov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-23 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
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: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: ''
70
+ email:
71
+ - alexariabov@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".pryrc"
78
+ - Gemfile
79
+ - Guardfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/pry-toys.rb
84
+ - lib/pry-toys/array.rb
85
+ - lib/pry-toys/hash.rb
86
+ - lib/pry-toys/string.rb
87
+ - lib/pry-toys/toy.rb
88
+ - pry-toys.gemspec
89
+ - spec/lib/pry-toys/array_spec.rb
90
+ - spec/lib/pry-toys/hash_spec.rb
91
+ - spec/lib/pry-toys/string_spec.rb
92
+ - spec/lib/pry-toys/toy_spec.rb
93
+ - spec/spec_helper.rb
94
+ homepage: http://github.com/ariabov/pry-toys
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.2.2
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Toy objects for Pry
118
+ test_files:
119
+ - spec/lib/pry-toys/array_spec.rb
120
+ - spec/lib/pry-toys/hash_spec.rb
121
+ - spec/lib/pry-toys/string_spec.rb
122
+ - spec/lib/pry-toys/toy_spec.rb
123
+ - spec/spec_helper.rb