Sha256: 2e53e6781f9a72d6b5e510c60bf24c42e1992d96122afa87bf3cb9a413b6ab6f

Contents?: true

Size: 1.35 KB

Versions: 19

Compression:

Stored size: 1.35 KB

Contents

module Kuhsaft
  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

19 entries across 19 versions & 2 rubygems

Version Path
kuhsaft-2.6.3 lib/kuhsaft/translatable.rb
kuhsaft-2.6.2 lib/kuhsaft/translatable.rb
kuhsaft-2.6.1 lib/kuhsaft/translatable.rb
kuhsaft-2.5.2 lib/kuhsaft/translatable.rb
kuhsaft-2.6.0 lib/kuhsaft/translatable.rb
kuhsaft-2.5.1 lib/kuhsaft/translatable.rb
kuhsaft-2.5.0 lib/kuhsaft/translatable.rb
qbrick-2.5.0.pre lib/kuhsaft/translatable.rb
kuhsaft-2.4.3 lib/kuhsaft/translatable.rb
kuhsaft-2.4.2 lib/kuhsaft/translatable.rb
kuhsaft-2.4.1 lib/kuhsaft/translatable.rb
kuhsaft-2.4.0 lib/kuhsaft/translatable.rb
kuhsaft-2.3.6 lib/kuhsaft/translatable.rb
kuhsaft-2.3.5 lib/kuhsaft/translatable.rb
kuhsaft-2.3.4 lib/kuhsaft/translatable.rb
kuhsaft-2.3.3 lib/kuhsaft/translatable.rb
kuhsaft-2.3.2 lib/kuhsaft/translatable.rb
kuhsaft-2.3.1 lib/kuhsaft/translatable.rb
kuhsaft-2.3.0 lib/kuhsaft/translatable.rb