embrace 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b7a68687b49b752093867e4dceebd1500a7eb02d
4
- data.tar.gz: f0117298d06412edda85916b9306d78fe203543d
3
+ metadata.gz: 75b37f0f9a63be6227d517a43fd646e59be725d9
4
+ data.tar.gz: 626f977a239048160e1ed9494d37e6933f4e3c54
5
5
  SHA512:
6
- metadata.gz: fed4641a0817635b2f1f817805104e38ef082261d1ae5ce9e4a5aa9c4d1b41e7de7e4e7d01eade8b14339d116992329866197a8908afd45d2ccb4cfc3c140178
7
- data.tar.gz: 69248b4b2cbfb302ca889c840bdfd9682c3d83e565f9c378158405427266e21ed280c60e39eda9812c22bf9d65d255b424541363896e8a251ae9417c71ede4ad
6
+ metadata.gz: 8fbc5529b32346338814f34d0ea9cb4dc18c7d44ed41439e5ada2e1ee4524326cbbfab191ea2b4b247e2cf2ff53d7000b4d0b08825c9de83392364b8fd5e902d
7
+ data.tar.gz: a6ff5d1f94a1d09c49be21cf218d170407444e2c79bd5948fcf72d6e780fed00d6515e4923199dee15d09a526413f0a8a60e16b46cf89f8426d12a5070de0eab
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.travis.yml CHANGED
@@ -1,5 +1,16 @@
1
1
  sudo: false
2
+
2
3
  language: ruby
3
4
  rvm:
4
5
  - 2.3.1
6
+
5
7
  before_install: gem install bundler -v 1.13.6
8
+
9
+ script: 'bundle exec rake'
10
+
11
+ notifications:
12
+ email:
13
+ recipients:
14
+ - john+embrace@carney.id.au
15
+ on_failure: change
16
+ on_success: never
data/README.md CHANGED
@@ -1,8 +1,97 @@
1
1
  # Embrace
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/embrace`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [![Gem Version][gem-badge]][gem]
4
+ [![Build status][build-badge]][build]
5
+ [![Coverage Status][coverage-badge]][coverage]
4
6
 
5
- TODO: Delete this and the text above, and describe your gem
7
+ Embrace is a simply library for bracketing strings, or parts of strings. While it focuses on common
8
+ bracketing styles - `()`, `[]`, and `{}` - you can use custom styles, and even wrap text in arbitrary
9
+ strings.
10
+
11
+ ## Usage
12
+
13
+ The simplest way to use Embrace is to use its `String` refinements. Just put the following in your
14
+ file, module or class.
15
+
16
+ ```ruby
17
+ require "embrace"
18
+
19
+ module MyModule
20
+ using Embrace
21
+
22
+ # ...
23
+ end
24
+ ```
25
+
26
+ If you want Embrace's `String` methods to be available globally, then include `Embrace::StringMethods`
27
+ in an app initializer somewhere.
28
+
29
+ ```ruby
30
+ require "embrace"
31
+
32
+ String.include Embrace::StringMethods
33
+ ```
34
+
35
+ ### Basic bracketing methods
36
+
37
+ Whether you use the refinements, or include the string methods, the following string methods will become
38
+ available:
39
+
40
+ ```
41
+ "text".bracket # => "[text]"
42
+ "text".parenthesize # => "(text)"
43
+ "text".brace # => "{text}"
44
+ ```
45
+
46
+ ### Custom bracket styles
47
+
48
+ Each of the methods accepts a `style` parameter, which will be used to bracket the text. Note that the
49
+ style should include both opening and closing strings, but is otherwise entirely arbitrary. The first
50
+ half of the style will be placed before the text, and the second half will be placed after the text. If
51
+ the style is an odd length, the extra character will go after the text.
52
+
53
+ ```ruby
54
+ "text".bracket(style: "<b></b>") # => "<b>text</b>"
55
+ "text".parenthesize(style: "<-->") # => "<-text->"
56
+ "text".brace(style: "«»") # => "«text»"
57
+ ```
58
+
59
+ ### Bracketing portions of text
60
+
61
+ All of the Embrace methods accept a `pattern` parameter. If supplied, then only the matching portions of
62
+ the string will be bracketed.
63
+
64
+ ```ruby
65
+ "the quick brown fox.".parenthesize # => "(the quick brown fox.)"
66
+ "the quick brown fox.".parenthesize(pattern: /quick|fox/) # => "the (quick) brown (fox)."
67
+ ```
68
+
69
+ ### Wrapping text in arbitrary strings
70
+
71
+ If you need to wrap text in asymmetrical strings, then you can supply an array as the `style` parameter.
72
+
73
+ ```ruby
74
+ "the quick brown fox.".bracket(style: %w{ beginning> <end }) # => "beginning>the quick brown fox.<end"
75
+ ```
76
+
77
+ Note that only the first two elements of the array will be use.
78
+
79
+ ### Module function
80
+
81
+ `bracket` is available as a module function in the `Embrace` module.
82
+
83
+ ```ruby
84
+ Embrace.bracket("some text", style: "<i></i>") # => "<i>some text</i>"
85
+ ```
86
+
87
+ ### Currying
88
+
89
+ Finally, you can create a "curried" version of `Embrace.bracket`.
90
+
91
+ ```ruby
92
+ wrapper = Embrace.bracketer(style: "()", pattern: "text")
93
+ [ "some text", "some more text" ].map(&wrapper) # => [ "some (text)", "some more (text)" ]
94
+ ```
6
95
 
7
96
  ## Installation
8
97
 
@@ -20,22 +109,29 @@ Or install it yourself as:
20
109
 
21
110
  $ gem install embrace
22
111
 
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
112
  ## Development
28
113
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
114
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
115
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
116
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
117
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update
118
+ the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the
119
+ version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
120
 
33
121
  ## Contributing
34
122
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/embrace. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
123
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/embrace. This project is
124
+ intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the
125
+ [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
126
 
37
127
 
38
128
  ## License
39
129
 
40
130
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
131
 
132
+ [gem-badge]: https://badge.fury.io/rb/embrace.svg
133
+ [gem]: http://badge.fury.io/rb/embrace
134
+ [build-badge]: https://travis-ci.org/johncarney/embrace.svg?branch=master
135
+ [build]: https://travis-ci.org/johncarney/embrace
136
+ [coverage-badge]: https://img.shields.io/coveralls/johncarney/embrace.svg
137
+ [coverage]: https://coveralls.io/r/johncarney/embrace?branch=master
data/embrace.gemspec CHANGED
@@ -21,7 +21,10 @@ Gem::Specification.new do |spec|
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
23
 
24
+ spec.required_ruby_version = ">= 2.1.0"
25
+
24
26
  spec.add_development_dependency "bundler", "~> 1.13"
25
27
  spec.add_development_dependency "rake", "~> 10.0"
26
28
  spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_development_dependency "coveralls"
27
30
  end
@@ -6,8 +6,7 @@ module Embrace
6
6
 
7
7
  def split(style)
8
8
  i = style.size / 2
9
- j = style.size - i
10
- [ style[0...i], style[j..-1] ]
9
+ [ style[0...i], style[i..-1] ]
11
10
  end
12
11
  end
13
12
 
@@ -1,19 +1,15 @@
1
1
  module Embrace
2
2
  module StringMethods
3
- def wrap(*with)
4
- Embrace.wrap(self, *with)
3
+ def bracket(style: "[]", **options)
4
+ Embrace.bracket(self, style: style, **options)
5
5
  end
6
6
 
7
- def bracket(style: "[]")
8
- Embrace.bracket(self, style: style)
7
+ def parenthesize(style: "()", **options)
8
+ Embrace.bracket(self, style: style, **options)
9
9
  end
10
10
 
11
- def parenthesize(style: "()")
12
- Embrace.bracket(self, style: style)
13
- end
14
-
15
- def brace(style: "{}")
16
- Embrace.bracket(self, style: style)
11
+ def brace(style: "{}", **options)
12
+ Embrace.bracket(self, style: style, **options)
17
13
  end
18
14
  end
19
15
  end
@@ -1,3 +1,3 @@
1
1
  module Embrace
2
- VERSION = "0.2.0"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/embrace.rb CHANGED
@@ -3,71 +3,19 @@ require "embrace/brackets"
3
3
  require "embrace/string_methods"
4
4
 
5
5
  module Embrace
6
- using Brackets
7
-
8
6
  refine String do
9
7
  include StringMethods
10
8
  end
11
9
 
12
10
  module_function
13
11
 
14
- # wrap
15
-
16
- def wrap(text, before, after)
17
- "#{before}#{text}#{after}"
18
- end
19
-
20
- def wrap_if(text, *with, &test)
21
- return text unless test.yield text
22
-
23
- wrap(text, *with)
24
- end
25
-
26
- def wrap_unless(text, *with, &test)
27
- return text if test.yield text
28
-
29
- wrap(text, *with)
30
- end
31
-
32
- # wrapper
33
-
34
- def wrapper(*with)
35
- ->(text) { wrap(text, *with) }
36
- end
37
-
38
- def if_wrapper(*with, &test)
39
- ->(text) { wrap_if(text, *with, &test) }
40
- end
41
-
42
- def unless_wrapper(*with, &test)
43
- ->(text) { wrap_unless(text, *with, &test) }
44
- end
45
-
46
- # bracket
47
-
48
- def bracket(text, style:)
49
- wrap(text, *Brackets(style))
50
- end
51
-
52
- def bracket_if(text, style:, &test)
53
- wrap_if(text, *Brackets(style), &test)
54
- end
55
-
56
- def bracket_unless(text, style:, &test)
57
- wrap_unless(text, *Brackets(style), &test)
58
- end
59
-
60
- # bracketer
61
-
62
- def bracketer(style:)
63
- wrapper(*Brackets(style))
64
- end
65
-
66
- def if_bracketer(style:, &test)
67
- if_wrapper(*Brackets(style), &test)
12
+ def bracket(text, style:, pattern: /\A.*\z/)
13
+ opening, closing = Brackets(style)
14
+ text.gsub(pattern, "#{opening}\\0#{closing}")
68
15
  end
69
16
 
70
- def unless_bracketer(style:, &test)
71
- unless_wrapper(*Brackets(style), &test)
17
+ def bracketer(style:, **options)
18
+ brackets = Brackets(style)
19
+ ->(text) { bracket(text, style: brackets, **options)}
72
20
  end
73
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embrace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Carney
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-11-12 00:00:00.000000000 Z
11
+ date: 2016-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: coveralls
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'
55
69
  description: Simple library for enclosing text in brackets, or anything else that
56
70
  takes your fancy.
57
71
  email:
@@ -60,6 +74,7 @@ executables: []
60
74
  extensions: []
61
75
  extra_rdoc_files: []
62
76
  files:
77
+ - ".coveralls.yml"
63
78
  - ".gitignore"
64
79
  - ".rspec"
65
80
  - ".travis.yml"
@@ -87,7 +102,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
87
102
  requirements:
88
103
  - - ">="
89
104
  - !ruby/object:Gem::Version
90
- version: '0'
105
+ version: 2.1.0
91
106
  required_rubygems_version: !ruby/object:Gem::Requirement
92
107
  requirements:
93
108
  - - ">="