embrace 1.1.0 → 1.2.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: a44011000a9416a96d2efd8be38b4570d3954ae4
4
- data.tar.gz: 74002464e5315a25257c3645e2afbea79e23c3e5
3
+ metadata.gz: 2c95bc8f771e08e4ef58fa14e41645a09eda9b45
4
+ data.tar.gz: 07e95a03836461446da5297a12c7843d338b1ceb
5
5
  SHA512:
6
- metadata.gz: 6bb8a6fbf3c5908e3d07767bfde1e62940b9744fd90ca35d1cff1b743580b4222c956c48e3f872ad1b9fb7e971fc8f1bbf42f957bcc6661e276e582fdf5bc66d
7
- data.tar.gz: 52011de09465cf9706ba7e6ddf7dc87ed65b7dbf105eb69acc29208b10e246ffabfa22896fa24290c575066b4f128bf02043d583bfdac7897b2c4b431d90d798
6
+ metadata.gz: 012075dc5a54d2433ba514ab50ca31a32fee46bbfb699072995276b58c09caf0f4a8db8dafeac8db20cb30b878ff9e340ee624fb8f0e6218a60f98a8054bb077
7
+ data.tar.gz: 71464d197802977afd6e15548cbd8ff60c4e7756a7839d3201a949b6709149e830dfa2406e1b6b073014ca00e51197be3a2a10078cc8a5bd8d1c6a9d264fc8aa
@@ -1,40 +1,41 @@
1
1
  require "embrace"
2
2
 
3
3
  module Embrace
4
- class Brackets
5
- attr_reader :opening, :closing
6
-
7
- def initialize(opening, closing)
8
- @opening = opening
9
- @closing = closing
10
- end
11
-
12
- def around(text)
13
- "#{opening}#{text}#{closing}"
4
+ class Brackets < Proc
5
+ class << self
6
+ def from_brackets(opening, closing)
7
+ new do |text|
8
+ "#{opening}#{text}#{closing}"
9
+ end
10
+ end
11
+
12
+ def from_str(style)
13
+ i = style.size / 2
14
+ from_brackets(style[0...i], style[i..-1])
15
+ end
14
16
  end
15
17
 
16
18
  def to_ary
17
- [ opening, closing ]
19
+ bracketed = self["a"]
20
+ index = bracketed.chars.zip(self["b"].chars).find_index { |a, b| a != b }
21
+ [ bracketed[0...index], bracketed[(index + 1)..-1] ]
18
22
  end
19
23
 
20
24
  alias_method :to_a, :to_ary
21
25
 
22
- def to_s
23
- "#{opening}#{closing}"
24
- end
25
-
26
- def self.from_str(style)
27
- i = style.size / 2
28
- new(style[0...i], style[i..-1])
26
+ def inspect
27
+ to_a.inspect
29
28
  end
30
29
  end
31
30
 
32
31
  module_function
33
32
 
34
- def Brackets(style)
33
+ def Brackets(style, *rest)
34
+ return Brackets.from_brackets(*[ style, *rest ].map(&:to_s)) unless rest.empty?
35
+
35
36
  case style
36
37
  when Brackets then style
37
- when Array then Brackets.new(*style)
38
+ when Array then Brackets(*style)
38
39
  else
39
40
  Brackets.from_str(style.to_s)
40
41
  end
@@ -1,3 +1,3 @@
1
1
  module Embrace
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/embrace.rb CHANGED
@@ -3,9 +3,9 @@ require "embrace/brackets"
3
3
  require "embrace/string_methods"
4
4
 
5
5
  module Embrace
6
- BRACKETS = Brackets.new("[", "]").freeze
7
- PARENTHESES = Brackets.new("(", ")").freeze
8
- BRACES = Brackets.new("{", "}").freeze
6
+ BRACKETS = Brackets("[", "]").freeze
7
+ PARENTHESES = Brackets("(", ")").freeze
8
+ BRACES = Brackets("{", "}").freeze
9
9
 
10
10
  refine String do
11
11
  include StringMethods
@@ -14,7 +14,7 @@ module Embrace
14
14
  module_function
15
15
 
16
16
  def bracket(text, style:, pattern: /\A.*\z/)
17
- text.gsub(pattern, &Brackets(style).method(:around))
17
+ text.gsub(pattern, &Brackets(style))
18
18
  end
19
19
 
20
20
  def bracketer(style:, **options)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embrace
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Carney