Sha256: 74ba12eac6edd62dc25ae0da999a591d92a127571565a6b74802bda53fe9a0e7

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

module Qbrick
  module Translatable
    def self.included(base)
      base.extend ClassMethods
    end

    extend self

    module ClassMethods
      def translate(*args)
        args.each do |attr_name|
          define_localized_attr_getter attr_name
          define_localized_attr_setter attr_name
          define_localized_attr_finder attr_name
          define_localized_attr_predicate_method attr_name
        end
      end

      def define_localized_attr_getter(attr_name)
        define_method attr_name do
          send "#{attr_name}_#{locale_for_attr_name}"
        end
      end

      def define_localized_attr_predicate_method(attr_name)
        define_method "#{attr_name}?" do
          send "#{attr_name}_#{locale_for_attr_name}?"
        end
      end

      def define_localized_attr_setter(attr_name)
        define_method "#{attr_name}=" do |val|
          send "#{attr_name}_#{locale_for_attr_name}=", val
        end
      end

      def define_localized_attr_finder(attr_name)
        define_singleton_method "find_by_#{attr_name}" do |val|
          send "find_by_#{attr_name}_#{locale_for_attr_name}", val
        end
      end

      def locale_for_attr_name
        I18n.locale.to_s.underscore
      end

      def locale_attr(attr_name)
        "#{attr_name}_#{I18n.locale.to_s.underscore}"
      end
    end

    include ClassMethods
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
qbrick-2.5.2 lib/qbrick/translatable.rb
qbrick-2.5.1 lib/qbrick/translatable.rb
qbrick-2.5.0 lib/qbrick/translatable.rb