class Locomotive::Steam::Liquid::Drops::MetafieldsNamespace

Public Instance Methods

liquid_method_missing(meth) click to toggle source
# File lib/locomotive/steam/liquid/drops/metafields.rb, line 13
def liquid_method_missing(meth)
  find_value(meth.to_s)
end
namespace=(namespace) click to toggle source
# File lib/locomotive/steam/liquid/drops/metafields.rb, line 17
def namespace=(namespace)
  @namespace = namespace
end

Protected Instance Methods

fields() click to toggle source
# File lib/locomotive/steam/liquid/drops/metafields.rb, line 53
def fields
  return @fields if @fields

  (@fields = {}).tap do
    (@namespace['fields'] || []).each do |field|
      @fields[field['name']] = field
    end
  end
end
find_value(name) click to toggle source
# File lib/locomotive/steam/liquid/drops/metafields.rb, line 23
def find_value(name)
  if field = fields[name]
    safe_value(t(values[name], field['localized']), field['type'])
  else
    Locomotive::Common::Logger.warn "[Liquid template] unknown site metafield \"#{name}\" under #{@namespace['name']}"
    nil
  end
end
labels_and_values() click to toggle source
# File lib/locomotive/steam/liquid/drops/metafields.rb, line 36
def labels_and_values
  return [] if @namespace['fields'].blank?

  return @labels_and_values if @labels_and_values

  ordered_fields = @namespace['fields'].sort { |a, b| a['position'] <=> b['position'] }

  @labels_and_values = ordered_fields.map do |field|
    value, localized = values[field['name']], field['localized']
    {
      'name'  => field['name'],
      'label' => t(field['label']) || field['name'].humanize,
      'value' => safe_value(t(value, localized))
    }
  end
end
safe_value(value, type = 'string') click to toggle source
# File lib/locomotive/steam/liquid/drops/metafields.rb, line 69
def safe_value(value, type = 'string')
  case type
  when 'boolean'
    ['1', 'true', true].include?(value) ? true : false
  else
    value.blank? ? nil : value
  end
end
t(value, localized = true) click to toggle source
# File lib/locomotive/steam/liquid/drops/metafields.rb, line 63
def t(value, localized = true)
  key   = localized ? @context.registers[:locale] : 'default'
  value = { 'default' => value } unless value.is_a?(Hash)
  value[key]
end
values() click to toggle source
# File lib/locomotive/steam/liquid/drops/metafields.rb, line 32
def values
  @_source.metafields[@namespace['name']] || {}
end