lib/mbrao/content_interface.rb in mbrao-1.5.0 vs lib/mbrao/content_interface.rb in mbrao-1.6.0
- old
+ new
@@ -17,22 +17,22 @@
"%F %T %z", "%F %T %Z",
"%F %T.%L %z", "%F %T.%L %Z",
"%F %T.%L", "%F %T", "%F %H:%M", "%F",
"%d/%m/%Y %T.%L", "%d/%m/%Y %T", "%d/%m/%Y %H:%M", "%d/%m/%Y"
- ]
+ ].freeze
# Class methods.
module ClassMethods
# Validates locales for attribute retrieval.
#
# @param locales [Array] A list of desired locales for an attribute. Can include `*` to match all. If none are specified, the default mbrao locale will be
# used.
# @param content [Content|nil] An optional content to check for availability
# @return [Array] The validated list of locales.
def validate_locales(locales, content = nil)
- locales = locales.ensure_array(nil, true, true, true) { |l| l.ensure_string.strip }
+ locales = locales.ensure_array(no_duplicates: true, compact: true, flatten: true) { |l| l.ensure_string.strip }
locales = (locales.empty? ? [Mbrao::Parser.locale] : locales)
raise Mbrao::Exceptions::UnavailableLocalization if content && !content.enabled_for_locales?(locales)
locales
end
@@ -52,11 +52,11 @@
# Checks if the content is available for at least one of the provided locales.
#
# @param locales [Array] The desired locales. Can include `*` to match all. If none are specified, the default mbrao locale will be used.
# @return [Boolean] `true` if the content is available for at least one of the desired locales, `false` otherwise.
def enabled_for_locales?(*locales)
- locales = locales.flatten.ensure_array(nil, false, false, true) { |l| l.ensure_string.strip }.reject { |l| l == "*" }
+ locales = locales.flatten.ensure_array(flatten: true) { |l| l.ensure_string.strip }.reject { |l| l == "*" }
@locales.blank? || locales.blank? || (@locales & locales).present?
end
# Gets the title of the content in the desired locales.
#
@@ -108,16 +108,11 @@
::Mbrao::Parser.as_json(self, keys, options)
end
private
- # Filters an attribute basing a set of locales.
- #
- # @param attribute [Object|HashWithIndifferentAccess] The desired attribute.
- # @param locales [String|Array] The desired locales. Can include `*` to match all. If none are specified, the default mbrao locale will be used.
- # @return [String|HashWithIndifferentAccess] Return the object for desired locales. If only one locale is available, then only a object is returned,
- # else a `HashWithIndifferentAccess` with locales as keys.
+ # :nodoc:
def filter_attribute_for_locales(attribute, locales)
locales = ::Mbrao::Content.validate_locales(locales, self)
if attribute.is_a?(HashWithIndifferentAccess)
rv = attribute.reduce(HashWithIndifferentAccess.new) { |a, e| append_value_for_locale(a, e[0], locales, e[1]) }
@@ -125,17 +120,10 @@
else
attribute
end
end
- # Adds an value on a hash if enable for requested locales.
- #
- # @param hash [Hash] The hash to handle.
- # @param locale [String] The list of locale (separated by commas) for which the value is available.
- # @param locales [Array] The list of locale for which this value is requested. Can include `*` to match all. If none are specified, the default mbrao
- # locale will be used.
- # @param value [Object] The value to add.
- # @return [Hash] Return the original hash.
+ # :nodoc:
def append_value_for_locale(hash, locale, locales, value)
locale.split(/\s*,\s*/).map(&:strip).each do |l|
hash[l] = value if locales.include?("*") || locales.include?(l)
end