refined-refinements 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5294d42af35dee68ae9f9d9477d83a39ec3d5de2
4
+ data.tar.gz: '056769fd009f3c71f019322a53ff7577b543a585'
5
+ SHA512:
6
+ metadata.gz: d915c1737d66d960769ba4c72ddbd2022d7b326a5cf2639d46d93c87592335dd19dea33a181d012e914b28e46a4d57e9f12ea289cfb5a8b9f5842872942bc631
7
+ data.tar.gz: 86d9e617849bcbc70fdfe70db3630ec6eadc04c1da25ac67965e618eb7c52bac3f318dff45a39381bf58565f996256938241e0b3c44b6d6b326f953ed58d1a6c
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # About
2
+
3
+ Collection of core class extensions I found useful across multiple projects. They are implemented as refinements, they do not override the global scope, so are safe to use.
4
+
5
+ Refinements allow you to redefine methods on core classes only within given context. For instance if you have class Foo and you want to redefine String#gsub only within that class, you can do so using refinements. Magnus Holm wrote a nice [intro into refinements](http://timelessrepo.com/refinements-in-ruby).
6
+
7
+ ## RR::ColourExts
8
+
9
+ ```ruby
10
+ require 'refined-refinements/colours'
11
+
12
+ using RR::ColourExts
13
+
14
+ puts '<green>Hello <red.bold>world</red.bold></green>!'.colourise
15
+ puts '<green>Hello <red.bold>world</red.bold></green>!'.colourise(bold: true)
16
+ ```
17
+
18
+ ## RR::MatchingExts
19
+
20
+ ```ruby
21
+ require 'refined-refinements/matching'
22
+
23
+ using RR::MatchingExts
24
+
25
+ string = 'Hello <bold>bold</bold> world!'
26
+ regexp = /
27
+ <(?<tag_name>[^>]+)>
28
+ (?<inner_text>.+)
29
+ <\/\k<tag_name>>
30
+ /x
31
+
32
+ string.sub(regexp) do |match, text_before_match, text_after_match|
33
+ p [:m, match]
34
+ p [:b, text_before_match]
35
+ p [:a, text_after_match]
36
+ end
37
+
38
+ # [:m, #<MatchData "<bold>bold</bold>" tag_name:"bold" inner_text:"bold">]
39
+ # [:b, "Hello "]
40
+ # [:a, " world!"]
41
+ ```
42
+
43
+ ## RR::MatchingExts
44
+
45
+ ```ruby
46
+ require 'refined-refinements/string'
47
+
48
+ using RR::StringExts
49
+
50
+ puts 'hello world!'.titlecase
51
+
52
+ # Hello world!
53
+ ```
@@ -0,0 +1,50 @@
1
+ require 'term/ansicolor'
2
+ require 'refined-refinements/matching'
3
+
4
+ module RR
5
+ module ColourExts
6
+ def self.colours
7
+ @colours ||= Object.new.extend(Term::ANSIColor)
8
+ end
9
+
10
+ refine String do
11
+ using RR::MatchingExts
12
+
13
+ def colourise(options = Hash.new)
14
+ regexp = /
15
+ <(?<dot_separated_methods>[^>]+)>
16
+ (?<text_between_tags>.*?)
17
+ <\/\k<dot_separated_methods>>
18
+ /xm
19
+
20
+ colours = RR::ColourExts.colours
21
+
22
+ result = self.gsub(regexp) do |match|
23
+ # p [:m, match] ####
24
+ methods = match[:dot_separated_methods].split('.').map(&:to_sym)
25
+ methods.push(:bold) if options[:bold]
26
+ # p [:i, match[:text_between_tags], methods, options] ####
27
+
28
+ methods.reduce(inner_text = match[:text_between_tags]) do |result, method|
29
+ # (print ' '; p [:r, result, method]) if result.match(/(<\/[^>]+>)/) ####
30
+ result.gsub!(/(<\/[^>]+>)/, "#{colours.send(method)}\\1")
31
+ # puts "#{result.inspect}.#{method}" ####
32
+ "#{colours.send(method)}#{result}#{colours.reset unless options[:recursed]}"
33
+ end
34
+ end
35
+
36
+ result.match(regexp) ? result.colourise(options.merge(recursed: true)) : result
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ # using RR::ColourExts
43
+ #
44
+ # s1 = "<green>Hello <red>world</red>!</green>"
45
+ # s2 = "<green>H<yellow>e</yellow>llo <red>world</red>!</green>" # F
46
+ # s3 = "<green>H</green>ello <red>w</red>orld!"
47
+ #
48
+ # puts s1.colourise, ''
49
+ # puts s2.colourise(bold: true), ''
50
+ # puts s3.colourise, ''
@@ -0,0 +1,15 @@
1
+ require 'date'
2
+
3
+ module RR
4
+ module DateExts
5
+ refine Date do
6
+ def weekend?
7
+ self.saturday? || self.sunday?
8
+ end
9
+
10
+ def weekday?
11
+ ! self.weekend?
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ module RR
2
+ module MatchingExts
3
+ # Counter-intuitively when regexp replacement methods are called with a block,
4
+ # the first and only block argument is the whole match as a string.
5
+ #
6
+ # There is no way to get match groups and the string before and after the match
7
+ # other than using global variables such as $~, $1, $` etc.
8
+ #
9
+ # The problem with that, besides readability and violations of basic
10
+ # industry practices, is that the minute you use another String#sub or
11
+ # String#match, these variables gets reassigned.
12
+ #
13
+ # With this refinement you can access these variables through block arguments:
14
+ #
15
+ # using RR::CoreExts
16
+ #
17
+ # 'Hello world!'.sub(/(?<word>[a-zA-Z]+)/) do |match, string_before_match, string_after_match|
18
+ # match[:word].upcase
19
+ # end
20
+
21
+ refine String do
22
+ [:sub, :sub!, :gsub, :gsub!].each do |method_name|
23
+ define_method(method_name) do |*args, &block|
24
+ if block
25
+ super(*args) { block.call($~, $`, $') }
26
+ else
27
+ super(*args)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,9 @@
1
+ module RR
2
+ module StringExts
3
+ refine String do
4
+ def titlecase
5
+ "#{self[0].upcase}#{self[1..-1]}"
6
+ end
7
+ end
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: refined-refinements
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - James C Russell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: "."
14
+ email: james@101ideas.cz
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - lib/refined-refinements/colours.rb
21
+ - lib/refined-refinements/date.rb
22
+ - lib/refined-refinements/matching.rb
23
+ - lib/refined-refinements/string.rb
24
+ homepage: http://github.com/botanicus/refined-refinements
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.6.11
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: ''
48
+ test_files: []