Project

russial

0.0
No commit activity in last 3 years
No release in over 3 years
Get a simple way for case inflection.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.13
~> 0.7.0
~> 0.10.4
~> 10.0
~> 3.0
~> 0.12.0
 Project Readme

Russial Gem Version Build Status

An easy way to make word declension.

Installation

Add this line to your application's Gemfile:

gem "russial"

And then execute:

$ bundle

Or install it yourself as:

$ gem install russial

Note: for Rubies less than 2.3.0, please use the backport_dig gem. But it's better to upgrade your Ruby version to the last one.

Usage

The Russial gem automatically works with any Hash-like structures that you pass as dictionary. You can use it with plain Ruby or I18n/Rails. Keys must be symbols.

Plain Ruby

With plain Ruby it looks like:

ruby_dictionary = {
  ruby: {
    singular: {
      nominative: "рубин",
      genitive: "рубина",
      dative: "рубину",
      accusative: "рубин",
      instrumental: "рубином",
      prepositional: "рубине"
    },
    plural: {
      nominative: "рубины",
      genitive: "рубинов",
      dative: "рубинам",
      accusative: "рубины",
      instrumental: "рубинами",
      prepositional: "рубинах"
    }
  }   
}

word = Russial.new("ruby", dictionary: ruby_dictionary)

# Default state is singular
word.dative # "рубину"

# Save state
plural_word = word.plural

# Get cases of plural form
plural_word.genitive # "рубинов"
plural_word.instrumental # "рубинами"

With I18n/Rails

With I18n/Rails support you need to add dictionary to locales. For example in file russial.yml:

ru:
  russial:
    идти:
      future: пройдёт
      past:
        singular:
          male: прошёл
          female: прошла
        plural: прошли
      present: проходит

And then:

# Rails wiil do this automatically
I18n.tap do |c|
  c.available_locales = [:ru]
  c.locale = :ru
  c.load_path = ["russial.yml"]
end

word = Russial.new("идти")

word.future # "пройдёт"
word.reset.past.singular.male # "прошёл"
word.reset.past.singular.female # "прошла"

Substitutions

With this feature you can use the gem more advanced way. See example:

flat_dictionary = {
  "многокомнатная квартира": { 
    nominative: "___комнатная квартира",
    genitive: "___комнатной квартиры",
    dative: "___комнатной квартире",
    accusative: "___комнатную квартиру",
    instrumental: "___комнатной квартирой",
    prepositional: "___комнатной квартире"
  }
}
substitutions = { "___" => "трёх" }

word = Russial.new("многокомнатная квартира", dictionary: flat_dictionary, substitutions: substitutions)

word.dative # "трёхкомнатной квартире"
word.instrumental # "трёхкомнатной квартирой"

Shortcut

Instead of using full form of instantiating, you can use shortcut:

Russial("ruby")

Settings

You can configure this gem. Add settings to initializers.

Aliases

Add shorthands for any keys you want.

Russial.configure do |c|
  c.aliases = {
    n: :nominative,
    g: :genitive,
    d: :dative,
    a: :accusative,
    i: :instrumental,
    p: :prepositional
  }.freeze
end

And then:

word = Russial.new("ruby", dictionary: ruby_dictionary)

word.d # "рубину"
word.plural.g # "рубинов"
word.reset.plural.i # "рубинами"

Scope for I18n

Change the root scope for I18n search.

Russial.configure do |c|
  c.i18n_scope = "russial"
end

Default is russial.

Contribute

Feel free to add pull requests and issues.

License

See LICENSE for license information.