Sha256: e56be536677c2dfe435e8f68e01fffab9fb645d94cb3286336eee7cd7a829ee0

Contents?: true

Size: 1.59 KB

Versions: 7

Compression:

Stored size: 1.59 KB

Contents

module Locomotive::Steam
  module Models

    class I18nField

      extend Forwardable

      def_delegators :@translations, :values, :default

      attr_reader :name, :translations

      def initialize(name, translations)
        @name = name
        self.translations = translations
      end

      def initialize_copy(field)
        super
        self.translations = field.translations.dup
      end

      def [](locale)
        @translations[locale]
      end

      def []=(locale, value)
        @translations[locale] = value
      end

      def translations=(translations)
        @translations = (if translations.respond_to?(:fetch)
          translations
        else
          Hash.new(translations)
        end).with_indifferent_access
      end

      def each(&block)
        @translations.each(&block)
      end

      def blank?
        @translations.default.blank? && (
          @translations.blank? ||
          @translations.values.all? { |v| v.blank? }
        )
      end

      def apply(&block)
        if default
          @translations = Hash.new(yield(default))
        else
          each do |l, _value|
            self[l] = block.arity == 2 ? yield(_value, l) : yield(_value)
          end
        end
        self
      end

      def duplicate(new_name)
        self.class.new(new_name, self.translations)
      end

      alias :__translations__ :translations

      alias :to_hash :translations

      def serialize(attributes, custom_name = nil)
        attributes[custom_name || @name] = @translations
      end

      def to_json
        to_hash.to_json
      end

    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
locomotivecms_steam-1.8.0.alpha2 lib/locomotive/steam/models/i18n_field.rb
locomotivecms_steam-1.8.0.alpha1 lib/locomotive/steam/models/i18n_field.rb
locomotivecms_steam-1.7.1 lib/locomotive/steam/models/i18n_field.rb
locomotivecms_steam-1.7.0 lib/locomotive/steam/models/i18n_field.rb
locomotivecms_steam-1.6.1 lib/locomotive/steam/models/i18n_field.rb
locomotivecms_steam-1.6.0 lib/locomotive/steam/models/i18n_field.rb
locomotivecms_steam-1.6.0.rc1 lib/locomotive/steam/models/i18n_field.rb