layout_convert 0.0.2

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: 6fb9407c176772d8f7861bd3604d29369cfb3b01
4
+ data.tar.gz: 40f9a6eda1b166cb7f0f513ec854a74e0625082d
5
+ SHA512:
6
+ metadata.gz: 0bf19e8b5724c78fe2823a03624df4327dfa4fa4a04915333907c171622fe0fde5188b6438d90a035b880092efea2d66624a8d45b837080029b617a0557eae6e
7
+ data.tar.gz: af549ca221160455a5c127c0c7ebc316228be0d2c374af31745f87699986f0e119ca778756464d596b5981a16f163a46d53265c2fb6937e5686b55526636ddd7
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ *.swp
24
+ *.swo
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in layout_convert.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Vovan
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # Basic latin2cyrillic layout swapper
2
+
3
+ A String class now has these methods available
4
+ * cyrillic?
5
+ * latin?
6
+ * mixed?
7
+ * cyrillish?
8
+ * latinish?
9
+ * swap_layout
10
+
11
+ [![Gem Version](https://badge.fury.io/rb/layout_convert.svg)](http://badge.fury.io/rb/layout_convert)
12
+ [![Code Climate](https://codeclimate.com/github/bluurn/layout_convert.png)](https://codeclimate.com/github/bluurn/layout_convert)
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc 'Spec all'
4
+
5
+ task :spec_all do
6
+ system "rspec spec/*"
7
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'layout_convert/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "layout_convert"
8
+ spec.version = LayoutConvert::VERSION
9
+ spec.authors = ["bluurn"]
10
+ spec.email = ["bluurn@gmail.com"]
11
+ spec.summary = %q{Basic string Latin <-> Cyrillic Layour swapper }
12
+ spec.description = %q{
13
+ A String class now has these methods available
14
+ * cyrillic?
15
+ * latin?
16
+ * mixed?
17
+ * cyrillish?
18
+ * latinish?
19
+ * swap_layout }
20
+ spec.homepage = ""
21
+ spec.license = "MIT"
22
+
23
+ spec.files = `git ls-files -z`.split("\x0")
24
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
25
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.6"
29
+ spec.add_development_dependency "rake"
30
+ end
@@ -0,0 +1,3 @@
1
+ module LayoutConvert
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,106 @@
1
+ ##
2
+ # Mix-in necessary methods to make layout swap available
3
+
4
+ class ::String
5
+
6
+ LAYOUTS = {
7
+ lat: %q!qwertyuiop[]asdfghjkl;'zxcvbnm,./QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<>?!.scan(/./),
8
+ cyr: %q!йцукенгшщзхъфывапролджэячсмитьбю.ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ,!.scan(/./)
9
+ }
10
+
11
+ LANG = {
12
+ lat: ('a'..'z').to_a + ('A'..'Z').to_a,
13
+ cyr: ('а'..'я').to_a + ('А'..'Я').to_a
14
+ }
15
+
16
+ SPLIT_REGEX =/[a-zA-Zа-яА-Я\[\];',\.\/{}:"<>?]+/
17
+
18
+ ##
19
+ # Detect whether the given string is cyrillic
20
+
21
+ def cyrillic?
22
+ guess_layout.equal?(:cyr)
23
+ end
24
+
25
+ ##
26
+ # Detect whether the given string is latin
27
+ def latin?
28
+ guess_layout.equal?(:lat)
29
+ end
30
+
31
+ ##
32
+ # Detect whether the given string has both latin and cyrillic letters
33
+
34
+ def mixed?
35
+ guess_layout.equal?(:mixed)
36
+ end
37
+
38
+ ##
39
+ # Detect whether the given string has more latin letters
40
+ def latinish?
41
+ latin_count = cyrillic_count = 0
42
+ split_words.each do |word|
43
+ if word.latin?
44
+ latin_count+=1
45
+ else
46
+ cyrillic_count+=1
47
+ end
48
+ end
49
+ latin_count > cyrillic_count
50
+ end
51
+
52
+ ##
53
+ # Detect whether the given string has more cyrillic letters
54
+ def cyrillish?
55
+ !self.latinish?
56
+ end
57
+
58
+ ##
59
+ # Swap the layout
60
+ def swap_layout
61
+ if self.latinish? || self.cyrillish?
62
+ split_words.map do |word|
63
+ layout_map = set_layout word
64
+ word.scan(/./).map do |ch|
65
+ layout_map[ch].nil? ? ch : layout_map[ch]
66
+ end.join
67
+ end.join ' '
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ ##
74
+ # Guess the layout
75
+
76
+ def guess_layout
77
+ letters = self.scan(/[[:alpha:]]/).uniq
78
+ if (letters - LANG[:lat]).empty?
79
+ :lat
80
+ elsif (letters - LANG[:cyr]).empty?
81
+ :cyr
82
+ else
83
+ :mixed
84
+ end
85
+ end
86
+
87
+ ##
88
+ # Split words using tricky regex
89
+
90
+ def split_words
91
+ self.scan SPLIT_REGEX
92
+ end
93
+
94
+ ##
95
+ # Set the layout hash
96
+
97
+ def set_layout(word = self)
98
+ if word.latin?
99
+ Hash[LAYOUTS[:lat].zip LAYOUTS[:cyr]]
100
+ elsif word.cyrillic?
101
+ Hash[LAYOUTS[:cyr].zip LAYOUTS[:lat]]
102
+ else
103
+ throw "Cannot detect layout"
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,105 @@
1
+ require "layout_convert"
2
+
3
+ describe "A magic mixin module" do
4
+ let(:latin_example) { 'hello world' }
5
+ let(:cyrillic_example) { 'здравствуй, мир' }
6
+ let(:mixed_example) { 'здравствуй, мир world' }
7
+
8
+ let(:latin_example_in_cyrillic) { 'руддщ цщкдв' }
9
+ let(:cyrillic_example_in_latin) { 'plhfdcndeq? vbh' }
10
+
11
+ let(:mixed_example_puzzle) { "Z ckeif. njkmrj Кщслфишддн" }
12
+ let(:mixed_example_puzzle_two) { "Вкштл мщвлф фтв здфн ,fkfkfqrf" }
13
+
14
+ let(:mixed_example_puzzle_solution) { "Я слушаю только Rockabilly" }
15
+ let(:mixed_example_puzzle_two_solution) { "Drink vodka and play балалайка" }
16
+
17
+ describe "has the 'guess_layout'" do
18
+ it "should guess the latin layout of the string with latin chars" do
19
+ expect(latin_example.send('guess_layout')).to be_equal :lat
20
+ end
21
+ it "should guess the cyrillic layout of the string with cyrillic chars" do
22
+ expect(cyrillic_example.send('guess_layout')).to be_equal :cyr
23
+ end
24
+ it "should guess the mixed layout of the string with cyrillic and latin chars" do
25
+ expect(mixed_example.send('guess_layout')).to be_equal :mixed
26
+ end
27
+ end
28
+
29
+ describe "has the 'cyrillic?' method" do
30
+ it "should return true if the given string is cyrillic" do
31
+ expect(cyrillic_example.cyrillic?).to be true
32
+ end
33
+
34
+ it "should return false if the given string is latin" do
35
+ expect(latin_example.cyrillic?).to be false
36
+ end
37
+
38
+ it "should return false if the given string is both latin and cyrillic" do
39
+ expect(mixed_example.cyrillic?).to be false
40
+ end
41
+ end
42
+
43
+ describe "has the 'latin?' method" do
44
+ it "should return false if the given string is cyrillic" do
45
+ expect(cyrillic_example.latin?).to be false
46
+ end
47
+
48
+ it "should return true if the given string is latin" do
49
+ expect(latin_example.latin?).to be true
50
+ end
51
+
52
+ it "should return false if the given string is both latin and cyrillic" do
53
+ expect(mixed_example.latin?).to be false
54
+ end
55
+ end
56
+
57
+ describe "has the 'mixed?' method" do
58
+ it "should return true if the given string is cyrillic" do
59
+ expect(cyrillic_example.mixed?).to be false
60
+ end
61
+
62
+ it "should return false if the given string is latin" do
63
+ expect(latin_example.mixed?).to be false
64
+ end
65
+
66
+ it "should return false if the given string is both latin and mixed" do
67
+ expect(mixed_example.mixed?).to be true
68
+ end
69
+ end
70
+
71
+ describe "has the 'latinish?' method" do
72
+ it "should return true if there are more latin letters than cyrillic" do
73
+ expect(mixed_example_puzzle.latinish?).to be true
74
+ end
75
+ it "should return false if there are more cyrillic letters than latin" do
76
+ expect(mixed_example_puzzle_two.latinish?).to be false
77
+ end
78
+ end
79
+
80
+ describe "has the 'cyrillish?' method" do
81
+ it "should return true if there are more cyrillic letters than latin?" do
82
+ expect(mixed_example_puzzle_two.cyrillish?).to be true
83
+ end
84
+ it "should return false if there are more latin letters than cyrillic" do
85
+ expect(mixed_example_puzzle.cyrillish?).to be false
86
+ end
87
+ end
88
+
89
+ describe "has the 'swap_layout' method" do
90
+ it "should convert cyrillic string in latin to cyrillic" do
91
+ expect(cyrillic_example_in_latin.swap_layout).to be_eql cyrillic_example
92
+ end
93
+ it "should convert cyrillic latin in cyrillic to latin" do
94
+ expect(latin_example_in_cyrillic.swap_layout).to be_eql latin_example
95
+ end
96
+ describe "with mixed puzzles" do
97
+ it "should convert latinish puzzle correctly" do
98
+ expect(mixed_example_puzzle.swap_layout).to be_eql mixed_example_puzzle_solution
99
+ end
100
+ it "should convert cyrillish puzzle correctly" do
101
+ expect(mixed_example_puzzle_two.swap_layout).to be_eql mixed_example_puzzle_two_solution
102
+ end
103
+ end
104
+ end
105
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: layout_convert
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - bluurn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: "\nA String class now has these methods available\n* cyrillic?\n* latin?\n*
42
+ mixed?\n* cyrillish?\n* latinish?\n* swap_layout "
43
+ email:
44
+ - bluurn@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .gitignore
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - layout_convert.gemspec
55
+ - lib/layout_convert.rb
56
+ - lib/layout_convert/version.rb
57
+ - spec/layout_convert_spec.rb
58
+ homepage: ''
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.2.2
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Basic string Latin <-> Cyrillic Layour swapper
82
+ test_files:
83
+ - spec/layout_convert_spec.rb