module Translates

Constants

VERSION

Public Instance Methods

translates(*attributes, backend:, plugins: []) click to toggle source
# File lib/translates.rb, line 4
def translates(*attributes, backend:, plugins: [])
  backend_subclass = Class.new(backend)
  plugins.each { |plugin| backend_subclass.include plugin }

  attributes.each do |attribute|
    define_accessor(attribute)
    define_backend(attribute, backend_subclass)
  end
  backend_subclass.setup_model(self, attributes)
end

Private Instance Methods

define_accessor(attribute) click to toggle source
# File lib/translates.rb, line 24
def define_accessor(attribute)
  define_method(attribute) do
    send("#{attribute}_backend").read(I18n.locale)
  end

  define_method("#{attribute}=") do |value|
    send("#{attribute}_backend").write(I18n.locale, value)
  end
end
define_backend(attribute, backend_class) click to toggle source
# File lib/translates.rb, line 17
def define_backend(attribute, backend_class)
  define_method "#{attribute}_backend" do
    @backends ||= {}
    @backends[attribute] ||= backend_class.new(self, attribute)
  end
end