summaryrefslogtreecommitdiff
path: root/lib/property_set.rb
diff options
context:
space:
mode:
authorelijah <elijah@riseup.net>2012-08-24 21:12:36 -0700
committerelijah <elijah@riseup.net>2012-08-24 21:12:36 -0700
commit627f0488e5bd3c31359fc9e78acffbfea4a86a8b (patch)
treef7b14733e314cf82d5fa3e18cbe9d98c2ffe5c7c /lib/property_set.rb
parentb774dea07f97b078fa17e5dcbf901d1c83fed0d6 (diff)
committed website v. 0.1.0
Diffstat (limited to 'lib/property_set.rb')
-rw-r--r--lib/property_set.rb158
1 files changed, 158 insertions, 0 deletions
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 == "<h1>hi</h1>"
+ p ps.body == "this is the body\n"
+end