string_stubber 0.0.2
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.
- data/.gitignore +6 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +16 -0
- data/README.md +37 -0
- data/Rakefile +22 -0
- data/lib/string_stubber/base.rb +41 -0
- data/lib/string_stubber/core_ext.rb +74 -0
- data/lib/string_stubber/version.rb +3 -0
- data/lib/string_stubber.rb +16 -0
- data/string_stubber.gemspec +21 -0
- metadata +74 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# StringStubber
|
2
|
+
|
3
|
+
String Stubber is a simple gem that lets you stub strings, using "words" as the unit of measure. For example:
|
4
|
+
|
5
|
+
require 'string_stubber'
|
6
|
+
require 'lorem' # Or just add your own text
|
7
|
+
|
8
|
+
str = Lorem::Base.new('paragraphs', 1).output
|
9
|
+
# => "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus vitae risus ..."
|
10
|
+
|
11
|
+
# Get the first 5 words
|
12
|
+
str.stub_words(5)
|
13
|
+
# => "Lorem ipsum dolor sit amet"
|
14
|
+
|
15
|
+
# Get the NEXT 5 words
|
16
|
+
str.stub_words(5, true)
|
17
|
+
# => ", consectetuer adipiscing elit. Vivamus vitae"
|
18
|
+
|
19
|
+
# Grab the _complete_ words, appearing _up to_ position 20
|
20
|
+
str.stub_to(70)
|
21
|
+
# => "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus "
|
22
|
+
|
23
|
+
str.stub_to(70).size
|
24
|
+
# => 66
|
25
|
+
|
26
|
+
# Grab the _complete_ words, appearing _through_ position 20
|
27
|
+
str.stub_thru(70)
|
28
|
+
# => "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus risus"
|
29
|
+
|
30
|
+
str.stub_thru(70).size
|
31
|
+
# => 72
|
32
|
+
|
33
|
+
So far, so good! Suggestions are welcome.
|
34
|
+
|
35
|
+
# TODO
|
36
|
+
|
37
|
+
stubbing from the right direction is simple to do and will be the next addition.
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rspec'
|
5
|
+
require 'rspec/core'
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
|
8
|
+
task :release => :spec
|
9
|
+
|
10
|
+
desc "Run Specs"
|
11
|
+
Rspec::Core::RakeTask.new(:spec) do |spec|
|
12
|
+
spec.pattern = "spec/**/*_spec.rb"
|
13
|
+
spec.verbose = true
|
14
|
+
spec.rspec_opts = ['--color']
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'yard'
|
18
|
+
|
19
|
+
desc "Generate YARD docs"
|
20
|
+
YARD::Rake::YardocTask.new(:yard) do |t|
|
21
|
+
t.files += ['lib/**/*.rb']
|
22
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module StringStubber
|
2
|
+
module Base
|
3
|
+
WORD = /\b(\w+)\b/
|
4
|
+
SNIP = /\s*?\W+$/
|
5
|
+
|
6
|
+
def stub_words(text, max_words)
|
7
|
+
scanner = StringScanner.new(text.to_s)
|
8
|
+
|
9
|
+
return scan_words(scanner, max_words)
|
10
|
+
end
|
11
|
+
|
12
|
+
def stub_text(text, max_text)
|
13
|
+
string = text.to_s
|
14
|
+
scanner = StringScanner.new(string)
|
15
|
+
|
16
|
+
return scan_text(scanner, max_text)
|
17
|
+
end
|
18
|
+
|
19
|
+
def scan_word(scanner)
|
20
|
+
(str = scanner.scan_until(WORD)).gsub!(SNIP, '')
|
21
|
+
|
22
|
+
return str
|
23
|
+
end
|
24
|
+
|
25
|
+
def scan_words(scanner, max_words)
|
26
|
+
max_words.times.map {
|
27
|
+
scanner.scan_until(WORD)
|
28
|
+
}.flatten.join
|
29
|
+
end
|
30
|
+
|
31
|
+
def scan_text(scanner, max_text)
|
32
|
+
start = scanner.pos
|
33
|
+
|
34
|
+
until scanner.pos > max_text || scanner.scan_until(WORD).nil?; end
|
35
|
+
|
36
|
+
(str = scanner.pre_match || scanner.string[start, max_text]).gsub!(SNIP, '')
|
37
|
+
|
38
|
+
return str
|
39
|
+
end
|
40
|
+
end # module Base
|
41
|
+
end # module StringStubber
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module StringStubber
|
2
|
+
module CoreExt
|
3
|
+
include StringStubber::Base
|
4
|
+
|
5
|
+
def scanner
|
6
|
+
@scanner ||= StringScanner.new(self)
|
7
|
+
end
|
8
|
+
|
9
|
+
def stub_words(max_words, save_pos=false)
|
10
|
+
scanner.reset unless save_pos
|
11
|
+
|
12
|
+
if max_words < 0
|
13
|
+
rev_stub_words(max_words.abs, save_pos)
|
14
|
+
else
|
15
|
+
scan_words(scanner, max_words)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
alias :stub :stub_words
|
19
|
+
alias :words :stub_words
|
20
|
+
|
21
|
+
def stub_at_most(max_chars, save_pos=true)
|
22
|
+
scanner.reset unless save_pos
|
23
|
+
|
24
|
+
if max_chars < 0
|
25
|
+
rev_stub_at_most(max_chars.abs, save_pos)
|
26
|
+
else
|
27
|
+
scan_text(scanner, max_chars)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
alias :stub_text :stub_at_most
|
31
|
+
alias :stub_chars :stub_at_most
|
32
|
+
alias :stub_pos :stub_at_most
|
33
|
+
alias :stub_to :stub_at_most
|
34
|
+
|
35
|
+
def stub_at_least(min_chars, save_pos=true)
|
36
|
+
scanner.reset unless save_pos
|
37
|
+
|
38
|
+
if min_chars < 0
|
39
|
+
rev_stub_at_least(min_chars.abs, save_pos)
|
40
|
+
else
|
41
|
+
scan_text(scanner, min_chars) << scan_word(scanner).to_s
|
42
|
+
end
|
43
|
+
end
|
44
|
+
alias :stub_thru :stub_at_least
|
45
|
+
alias :stub_through :stub_at_least
|
46
|
+
|
47
|
+
|
48
|
+
private
|
49
|
+
def rev_stub_words(max_words, save_pos=false)
|
50
|
+
str = self.reverse
|
51
|
+
str.scanner.pos = 0 - scanner.pos if save_pos
|
52
|
+
|
53
|
+
return str.stub_words(max_words, save_pos).reverse!
|
54
|
+
end
|
55
|
+
|
56
|
+
def rev_stub_at_most(max_chars, save_pos=true)
|
57
|
+
str = self.reverse
|
58
|
+
str.scanner.pos = 0 - scanner.pos if save_pos
|
59
|
+
|
60
|
+
return str.stub_at_most(max_chars, save_pos).reverse!
|
61
|
+
end
|
62
|
+
|
63
|
+
def rev_stub_at_least(min_chars, save_pos=true)
|
64
|
+
str = self.reverse
|
65
|
+
str.scanner.pos = 0 - scanner.pos if save_pos
|
66
|
+
|
67
|
+
return str.stub_at_least(min_chars, save_pos).reverse!
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
class String
|
73
|
+
include StringStubber::CoreExt
|
74
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
require 'rubygems' if RUBY_VERSION < '1.9'
|
5
|
+
require 'bundler'
|
6
|
+
|
7
|
+
Bundler.require
|
8
|
+
|
9
|
+
require 'strscan'
|
10
|
+
|
11
|
+
require 'string_stubber/base'
|
12
|
+
require 'string_stubber/core_ext'
|
13
|
+
|
14
|
+
module StringStubber
|
15
|
+
extend StringStubber::Base
|
16
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "string_stubber/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "string_stubber"
|
7
|
+
s.version = StringStubber::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Scott Gonyea"]
|
10
|
+
s.email = ["me@sgonyea.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Allows you to truncate Strings, while preserving whole-words.}
|
13
|
+
s.description = %q{StringStubber allows you to truncate Strings, while preserving whole-words.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "string_stubber"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: string_stubber
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Scott Gonyea
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-01-19 00:00:00 -08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: StringStubber allows you to truncate Strings, while preserving whole-words.
|
22
|
+
email:
|
23
|
+
- me@sgonyea.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- Gemfile.lock
|
34
|
+
- README.md
|
35
|
+
- Rakefile
|
36
|
+
- lib/string_stubber.rb
|
37
|
+
- lib/string_stubber/base.rb
|
38
|
+
- lib/string_stubber/core_ext.rb
|
39
|
+
- lib/string_stubber/version.rb
|
40
|
+
- string_stubber.gemspec
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: ""
|
43
|
+
licenses: []
|
44
|
+
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project: string_stubber
|
69
|
+
rubygems_version: 1.3.7
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Allows you to truncate Strings, while preserving whole-words.
|
73
|
+
test_files: []
|
74
|
+
|