class Amber::StaticPage::PageProperties

Public Class Methods

new(page=nil) click to toggle source
# File lib/amber/static_page/page_properties.rb, line 24
def initialize(page=nil)
  @page = page
  @locales = {}
end

Public Instance Methods

eval(template_string, locale=I18n.default_locale) click to toggle source

evaluate the template_string, and load the variables defined into an AttrObject.

# File lib/amber/static_page/page_properties.rb, line 32
def eval(template_string, locale=I18n.default_locale)
  locale = locale.to_sym # locales are always symbols

  # render to the template to get the instance variables
  ps = PropertySet.new
  begin
    # template is evaluated with binding of ps object
    Haml::Engine.new(template_string, :format => :html5).render(ps)
  rescue Exception => exc
    raise exc if defined?(TESTING)
  end

  # convert date/time variables to objects of class Time
  ps.instance_variables.grep(/_at$/).each do |time_variable|
    ps.instance_variable_set(time_variable, Time.parse(ps.instance_variable_get(time_variable)))
  end

  # save the AttrObject
  @locales[locale] = ps
end
locales() click to toggle source

returns an array of locale symbols that are active for this page.

# File lib/amber/static_page/page_properties.rb, line 117
def locales
  @locales.keys
end
method_missing(method) click to toggle source

allows property_set.propname shortcut, assumes default locale

# File lib/amber/static_page/page_properties.rb, line 56
def method_missing(method)
  prop(I18n.locale, method)
end
prop(locale, var_name, inherited=false) click to toggle source

get an attribute value for a particular locale. if `inherited` is true, we do not consider special non-inheritable properties.

# File lib/amber/static_page/page_properties.rb, line 68
def prop(locale, var_name, inherited=false)
  return nil unless locale
  properties = @locales[locale.to_sym]
  value = (properties.get(var_name, inherited) if properties)
  if value.nil? && locale != I18n.default_locale
    properties = @locales[I18n.default_locale]
    value = properties.get(var_name, inherited) if properties
  end
  if value.nil? && @page && @page.parent
    value = @page.parent.prop(locale, var_name,  true)
  end
  value
end
prop_with_fallback(locale, var_names) click to toggle source

like prop_without_inheritance, but defaults to default_locale and tries multiple properties

# File lib/amber/static_page/page_properties.rb, line 97
def prop_with_fallback(locale, var_names)
  [locale, I18n.default_locale].each do |l|
    var_names.each do |var|
      value = prop_without_inheritance(l, var)
      return value if value
    end
  end
  return nil
end
prop_without_inheritance(locale, var_name) click to toggle source

like prop(), but does not allow inheritance

# File lib/amber/static_page/page_properties.rb, line 85
def prop_without_inheritance(locale, var_name)
  properties = @locales[locale.to_sym]
  if properties
    properties.get(var_name)
  else
    nil
  end
end
set_prop(locale, var_name, value) click to toggle source
# File lib/amber/static_page/page_properties.rb, line 107
def set_prop(locale, var_name, value)
  properties = @locales[locale.to_sym]
  if properties
    properties.set(var_name, value)
  end
end