From 627f0488e5bd3c31359fc9e78acffbfea4a86a8b Mon Sep 17 00:00:00 2001 From: elijah Date: Fri, 24 Aug 2012 21:12:36 -0700 Subject: committed website v. 0.1.0 --- lib/property_set.rb | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 lib/property_set.rb (limited to 'lib/property_set.rb') diff --git a/lib/property_set.rb b/lib/property_set.rb new file mode 100644 index 0000000..c315a13 --- /dev/null +++ b/lib/property_set.rb @@ -0,0 +1,158 @@ +# +# holds the set or properties defined for each static page. +# property sets are organized by locale. +# +# e.g. +# +# setting the property (in en.haml): +# +# - @title = 'hi' +# +# getting the property +# +# page.props.title +# + +require 'i18n' +require 'time' +require 'rubygems' +require 'haml' +require 'RedCloth' + +class PropertySet + + DEFAULT_LOCALE = 'en' + + # + # a simple class to pass through all member variables as attributes. + # when the template for a page is evaluated, all the member variabled defined in that template + # are loaded as member variables of the AttrObject instance. + # + class AttrObject + def initialize(property_set, locale) + @_ps = property_set + @_locale = locale + #code.gsub!(/^((?!^\-\ \@).)*$/, '') # remove any lines not starting with '- @' + #code.gsub!(/^- /m, '') # remove '-' + #instance_eval(code) + #instance_variables.grep(/_at$/).each do |time_variable| + # instance_variable_set(time_variable, Time.parse(instance_variable_get(time_variable))) + #end + end + def method_missing(method) + get(method) + end + def textile(str) + RedCloth.new(str).to_html + end + def get(var_name) + value = instance_variable_get("@#{var_name}") + if value.nil? + if @_locale != DEFAULT_LOCALE + # try value from default locale + @_ps.get_var(var_name) + else + # try inherited value + @_ps.get_inherited_var(var_name, @_locale) + end + else + value + end + end + def set(var_name, value) + instance_variable_set("@#{var_name}", value) + end + end + + def initialize(page=nil) + @page = page + @locales = {} + end + + + # + # maybe something like this in the future: + # + #contr = PagesController.new() + #contr.response = ActionController::Response.new() + #scope = ActionView::Base.new(["#{RAILS_ROOT}/app/views/pages","#{RAILS_ROOT}/app/views"], {}, contr) + #scope.template_format = 'html' + #Haml::Engine.new(value, :format => :html5).render(scope) + # + + # + # evaluate the template_string, and load the variables defined into an AttrObject. + # + def eval(locale, template_string) + attrs = AttrObject.new(self, locale) + body = nil + begin + body = Haml::Engine.new(template_string, :format => :html5).render(attrs) # template is evaluated in scope of attrs + rescue Exception => exc + # eat exceptions + end + attrs.instance_variable_set('@body', body) + attrs.instance_variables.grep(/_at$/).each do |time_variable| + attrs.instance_variable_set(time_variable, Time.parse(attrs.instance_variable_get(time_variable))) + end + @locales[locale] = attrs + end + + # + # allows property_set.propname shortcut, assumes default locale + # + def method_missing(method) + get_var(method) + end + + def locale(l) + @locales[l.to_s] || @locales[DEFAULT_LOCALE] + end + + def get_var(var_name, locale=I18n.locale) + attrs = locale(locale) + if attrs + attrs.get(var_name) + else + nil + end + end + + # + # tries to get the value of an inherited variable + # + def get_inherited_var(var_name, locale=I18n.locale) + if @page && @page.parent && @page.parent.props + @page.parent.props.get_var(var_name, locale) + end + end +end + +# +# a simple little test. +# +if ARGV.grep('--test').any? + text_en = " +- @title = 'hi' +- @author = 'you' +- @created_at = 'Sun Aug 12 18:32:20 PDT 2012' +- ignored = 1 +this is the body" + + text_es = " +- @title = 'hola' +- @author = 'tu' +- @heading = textile 'h1. hi' +" + + ps = PropertySet.new + ps.eval('en', text_en) + ps.eval('es', text_es) + + p ps.title == 'hi' + p ps.locale(:es).title == 'hola' + p ps.locale(:es).created_at == Time.parse('Sun Aug 12 18:32:20 PDT 2012') + p ps.ignored == nil + p ps.locale(:es).heading == "

hi

" + p ps.body == "this is the body\n" +end -- cgit v1.2.3