turkish_support 1.0.4 → 2.0.1

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
- SHA1:
3
- metadata.gz: 290cce063477228d7a8032087805c068b467af96
4
- data.tar.gz: 5fb69af5c2164fdbc3101da288c0beac801da65c
2
+ SHA256:
3
+ metadata.gz: 661daa780f4a86533a3e5b3d7607b5d131549c928ab65db35598f93fbf89c103
4
+ data.tar.gz: 6006aa50c352658fbac41af13e40e77c49e09ea9887bb2ac605457f0734f14ad
5
5
  SHA512:
6
- metadata.gz: '018f11e35079fb843da1fa9f955b9f8dac21fa3bffc80b05a23aa9eba75447a09a4fcd2f15613218e272bca91485158084ecdc9b43a0672b975b37705d8f77e3'
7
- data.tar.gz: f2c51ae7bf25e8170b71e628b257b880a3db7d6a7a96cc001baed4db9db7ee518a493704907396dfaf250a07f74c80a42dc17b4066e33eca9a29f29a27598ec8
6
+ metadata.gz: 2b06283e2a0626719a52e3e18e26bc3c4555ecedc2f2e7b05dd998a15953ef0ddf0cbf0b00ce88364941e3c8451467f9d24b6a3ee972f52422e6133e67ff1a30
7
+ data.tar.gz: e1cffa240cec788281aee54e8a1435255f90e4757b945e5768d100d48408168071a3137e1ba1597d1ce46d8383635e2c426b9fdb9371b02fde58edf38aeb5af0
data/.gitignore CHANGED
@@ -16,3 +16,4 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  todo.txt
19
+ .idea/*
@@ -1,3 +1,5 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7
1
3
  StringLiterals:
2
4
  EnforcedStyle: single_quotes
3
5
  Documentation:
@@ -5,3 +7,5 @@ Documentation:
5
7
  DotPosition:
6
8
  EnforcedStyle: leading
7
9
  Enabled: true
10
+ Metrics/BlockLength:
11
+ ExcludedMethods: ['describe', 'context']
@@ -1,3 +1,3 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1.0
3
+ - 2.7.0
@@ -0,0 +1,46 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ ## Our Standards
8
+
9
+ Examples of behavior that contributes to creating a positive environment include:
10
+
11
+ * Using welcoming and inclusive language
12
+ * Being respectful of differing viewpoints and experiences
13
+ * Gracefully accepting constructive criticism
14
+ * Focusing on what is best for the community
15
+ * Showing empathy towards other community members
16
+
17
+ Examples of unacceptable behavior by participants include:
18
+
19
+ * The use of sexualized language or imagery and unwelcome sexual attention or advances
20
+ * Trolling, insulting/derogatory comments, and personal or political attacks
21
+ * Public or private harassment
22
+ * Publishing others' private information, such as a physical or electronic address, without explicit permission
23
+ * Other conduct which could reasonably be considered inappropriate in a professional setting
24
+
25
+ ## Our Responsibilities
26
+
27
+ Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28
+
29
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30
+
31
+ ## Scope
32
+
33
+ This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34
+
35
+ ## Enforcement
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at sbagdat@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38
+
39
+ Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40
+
41
+ ## Attribution
42
+
43
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44
+
45
+ [homepage]: http://contributor-covenant.org
46
+ [version]: http://contributor-covenant.org/version/1/4/
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014-2016 Sıtkı Bağdat
1
+ Copyright (c) 2014-2020 Sıtkı Bağdat
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -2,16 +2,15 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/turkish_support.svg)](http://badge.fury.io/rb/turkish_support)
4
4
  [![Build Status](https://travis-ci.org/sbagdat/turkish_support.svg?branch=master)](https://travis-ci.org/sbagdat/turkish_support)
5
- [![Gitter chat](https://badges.gitter.im/sbagdat/turkish_support.png)](https://gitter.im/sbagdat/turkish_support)
6
5
 
7
6
  Turkish character support for core ruby methods. This gem provides support nearly all `String` methods, such as `String#upcase`, `String#downcase`, `String#match`, `String#gsub`. It also provides support for `Array#sort`and some bonus methods like `String#titleize`.
8
7
 
9
8
  ## Requirements
10
9
 
11
- * Ruby >= 2.0.0
12
- * Rails >= 4.0.0
10
+ * Ruby >= 2.7.0
11
+ * Rails >= 6.0.0
13
12
 
14
- __Notice:__ TurkishSupport uses refinements instead of monkey patching. ~~Refinements come with Ruby 2.0.0 as a new feature and also, it is an experimental feature for now. If you want to more information about refinements, you can see the doc at [http://www.ruby-doc.org/core-2.0.0/doc/syntax/refinements_rdoc.html](http://www.ruby-doc.org/core-2.0.0/doc/syntax/refinements_rdoc.html)~~ *Refinements are not an experimental feature, it is a core feature now.*
13
+ __Notice:__ TurkishSupport uses __refinements__ instead of monkey patching.
15
14
 
16
15
  * [Installation](#installation)
17
16
  * [Usage](#usage)
@@ -19,8 +18,8 @@ __Notice:__ TurkishSupport uses refinements instead of monkey patching. ~~Refine
19
18
  * [Using with ruby on rails](#using-with-ruby-on-rails)
20
19
  * [Using Core Methods](#using-core-methods)
21
20
  * [String Methods](#string-methods)
21
+ * [#<=>](#-spaceship)
22
22
  * [#[] and #[]=](#-and-)
23
- * [#=~](#-equal-tilda)
24
23
  * [capitalize](#capitalize-and-capitalize)
25
24
  * [casecmp](#casecmp)
26
25
  * [downcase](#downcase-and-downcase)
@@ -123,17 +122,20 @@ If you want to use original set of the core methods in the same scope, you can u
123
122
 
124
123
  ## String Methods
125
124
 
126
- ### [] and []=
127
-
125
+ ### <=> (spaceship)
128
126
  ```ruby
129
- 'Türkiye Cumhuriyeti'[/\w+/] # => "Türkiye"
130
- 'Çetin'[/[a-ğ]+/i] # => "Çe"
127
+ 'Cahit' <=> 'Çağla' # => -1
128
+ 'Sıtkı' <=> 'Ömer' # => 1
129
+ 'Eren' <=> 'Eren' # => 0
130
+ 'c' <=> 'ca' # => -1
131
131
  ```
132
132
 
133
- ### =~ (equal-tilda)
133
+
134
+ ### [] and []=
134
135
 
135
136
  ```ruby
136
- 'Bağlarbaşı Çarşı Kalabalık' =~ (/[s-ü]+/i) # => 8
137
+ 'Türkiye Cumhuriyeti'[/\w+/] # => "Türkiye"
138
+ 'Çetin'[/[a-ğ]+/i] # => "Çe"
137
139
  ```
138
140
 
139
141
  ### capitalize and capitalize!
@@ -1,13 +1,12 @@
1
1
  module TurkishSupport
2
2
  refine Array do
3
3
  def sort
4
- sort_by do |item|
5
- item.chars.map do |ch|
6
- if ALPHABET.include?(ch)
7
- # Add 65 to put special chars and numbers in correct order
8
- ALPHABET.index(ch) + 65
9
- else
10
- ch.ord
4
+ if block_given?
5
+ super
6
+ else
7
+ sort_by do |item|
8
+ item.chars.map do |ch|
9
+ ALPHABET.include?(ch) ? ASCII_ALPHABET[ch] : ch.ord
11
10
  end
12
11
  end
13
12
  end
@@ -1,51 +1,38 @@
1
- module TurkishSupportHelpers
2
- ALPHA = {
3
- lower: 'abcçdefgğhıijklmnoöpqrsştuüvwxyz',
4
- upper: 'ABCÇDEFGĞHIİJKLMNOÖPQRSŞTUÜVWXYZ',
5
- tr_lower: 'çğıiöşü',
6
- tr_upper: 'ÇĞIİÖŞÜ'
7
- }.freeze
1
+ # frozen_string_literal: true
8
2
 
3
+ module TurkishSupportHelpers
4
+ ALPHA = { lower: 'abcçdefgğhıijklmnoöpqrsştuüvwxyz',
5
+ upper: 'ABCÇDEFGĞHIİJKLMNOÖPQRSŞTUÜVWXYZ',
6
+ tr_lower: 'çğıiöşü',
7
+ tr_upper: 'ÇĞIİÖŞÜ' }.freeze
9
8
  ALPHABET = ALPHA[:upper] + ALPHA[:lower]
10
-
11
- META_CHARS = {
12
- '\w' => '[\p{Latin}\d_]',
13
- '\W' => '[^\p{Latin}\d_]'
14
- }.freeze
15
-
16
- # Regexp required methods
17
- RE_RE_METHS = %i(match scan).freeze
18
-
19
- # Regexp optional methods
20
- RE_OP_METHS = %i(
21
- []
22
- []=
23
- =~
24
- index
25
- rindex
26
- partition
27
- rpartition
28
- slice
29
- slice!
30
- split
31
- sub
32
- sub!
33
- gsub
34
- gsub!
35
- ).freeze
36
-
37
- CASE_RELATED_METHS = %i(
38
- downcase
39
- downcase!
40
- upcase
41
- upcase!
42
- capitalize
43
- capitalize!
44
- ).freeze
45
-
46
- RANGE_REGEXP = /\[[^\]]*?([#{ALPHABET}]-[#{ALPHABET}])[^\[]*?\]/
47
-
48
- CONJUCTIONS = %w(ve ile veya).freeze
49
-
50
- SPECIAL_CHARS = %q{("'}.freeze
9
+ ASCII_ALPHABET = ALPHABET.chars.map.with_index { |ch, i| [ch, i + 65] }.to_h
10
+ META_CHARS = { '\w' => '[\p{Latin}\d_]', '\W' => '[^\p{Latin}\d_]' }.freeze
11
+
12
+ CASE_RELATED_METHS = %i[downcase
13
+ downcase!
14
+ upcase
15
+ upcase!
16
+ capitalize
17
+ capitalize!].freeze
18
+
19
+ REGEX_REQUIRED_METHODS = %i[match scan].freeze
20
+ REGEX_OPTIONAL_METHODS = %i[[]
21
+ []=
22
+ index
23
+ rindex
24
+ partition
25
+ rpartition
26
+ slice
27
+ slice!
28
+ split
29
+ sub
30
+ sub!
31
+ gsub
32
+ gsub!].freeze
33
+ REGEX_METHS = REGEX_REQUIRED_METHODS + REGEX_OPTIONAL_METHODS
34
+ RANGE_REGEXP = /\[[^\]]*?([#{ALPHABET}]-[#{ALPHABET}])[^\[]*?\]/.freeze
35
+
36
+ CONJUCTION = %w[ve ile veya].freeze
37
+ SPECIAL_CHARS = %q{("'}
51
38
  end
@@ -6,7 +6,7 @@ module TurkishSupportHelpers
6
6
 
7
7
  while re.match(RANGE_REGEXP)
8
8
  re.scan(RANGE_REGEXP).flatten.compact.each do |matching|
9
- re.gsub! matching, translate_range(matching, pattern.casefold?)
9
+ re.gsub! matching, translate_range(matching, casefold: pattern.casefold?)
10
10
  end
11
11
  end
12
12
 
@@ -14,38 +14,28 @@ module TurkishSupportHelpers
14
14
  Regexp.new(re.force_encoding('UTF-8'), Regexp::FIXEDENCODING | options)
15
15
  end
16
16
 
17
- def translate_range(range_as_string, casefold = false)
17
+ def translate_range(range_as_string, casefold: false)
18
18
  return '' unless range_as_string
19
19
 
20
- range_as_string.gsub!(/\[\]/, '')
20
+ range_as_string = range_as_string.gsub(/\[\]/, '')
21
21
  first, last = range_as_string.split('-')
22
-
23
22
  expand_range(first, last, casefold)
24
23
  end
25
24
 
26
- def prepare_for(meth, string)
27
- valid_meths = %i(upcase downcase capitalize)
28
- unless valid_meths.include?(meth) && string.is_a?(String)
29
- raise ArgumentError, 'Invalid arguments for method `prepare_for`!'
30
- end
31
-
32
- method("prepare_for_#{meth}").call(string)
25
+ def tr_char?(char)
26
+ tr_lower?(char) || tr_upper?(char)
33
27
  end
34
28
 
35
- def tr_char?(ch)
36
- tr_lower?(ch) || tr_upper?(ch)
29
+ def tr_lower?(char)
30
+ ALPHA[:tr_lower].include? char
37
31
  end
38
32
 
39
- def tr_lower?(ch)
40
- ALPHA[:tr_lower].include? ch
41
- end
42
-
43
- def tr_upper?(ch)
44
- ALPHA[:tr_upper].include? ch
33
+ def tr_upper?(char)
34
+ ALPHA[:tr_upper].include? char
45
35
  end
46
36
 
47
37
  def conjuction?(string)
48
- CONJUCTIONS.include? string
38
+ CONJUCTION.include? string
49
39
  end
50
40
 
51
41
  def start_with_a_special_char?(string)
@@ -54,21 +44,6 @@ module TurkishSupportHelpers
54
44
 
55
45
  private
56
46
 
57
- def prepare_for_upcase(string)
58
- string.tr(ALPHA[:tr_lower], ALPHA[:tr_upper])
59
- end
60
-
61
- def prepare_for_downcase(string)
62
- string.tr(ALPHA[:tr_upper], ALPHA[:tr_lower])
63
- end
64
-
65
- def prepare_for_capitalize(string)
66
- [
67
- prepare_for(:upcase, string.chr).upcase,
68
- prepare_for(:downcase, self[1..-1]).downcase
69
- ].join
70
- end
71
-
72
47
  def expand_range(first, last, casefold)
73
48
  if lower.include?(first) && lower.include?(last)
74
49
  downcase_range(first, last, casefold)
@@ -91,6 +66,7 @@ module TurkishSupportHelpers
91
66
 
92
67
  def lower(first = nil, last = nil)
93
68
  return ALPHA[:lower] if first.nil? || last.nil?
69
+
94
70
  ALPHA[:lower][ALPHA[:lower].index(first)..ALPHA[:lower].index(last)]
95
71
  end
96
72
 
@@ -100,6 +76,7 @@ module TurkishSupportHelpers
100
76
 
101
77
  def upper(first = nil, last = nil)
102
78
  return ALPHA[:upper] if first.nil? || last.nil?
79
+
103
80
  ALPHA[:upper][ALPHA[:upper].index(first)..ALPHA[:upper].index(last)]
104
81
  end
105
82
 
@@ -1,50 +1,46 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module TurkishSupport
2
- refine String do
3
- (RE_RE_METHS + RE_OP_METHS).each do |meth|
4
+ refine String do # rubocop:disable Metrics/BlockLength
5
+ REGEX_METHS.each do |meth|
4
6
  define_method meth do |*args|
5
- extend(TurkishSupportHelpers)
6
-
7
- if RE_RE_METHS.include?(meth) || args[0].is_a?(Regexp)
8
- args[0] = translate_regexp(args[0])
9
- end
7
+ extend TurkishSupportHelpers
10
8
 
11
- super(*args)
9
+ args[0] = translate_regexp(args[0]) if REGEX_REQUIRED_METHODS.include?(meth) || args[0].is_a?(Regexp)
10
+ instance_exec { super(*args) }
12
11
  end
13
12
  end
14
13
 
15
14
  CASE_RELATED_METHS.each do |meth|
16
- non_destructive = meth.to_s.chomp('!').to_sym
17
- define_method(meth) do
18
- extend(TurkishSupportHelpers)
19
- str = prepare_for(non_destructive, self).public_send(non_destructive)
20
- return meth.to_s.end_with?('!') ? public_send(:replace, str) : str
21
- end
15
+ define_method(meth) { super(:turkic) }
22
16
  end
23
17
 
24
- def titleize(conjuctions = true) # rubocop:disable Metrics/AbcSize
25
- split.map do |word|
26
- word.downcase!
27
- if conjuction?(word) && !conjuctions
18
+ def titleize(conjuction: true)
19
+ extend TurkishSupportHelpers
20
+
21
+ downcase.split.map do |word|
22
+ if !conjuction && conjuction?(word)
28
23
  word
29
24
  elsif start_with_a_special_char?(word)
30
- word.size > 1 ? word[0] + word[1..-1].capitalize : word.chr
25
+ word.chr + (word.length > 1 ? word[1..].capitalize : '')
31
26
  else
32
27
  word.capitalize
33
28
  end
34
29
  end.join(' ')
35
30
  end
36
31
 
37
- def titleize!(conjuctions = true)
38
- replace(titleize(conjuctions))
32
+ def titleize!(conjuction: true)
33
+ replace(titleize(conjuction: conjuction))
39
34
  end
40
35
 
41
36
  def swapcase
42
- extend(TurkishSupportHelpers)
43
- chars.map do |ch|
44
- if tr_char?(ch)
45
- tr_lower?(ch) ? ch.upcase : ch.downcase
37
+ extend TurkishSupportHelpers
38
+
39
+ chars.map do |c|
40
+ if tr_char?(c)
41
+ tr_lower?(c) ? c.upcase : c.downcase
46
42
  else
47
- ch.public_send(:swapcase)
43
+ c.instance_exec { super }
48
44
  end
49
45
  end.join
50
46
  end
@@ -53,8 +49,26 @@ module TurkishSupport
53
49
  replace(swapcase)
54
50
  end
55
51
 
56
- def casecmp(other_string)
57
- upcase.public_send(:casecmp, other_string.upcase)
52
+ def casecmp(other)
53
+ upcase.instance_exec { super(other.upcase) }
54
+ end
55
+
56
+ def <=>(other)
57
+ return nil unless other.is_a? String
58
+
59
+ each_char.with_index do |ch, i|
60
+ position1 = ASCII_ALPHABET[ch]
61
+ position2 = ASCII_ALPHABET[other[i]]
62
+
63
+ return (position2.nil? ? 0 : -1) if position1.nil?
64
+ return 1 if position2.nil?
65
+ return (position1 < position2 ? -1 : 1) if position1 != position2
66
+ end
67
+
68
+ return 0 if length == other.length
69
+ return -1 if length < other.length
70
+
71
+ 1
58
72
  end
59
73
  end
60
74
  end