russial 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 12a9817811bb18c4db5e3e76a55a30b40314f3cd
4
+ data.tar.gz: 388f5d2fbc99e206752af8149deda298ebb3ebb7
5
+ SHA512:
6
+ metadata.gz: 36bded35eeaee2eefdf0a1f513c5fbaf726b32cab1bebea1abc6450dd9dc8482c74b88160793290706e60936aa2053e44bb13e6b0ce42baf3f725ca2de7ba073
7
+ data.tar.gz: d2ea1c6472e0ecdb0dc4eef1d2ceb9aaf35fc10e74442ccfdf0cb31d63d3587c05071123236a2b500b2bc42c1c83b2aa90f148c277be6efdd46ec3312f735f6b
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ /.idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,12 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ Include:
4
+ - "**/Rakefile"
5
+ - "**/Gemfile"
6
+ - "**/*.gemspec"
7
+
8
+ Style/StringLiterals:
9
+ EnforcedStyle: double_quotes
10
+
11
+ Documentation:
12
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.0
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ - 2.3.0
6
+ - 2.2.0
7
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ source "https://rubygems.org"
3
+
4
+ # Specify your gem's dependencies in russial.gemspec
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Mesto.ru
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,151 @@
1
+ # Russial
2
+
3
+ An easy way to make word declension.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+ ~~~
9
+ gem "russial"
10
+ ~~~
11
+
12
+ And then execute:
13
+ ~~~
14
+ $ bundle
15
+ ~~~
16
+
17
+ Or install it yourself as:
18
+
19
+ ~~~
20
+ $ gem install russial
21
+ ~~~
22
+
23
+ **Note:** for Rubies less than 2.3.0 we use `backport_dig` gem, but we strongly recommend you to upgrade your Ruby version to the last one.
24
+
25
+ ## Usage
26
+
27
+ The Russial gem automatically works with any structures that you pass as dictionary. You can use it with plain Ruby or I18n/Rails.
28
+
29
+ ### Plain Ruby
30
+
31
+ With plain Ruby it's looks like:
32
+
33
+ ~~~
34
+ ruby_dictionary = {
35
+ ruby: {
36
+ singular: {
37
+ nominative: "рубин",
38
+ genitive: "рубина",
39
+ dative: "рубину",
40
+ accusative: "рубин",
41
+ instrumental: "рубином",
42
+ prepositional: "рубине"
43
+ },
44
+ plural: {
45
+ nominative: "рубины",
46
+ genitive: "рубинов",
47
+ dative: "рубинам",
48
+ accusative: "рубины",
49
+ instrumental: "рубинами",
50
+ prepositional: "рубинах"
51
+ }
52
+ }
53
+ }
54
+
55
+ word = Russial.new("ruby", dictionary: ruby_dictionary)
56
+
57
+ # Default state is singular
58
+ word.dative # "рубину"
59
+
60
+ # Save state
61
+ plural_word = word.plural
62
+
63
+ # Get cases of plural form
64
+ plural_word.genitive # "рубинов"
65
+ plural_word.instrumental # "рубинами"
66
+ ~~~
67
+
68
+ ### With I18n/Rails
69
+
70
+ With I18n/Rails support you need to add dictionary to locales. For example in file *`russial.yml`*:
71
+
72
+ ~~~
73
+ ru:
74
+ russial:
75
+ идти:
76
+ future: пройдёт
77
+ past:
78
+ singular:
79
+ male: прошёл
80
+ female: прошла
81
+ plural: прошли
82
+ present: проходит
83
+ ~~~
84
+
85
+ And then:
86
+
87
+ ~~~
88
+ # Rails wiil do this automatically
89
+ I18n.tap do |c|
90
+ c.available_locales = [:ru]
91
+ c.locale = :ru
92
+ c.load_path = ["russial.yml"]
93
+ end
94
+
95
+ word = Russial.new("идти")
96
+
97
+ word.future # "пройдёт"
98
+ word.reset.past.singular.male # "прошёл"
99
+ word.reset.past.singular.female # "прошла"
100
+ ~~~
101
+
102
+ ## Settings
103
+
104
+ You can configure this gem. Add settings to initializers.
105
+
106
+ ### Aliases
107
+
108
+ Add shorthands for any keys you want.
109
+
110
+ ~~~
111
+ Russial.configure do |c|
112
+ c.aliases = {
113
+ n: :nominative,
114
+ g: :genitive,
115
+ d: :dative,
116
+ a: :accusative,
117
+ i: :instrumental,
118
+ p: :prepositional
119
+ }.freeze
120
+ end
121
+ ~~~
122
+
123
+ And then:
124
+
125
+ ~~~
126
+ word = Russial.new("ruby", dictionary: ruby_dictionary)
127
+
128
+ word.d # "рубину"
129
+ word.plural.g # "рубинов"
130
+ word.reset.plural.i # "рубинами"
131
+ ~~~
132
+
133
+ ### Scope for I18n
134
+
135
+ Change the root scope for I18n search.
136
+
137
+ ~~~
138
+ described_class.configure do |c|
139
+ c.i18n_scope = "russial"
140
+ end
141
+ ~~~
142
+
143
+ Default is `russial`.
144
+
145
+ ## Contribute
146
+
147
+ Feel free to add pull requests and issues.
148
+
149
+ ## License
150
+
151
+ See LICENSE for license information.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "russial"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "pry"
15
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/russial.rb ADDED
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+ require "backport_dig" unless {}.respond_to?(:dig)
3
+
4
+ require "russial/version"
5
+ require "russial/config"
6
+ require "russial/config/configuration"
7
+ require "russial/dictionary/default_scope"
8
+ require "russial/dictionary/dynamic_methods"
9
+ require "russial/dictionary/initializer"
10
+ require "russial/dictionary/i18n"
11
+
12
+ class Russial
13
+ include Dictionary::DefaultScope
14
+ include Dictionary::I18n if defined?(I18n)
15
+ prepend Dictionary::DynamicMethods
16
+ prepend Dictionary::Initializer
17
+ extend Config::Configuration
18
+
19
+ def initialize(word, dictionary: {})
20
+ @word = word.to_sym
21
+ @dictionary = prepare_dictionary(dictionary)
22
+ @path = []
23
+ end
24
+
25
+ def reset
26
+ reset_path
27
+ self
28
+ end
29
+
30
+ private
31
+
32
+ attr_reader :dictionary, :word
33
+ attr_accessor :path
34
+
35
+ def soft_reset_path
36
+ path.pop
37
+ end
38
+
39
+ def reset_path
40
+ self.path = []
41
+ end
42
+
43
+ def result
44
+ dictionary.dig(*path) || super
45
+ end
46
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+ class Russial
3
+ class Config
4
+ attr_accessor :aliases, :i18n_scope
5
+
6
+ def initialize
7
+ @aliases = {}
8
+ @i18n_scope = :russial
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ class Russial
3
+ class Config
4
+ module Configuration
5
+ attr_writer :config
6
+
7
+ def config
8
+ @config ||= Config.new
9
+ end
10
+
11
+ def reset
12
+ @config = Config.new
13
+ end
14
+
15
+ def configure
16
+ yield(config)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+ class Russial
3
+ module Dictionary
4
+ module DefaultScope
5
+ def default_scope
6
+ @default_scope ||= keys.first.scope
7
+ end
8
+
9
+ def result
10
+ dictionary.dig(*default_scope, path.last)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+ class Russial
3
+ module Dictionary
4
+ module DynamicMethods
5
+ def initialize(*)
6
+ super
7
+ generate_methods
8
+ generate_aliases
9
+ end
10
+
11
+ private
12
+
13
+ def generate_methods
14
+ keys.each do |key|
15
+ name = key.name
16
+ define_singleton_method(name) do
17
+ path << name unless path.include? name
18
+ get
19
+ end
20
+ end
21
+ end
22
+
23
+ def generate_aliases
24
+ singleton_class.class_eval do
25
+ superclass.config.aliases.each { |a, m| alias_method a, m }
26
+ end
27
+ end
28
+
29
+ def get
30
+ case (memoized_result = result)
31
+ when Hash
32
+ self
33
+ when String
34
+ soft_reset_path
35
+ memoized_result
36
+ end
37
+ end
38
+
39
+ def keys
40
+ @keys ||= extract_keys(dictionary).flatten
41
+ end
42
+
43
+ def extract_keys(hash, scope = [])
44
+ hash.map do |k, v|
45
+ if v.is_a? Hash
46
+ extract_keys(v, scope + [k]) << Key.new(k, scope)
47
+ else
48
+ [Key.new(k, scope)]
49
+ end
50
+ end
51
+ end
52
+
53
+ Key = Struct.new(:name, :scope)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+ require "i18n"
3
+
4
+ class Russial
5
+ module Dictionary
6
+ module I18n
7
+ private
8
+
9
+ def prepare_dictionary(*)
10
+ ::I18n.t(word, scope: self.class.config.i18n_scope)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+ class Russial
3
+ module Dictionary
4
+ module Initializer
5
+ private
6
+
7
+ def prepare_dictionary(dictionary)
8
+ dictionary[word] || super
9
+ rescue NoMethodError
10
+ {}
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ class Russial
3
+ VERSION = "0.5.0"
4
+ end
data/russial.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "russial/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "russial"
9
+ spec.version = Russial::VERSION
10
+ spec.authors = ["Mesto.ru"]
11
+ spec.email = ["jt@mesto.ru"]
12
+
13
+ spec.summary = "Plain interface for word declension."
14
+ spec.description = "Get a simple way for case inflection."
15
+ spec.homepage = "https://github.com/mestoru/russial"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.required_ruby_version = ">= 2.2.0"
26
+
27
+ spec.add_dependency "backport_dig", "0.0.2" if RUBY_VERSION < "2.3.0"
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.13"
30
+ spec.add_development_dependency "i18n", "~> 0.7.0"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ spec.add_development_dependency "pry", "~> 0.10.4"
34
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: russial
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Mesto.ru
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-28 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: i18n
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.7.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.7.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.10.4
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.10.4
83
+ description: Get a simple way for case inflection.
84
+ email:
85
+ - jt@mesto.ru
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".rubocop.yml"
93
+ - ".ruby-version"
94
+ - ".travis.yml"
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - lib/russial.rb
102
+ - lib/russial/config.rb
103
+ - lib/russial/config/configuration.rb
104
+ - lib/russial/dictionary/default_scope.rb
105
+ - lib/russial/dictionary/dynamic_methods.rb
106
+ - lib/russial/dictionary/i18n.rb
107
+ - lib/russial/dictionary/initializer.rb
108
+ - lib/russial/version.rb
109
+ - russial.gemspec
110
+ homepage: https://github.com/mestoru/russial
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: 2.2.0
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.5.1
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Plain interface for word declension.
134
+ test_files: []