Class: Amber::StaticPage::PageProperties

Inherits:
Object
  • Object
show all
Defined in:
lib/amber/static_page/page_properties.rb

Instance Method Summary collapse

Constructor Details

#initialize(page = nil) ⇒ PageProperties

Returns a new instance of PageProperties.



24
25
26
27
# File 'lib/amber/static_page/page_properties.rb', line 24

def initialize(page=nil)
  @page = page
  @locales = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object

allows property_set.propname shortcut, assumes default locale



56
57
58
# File 'lib/amber/static_page/page_properties.rb', line 56

def method_missing(method)
  prop(I18n.locale, method)
end

Instance Method Details

#eval(template_string, locale = I18n.default_locale) ⇒ Object

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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

#localesObject

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



117
118
119
# File 'lib/amber/static_page/page_properties.rb', line 117

def locales
  @locales.keys
end

#prop(locale, var_name, inherited = false) ⇒ Object

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



68
69
70
71
72
73
74
75
76
77
78
79
80
# 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) ⇒ Object

like prop_without_inheritance, but defaults to default_locale and tries multiple properties



97
98
99
100
101
102
103
104
105
# 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) ⇒ Object

like prop(), but does not allow inheritance



85
86
87
88
89
90
91
92
# 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) ⇒ Object



107
108
109
110
111
112
# 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