string-builder 2.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
+ SHA256:
3
+ metadata.gz: 04362e749e0f89971291f469659cb489ef05e5c13016e60039c1efa1a0b8f0eb
4
+ data.tar.gz: 0f19ec17b5d0621f23640febb72e27373d291b1ecfd7cf8a5fe0c911b9a2c43d
5
+ SHA512:
6
+ metadata.gz: 46d9608d486bbf449b43d20ba78d9b350a7fa91f40bbd17a51b681f0406d7a3c4cca83a4acc4ed8cd5f2fb1eeebe8caf5afb25d50e344b8db8fa2a5a8f4425ca
7
+ data.tar.gz: ff9788db4215af48c64a74dcbea2564ffb4972425a86ea8065124d5fef5f11aec32c1659f7cb64bba85dd22247e997f5930933148875bc31c4053be6647a2905
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/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Edwin Onuonga
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,140 @@
1
+ [![Build Status](https://travis-ci.org/eonu/string-builder.svg?branch=master)](https://travis-ci.org/eonu/string-builder)
2
+ ![Gem](https://img.shields.io/gem/v/string-builder.svg)
3
+ [![License](https://img.shields.io/github/license/eonu/string-builder.svg)](https://github.com/eonu/string-builder/blob/master/LICENSE)
4
+
5
+ # String::Builder
6
+
7
+ Modified port of the [String::Builder IO initializer](https://crystal-lang.org/api/0.20.3/String/Builder.html#build%28capacity%3AInt%3D64%2C%26block%29%3AString-class-method) for the String class of the Crystal programming language.
8
+
9
+ ## Limitations of string building in Ruby and Crystal
10
+
11
+ The [String::Builder](https://crystal-lang.org/api/0.20.3/String/Builder.html) class of the Crystal programming language provides an initializer method for the `String` class called `build` which is essentially an optimized version of Ruby's `StringIO`.
12
+
13
+ Ruby's `StringIO` and Crystal's `String::Builder` are great because it essentially turns strings into IO objects, allowing you to pass a block into the constructor (yielding `self`) which leads to nice chaining such as:
14
+
15
+ ```ruby
16
+ # Ruby - StringIO
17
+ test = StringIO.open do |s|
18
+ s << 'Hello '
19
+ s << 'World!'
20
+ s.string
21
+ end
22
+ #=> "Hello World!"
23
+ ```
24
+
25
+ Note the necessary `StringIO#string` method call. Since we are yielding a `StringIO` object, we must convert it to a `String` at the end. This is a bit cleaner in Crystal:
26
+
27
+ ```ruby
28
+ # Crystal - String::Builder
29
+ test = String.build do |s|
30
+ s << "Hello "
31
+ s << "World!"
32
+ end
33
+ #=> "Hello World!"
34
+ ```
35
+
36
+ ---
37
+
38
+ However, since neither of these two implementations yield a `String` object, you can't use `String` methods to mutate the object:
39
+
40
+ ```ruby
41
+ # Ruby - StringIO
42
+ test = StringIO.open do |s|
43
+ s << 'Hello '
44
+ s << 'World!'
45
+ s.upcase!
46
+ s.string
47
+ end
48
+ #=> ... undefined method `upcase!' for #<StringIO:0x00007fe0bc09d810> (NoMethodError)
49
+ ```
50
+
51
+ ```ruby
52
+ # Crystal - String::Builder
53
+ test = String.build do |s|
54
+ s << "Hello "
55
+ s << "World!"
56
+ s.gsub!("!", "?")
57
+ end
58
+ #=> ... undefined method 'gsub!' for String::Builder
59
+ ```
60
+
61
+ **That's where this gem comes in!**
62
+
63
+ ## Example
64
+
65
+ This example shows how to make a simple logger by constructing log messages with `String::Builder`.
66
+
67
+ > **NOTE**:
68
+ > - You can pass an optional argument before the block, specifying what the starting string (or any object that has a working `to_s` method) should be.
69
+ > - Since the block `yield`s a `String` object, `String::Builder` allows you to mutate the string itself as seen in this example with the `String#gsub!` method.
70
+
71
+ ```ruby
72
+ class Logger
73
+ using String::Builder
74
+ %i[error success info warning].each do |severity|
75
+ define_method(severity) do |message|
76
+ String.build Time.now.strftime("[%H:%M:%Ss] ") do |s|
77
+ s << "(#{__FILE__}) "
78
+ s << "#{severity.to_s.upcase} » #{message}"
79
+ s.gsub! '?', '!'
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ logger = Logger.new
86
+
87
+ logger.error 'String::Builder is good?'
88
+ #=> [03:54:53s] (lib/string-builder.rb) ERROR » String::Builder is good!
89
+
90
+ logger.success 'String::Builder is good?'
91
+ #=> [03:54:55s] (lib/string-builder.rb) SUCCESS » String::Builder is good!
92
+
93
+ logger.info 'String::Builder is good?'
94
+ #=> [03:54:57s] (lib/string-builder.rb) INFO » String::Builder is good!
95
+
96
+ logger.warning 'String::Builder is good?'
97
+ #=> [03:54:59s] (lib/string-builder.rb) WARNING » String::Builder is good!
98
+ ```
99
+
100
+ ## Installation
101
+
102
+ Add this line to your application's Gemfile:
103
+
104
+ ```ruby
105
+ gem 'string-builder'
106
+ ```
107
+
108
+ And then execute:
109
+
110
+ $ bundle
111
+
112
+ Or install it yourself as:
113
+
114
+ $ gem install string-builder
115
+
116
+ ## Usage
117
+
118
+ This monkey-patch is in the form of a `refinement`. This means that you will have to call the following `using` directive within the scope that you want the `String.build` class method to be monkey-patched into the `String` class:
119
+
120
+ ```ruby
121
+ require 'string/builder'
122
+ using String::Builder
123
+ ```
124
+
125
+ Though you should typically avoid doing this in the global scope (unless you really need to), and instead only use the monkey-patch where you need it - inside your specific modules or classes:
126
+
127
+ ```ruby
128
+ require 'string/builder'
129
+
130
+ class A
131
+ using String::Builder
132
+ # CAN use String.build in this class
133
+ end
134
+
135
+ module B
136
+ # CANNOT use String.build in this module
137
+ end
138
+
139
+ # CANNOT use String.build in the global scope
140
+ ```
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 "string-builder"
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
@@ -0,0 +1,15 @@
1
+ module String::Builder
2
+ refine String.singleton_class do
3
+ def build(obj = String.new)
4
+ if block_given?
5
+ yield builder = self.new
6
+ obj.dup.to_s << builder
7
+ else
8
+ obj.to_s
9
+ end
10
+ end
11
+ def respond_to?(symbol, include_all=false)
12
+ ((list = self.methods(false).dup) << :build).include? symbol
13
+ end
14
+ end
15
+ end
@@ -0,0 +1 @@
1
+ require 'string/builder'
@@ -0,0 +1,24 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "string-builder"
6
+ spec.version = "2.0.1"
7
+ spec.authors = ["Edwin Onuonga"]
8
+ spec.email = ["edwinonuonga@gmail.com"]
9
+
10
+ spec.summary = %q{Modified port of the String::Builder IO initializer for the String class of the Crystal programming language.}
11
+ spec.homepage = "https://www.github.com/eonu/string-builder"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
+ f.match(%r{^(test|spec|features)/})
16
+ end
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.16"
22
+ spec.add_development_dependency "rake", "~> 12.3"
23
+ spec.add_development_dependency "rspec", "~> 3.7"
24
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: string-builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Edwin Onuonga
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-05 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.3'
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.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.7'
55
+ description:
56
+ email:
57
+ - edwinonuonga@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/string-builder.rb
71
+ - lib/string/builder.rb
72
+ - string-builder.gemspec
73
+ homepage: https://www.github.com/eonu/string-builder
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.7.3
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Modified port of the String::Builder IO initializer for the String class
97
+ of the Crystal programming language.
98
+ test_files: []