Sha256: 8d2d2f37dd56a514f63955971ba3d8615a590d65f7fdcc0b513244c9016926f1

Contents?: true

Size: 1.14 KB

Versions: 9

Compression:

Stored size: 1.14 KB

Contents

module ActiveData
  module Model
    module Attributes
      class Localized < Attribute
        def read
          @value ||= Hash[read_before_type_cast.map do |locale, value|
            [locale.to_s, normalize(enumerize(typecast(value)))]
          end]
        end

        def read_before_type_cast
          @value_before_type_cast ||= Hash[(@value_cache.presence || {}).map do |locale, value|
            [locale.to_s, defaultize(value)]
          end]
        end

        def write_locale(value, locale)
          pollute do
            write(read.merge(locale.to_s => value))
          end
        end

        def read_locale(locale)
          read[owner.class.fallbacks(locale).detect do |fallback|
            read[fallback.to_s]
          end.to_s]
        end

        def read_locale_before_type_cast(locale)
          read_before_type_cast[owner.class.fallbacks(locale).detect do |fallback|
            read_before_type_cast[fallback.to_s]
          end.to_s]
        end

        def locale_query(locale)
          value = read_locale(locale)
          !(value.respond_to?(:zero?) ? value.zero? : value.blank?)
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
active_data-1.2.0 lib/active_data/model/attributes/localized.rb
active_data-1.1.7 lib/active_data/model/attributes/localized.rb
active_data-1.1.6 lib/active_data/model/attributes/localized.rb
active_data-1.1.5 lib/active_data/model/attributes/localized.rb
active_data-1.1.4 lib/active_data/model/attributes/localized.rb
active_data-1.1.3 lib/active_data/model/attributes/localized.rb
active_data-1.1.2 lib/active_data/model/attributes/localized.rb
active_data-1.1.1 lib/active_data/model/attributes/localized.rb
active_data-1.1.0 lib/active_data/model/attributes/localized.rb