Sha256: c48ac8b7cf97410f033d5f51c5f668c2199071088bcbe46854454c83de56fc67

Contents?: true

Size: 969 Bytes

Versions: 3

Compression:

Stored size: 969 Bytes

Contents

# encoding: utf-8

module Corrector

  # Stores a list of translations (words or phrases).
  class Base < ActiveRecord::Base

    validates :from, :to, :scope, presence: true
    validates :from, uniqueness: { scope: :scope }, allow_nil: true

    # Selects records in a requested scope.
    #
    # @example
    #   Base.by_scope "UNITS" # Returns all units translations
    #
    # Params:
    # +value+:: the scope to select records in
    #
    # Returns an <tt>ActiveRecord::Association</tt> object.
    scope :by_scope, ->(value) { where scope: value }

    # Scans the dictionary for source string translation.
    # Returns the source if no translation found.
    #
    # @example
    #   Base.scan "КИЛОМЕТР" # => "КМ"
    #
    # Params:
    # +value+:: a string to be scanned
    #
    # Returns a translation or the source itself if no translation found.
    def self.scan(value)
      where(from: value).first.try(:to) || value
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
corrector-0.0.3 app/models/corrector/base.rb
corrector-0.0.2 app/models/corrector/base.rb
corrector-0.0.1 app/models/corrector/base.rb