corefines 1.1.0 → 1.2.0
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 +4 -4
- data/CHANGELOG.adoc +5 -0
- data/README.adoc +3 -2
- data/lib/corefines/string.rb +45 -0
- data/lib/corefines/version.rb +1 -1
- data/spec/string/indent_spec.rb +48 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c7839672b793a5df3d5ffa892272260eccf0c0db
|
4
|
+
data.tar.gz: 0a263d7e00645c8451d93461bfe9ed81cd8ccc1c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 93f5a5a21061dea9e9aaddf7ea5520e7743a0c84c9635d42c63cdaed032fae12e328e141a60d40ac22ea7d986b3cda8a258374a583df7fdbcafbf7d8856bc317
|
7
|
+
data.tar.gz: 7b31c6b28bb018d1b0ee9fd9368922a0a282a545e461c0e6f488111ea0837abcdee533249d97560c7f31adc47aaf8cc2698fd4dc03bf353ce2435b226aef7e2d
|
data/CHANGELOG.adoc
CHANGED
@@ -3,6 +3,11 @@
|
|
3
3
|
:doc-base-url: http://www.rubydoc.info/github/jirutka/corefines/Corefines
|
4
4
|
:issue-uri: {repo-uri}/issues
|
5
5
|
|
6
|
+
== 1.2.0 (2015-04-27)
|
7
|
+
|
8
|
+
* Add new refinement {doc-base-url}/String/Indent[String#indent].
|
9
|
+
|
10
|
+
|
6
11
|
== 1.1.0 (2015-04-25)
|
7
12
|
|
8
13
|
* Add new refinement {doc-base-url}/String/ToB[String#to_b].
|
data/README.adoc
CHANGED
@@ -41,12 +41,12 @@ TODO
|
|
41
41
|
Add this line to your application’s Gemfile:
|
42
42
|
|
43
43
|
[source]
|
44
|
-
gem 'corefines', '~> 1.
|
44
|
+
gem 'corefines', '~> 1.2'
|
45
45
|
|
46
46
|
or to your gemspec:
|
47
47
|
|
48
48
|
[source]
|
49
|
-
s.add_runtime_dependency 'corefines', '~> 1.
|
49
|
+
s.add_runtime_dependency 'corefines', '~> 1.2'
|
50
50
|
|
51
51
|
and then execute:
|
52
52
|
|
@@ -161,6 +161,7 @@ Not ideal indeed, but probably the best of what we can achieve.
|
|
161
161
|
** {doc-base-url}/String/Color[#color]
|
162
162
|
** {doc-base-url}/String/Concat[#concat]
|
163
163
|
** {doc-base-url}/String/Decolor[#decolor]
|
164
|
+
** {doc-base-url}/String/Indent[#indent]
|
164
165
|
** {doc-base-url}/String/Remove[#remove]
|
165
166
|
** {doc-base-url}/String/ToB[#to_b]
|
166
167
|
** {doc-base-url}/String/Unindent[#unindent] (alias `#strip_heredoc`)
|
data/lib/corefines/string.rb
CHANGED
@@ -126,6 +126,50 @@ module Corefines
|
|
126
126
|
end
|
127
127
|
end
|
128
128
|
|
129
|
+
##
|
130
|
+
# @!method indent(amount, indent_str = nil, indent_empty_lines = false)
|
131
|
+
# Returns an indented copy of this string.
|
132
|
+
#
|
133
|
+
# @example
|
134
|
+
# "foo".indent(2) # => " foo"
|
135
|
+
# " foo".indent(2) # => " foo"
|
136
|
+
# "foo\n\t\tbar".indent(2) # => "\t\tfoo\n\t\t\t\tbar"
|
137
|
+
# "foo".indent(2, '.') # => "..foo"
|
138
|
+
# "foo\n\nbar".indent(2) # => " foo\n\n bar"
|
139
|
+
# "foo\n\nbar".indent(2, nil, true) # => " foo\n \n bar"
|
140
|
+
#
|
141
|
+
# @param amount [Fixnum] the indent size.
|
142
|
+
# @param indent_str [String, nil] the indent character to use.
|
143
|
+
# The default is +nil+, which tells the method to make a guess by
|
144
|
+
# peeking at the first indented line, and fallback to a space if
|
145
|
+
# there is none.
|
146
|
+
# @param indent_empty_lines [Boolean] whether empty lines should be indented.
|
147
|
+
# @return [String] a new string.
|
148
|
+
#
|
149
|
+
# @!method indent!(amount, indent_str = nil, indent_empty_lines = false)
|
150
|
+
# Returns the indented string, or +nil+ if there was nothing to indent.
|
151
|
+
# This is same as {#indent}, except it indents the receiver in-place.
|
152
|
+
#
|
153
|
+
# @see #indent
|
154
|
+
# @param amount (see #indent)
|
155
|
+
# @param indent_str (see #indent)
|
156
|
+
# @param indent_empty_lines (see #indent)
|
157
|
+
# @return [String] self
|
158
|
+
#
|
159
|
+
module Indent
|
160
|
+
refine ::String do
|
161
|
+
def indent(amount, indent_str = nil, indent_empty_lines = false)
|
162
|
+
dup.tap { |str| str.indent! amount, indent_str, indent_empty_lines }
|
163
|
+
end
|
164
|
+
|
165
|
+
def indent!(amount, indent_str = nil, indent_empty_lines = false)
|
166
|
+
indent_str = indent_str || self[/^[ \t]/] || ' '
|
167
|
+
re = indent_empty_lines ? /^/ : /^(?!$)/
|
168
|
+
gsub! re, indent_str * amount
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
129
173
|
##
|
130
174
|
# @!method remove(*patterns)
|
131
175
|
# Returns a copy of this string with *all* occurrences of the _patterns_
|
@@ -232,6 +276,7 @@ module Corefines
|
|
232
276
|
|
233
277
|
class << self
|
234
278
|
alias_method :concat!, :concat
|
279
|
+
alias_method :indent!, :indent
|
235
280
|
alias_method :remove!, :remove
|
236
281
|
end
|
237
282
|
end
|
data/lib/corefines/version.rb
CHANGED
@@ -0,0 +1,48 @@
|
|
1
|
+
describe String do
|
2
|
+
using Corefines::String::indent
|
3
|
+
|
4
|
+
describe '#indent' do
|
5
|
+
|
6
|
+
context "with default indent_str" do
|
7
|
+
context "string already indented with spaces" do
|
8
|
+
it "indents with spaces" do
|
9
|
+
expect( "foo\n bar".indent(2) ).to eql " foo\n bar"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context "string already indented with tabs" do
|
14
|
+
it "indents with tabs" do
|
15
|
+
expect( "foo\n\tbar".indent(2) ).to eql "\t\tfoo\n\t\t\tbar"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context "string without indentation" do
|
20
|
+
it "indents with space" do
|
21
|
+
expect( "foo\nbar".indent(3) ).to eql " foo\n bar"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "string that only contain newlines (edge cases)" do
|
26
|
+
it "doesn't indent at all" do
|
27
|
+
['', "\n", "\n" * 7].each do |str|
|
28
|
+
expect( str.indent(8) ).to eql str
|
29
|
+
expect( str.indent(1, "\t") ).to eql str
|
30
|
+
expect( str.indent!(8) ).to be_nil
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "with indent_str" do
|
37
|
+
it "indents lines with the specified string" do
|
38
|
+
expect( "foo\n bar".indent(2, '.') ).to eql "..foo\n.. bar"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "with indent_empty_lines" do
|
43
|
+
it "indents all lines including empty ones" do
|
44
|
+
expect( "foo\n\nbar".indent(2, nil, true) ).to eql " foo\n \n bar"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: corefines
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jakub Jirutka
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: asciidoctor
|
@@ -142,6 +142,7 @@ files:
|
|
142
142
|
- spec/string/color_spec.rb
|
143
143
|
- spec/string/concat_spec.rb
|
144
144
|
- spec/string/decolor_spec.rb
|
145
|
+
- spec/string/indent_spec.rb
|
145
146
|
- spec/string/remove_spec.rb
|
146
147
|
- spec/string/to_b_spec.rb
|
147
148
|
- spec/string/unindent_spec.rb
|