Permalink
Browse files

Symbolize keys read from YAML

  • Loading branch information...
1 parent 76f76cd commit fda43bffec8fe9696f2e897e9cc3e5e1504d3213 @bdimcheff committed Jan 23, 2009
Showing with 14 additions and 5 deletions.
  1. +12 −3 glug.rb
  2. +2 −2 spec/glug_spec.rb
View
15 glug.rb
@@ -8,6 +8,15 @@
require 'metaid'
#require 'grit'
+class Hash
+ def symbolize_keys
+ inject({}) do |options, (key, value)|
+ options[(key.to_sym rescue key) || key] = value
+ options
+ end
+ end
+end
+
module Glug
class PageNotFound < Sinatra::NotFound
@@ -59,8 +68,8 @@ def basedir
def page_attr_accessor(*syms)
syms.each do |sym|
- define_method(sym) { attributes[sym.to_s] }
- define_method("#{sym}=") { |v| attributes[sym.to_s] = v }
+ define_method(sym) { attributes[sym] }
+ define_method("#{sym}=") { |v| attributes[sym] = v }
end
end
end
@@ -109,7 +118,7 @@ def initialize(raw_content = '')
# Returns nothing
def transform(raw_data)
if self.content =~ /\A(---.*?)---(.*)/m
- self.attributes = YAML.load($1)
+ self.attributes = YAML.load($1).symbolize_keys
self.content = $2
end
end
View
@@ -74,8 +74,8 @@
page.content.strip.should == 'text content'
page.attributes.size.should == 2
- page.attributes['foo'].should == 'bar'
- page.attributes['baz'].should == ['quux']
+ page.attributes[:foo].should == 'bar'
+ page.attributes[:baz].should == ['quux']
end
end

0 comments on commit fda43bf

Please sign in to comment.