Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Check it here is some old school code, rescued from the depths

  • Loading branch information...
commit ea9e6181425dedc5d958ce1ca1f660c0f157d259 1 parent 2bbc2aa
@xaviershay authored
View
14 yaml_helper/README
@@ -0,0 +1,14 @@
+= yaml_helper
+
+Adds transient fields and post-deserialization hooks to yaml4r, the built in
+Ruby YAML serializer
+
+== Getting Started
+
+1. Check example.rb for simplest usage
+2. See http://rhnh.net/2006/06/25/yaml-tutorial for a much better introduction
+
+== Meta
+Author: Xavier Shay (xavier@rhnh.net)
+Revisions:
+ 0.2 (2006-06-14) - Initial public release
View
13 yaml_helper/Rakefile
@@ -0,0 +1,13 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the navigator plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
View
36 yaml_helper/example.rb
@@ -0,0 +1,36 @@
+require 'lib/rhnh/yaml_helper'
+
+class BeefCake
+ attr_reader :ingredients
+ persistent :ingredients
+
+ def initialize
+ @ingredients = ["Beef", "Flour", "More Beef", "Egg"]
+ @result = nil
+ end
+
+ def mix!
+ @result = self.ingredients.join
+ end
+
+ def result
+ @result ? @result : 'Beef cake has not been mixed yet!'
+ end
+
+ def post_deserialize
+ puts "!!! Beef Cake got loaded !!!"
+ end
+end
+
+puts "Let's make a Beef Cake!"
+puts
+cake = BeefCake.new
+puts "Ingredients: " + cake.ingredients.join(", ")
+puts "Result: " + cake.result
+puts " > Mixing..."
+cake.mix!
+puts "Result: " + cake.result
+puts " > YAMLizing and Restoring"
+cake = YAML::load(cake.to_yaml)
+puts "Result: " + cake.result
+puts " > Result was not stored!"
View
96 yaml_helper/lib/rhnh/yaml_helper.rb
@@ -0,0 +1,96 @@
+require 'yaml'
+require 'set'
+
+class Class
+ def persist
+ @persist = [] if !@persist
+ @persist
+ end
+
+ def persist= p
+ @persist = p if p.kind_of?(Array)
+ end
+
+ def persist_with_parent
+ p = []
+ klass = self;
+ while klass
+ p.concat(klass.persist)
+ klass = klass.superclass
+ end
+ p.uniq
+ end
+end
+
+class Object
+ def self.persistent *var
+ for i in (0..var.length-1)
+ var[i] = var[i].to_s
+ end
+ self.persist.concat(var)
+ self.persist.uniq!
+ end
+
+ alias_method :old_to_yaml, :to_yaml
+
+ def to_yaml ( opts = {} )
+ p = self.class.persist_with_parent
+
+ if p && p.size > 0
+ yaml_emit opts do |map|
+ p.each do |m|
+ map.add( m, instance_variable_get( '@' + m ) )
+ end
+ end
+ else
+ old_to_yaml opts
+ end
+ end
+private
+ def yaml_emit opts
+ YAML::quick_emit( object_id, opts ) do |out|
+ out.map( taguri, to_yaml_style ) do |map|
+ yield map
+ end
+ end
+ end
+end
+
+module RHNH
+ module EnumerablePostDeserializeHelper
+ def post_deserialize
+ self.each do |e|
+ YAML.call_post_deserialize(e) if e
+ end
+ end
+ end
+end
+
+class Array
+ include RHNH::EnumerablePostDeserializeHelper
+end
+
+class Hash
+ include RHNH::EnumerablePostDeserializeHelper
+end
+
+
+module YAML
+ def YAML.call_post_deserialize obj, object_map = ::Set.new
+ if !object_map.include?(obj.object_id)
+ object_map.add(obj.object_id)
+
+ obj.instance_variables.each do |v|
+ call_post_deserialize obj.instance_variable_get(v), object_map
+ end
+
+ obj.post_deserialize if obj.respond_to?('post_deserialize')
+ end
+ end
+
+ def YAML.load( io )
+ yp = parser.load( io )
+ call_post_deserialize yp
+ yp
+ end
+end
View
41 yaml_helper/test/types.rb
@@ -0,0 +1,41 @@
+class Basic
+ attr_accessor :a
+end
+
+class Square
+ attr_accessor :width, :height, :area
+ persistent :width, :height
+
+ def calc_area
+ @area = @width * @height
+ end
+end
+
+class SuperSquare < Square
+ attr_accessor :bonus
+ persistent :bonus
+end
+
+class Square2
+ attr_accessor :width, :height, :area
+ persistent :width
+ persistent :height
+end
+
+class Square3
+ attr_accessor :width, :height, :area
+ persistent :width, :height
+
+ def post_deserialize
+ @area = @width * @height
+ end
+end
+
+class SquareCyclic
+ def initialize
+ @me = self
+ end
+
+ def post_deserialize
+ end
+end
View
92 yaml_helper/test/yaml_helper_test.rb
@@ -0,0 +1,92 @@
+require 'test/unit'
+require File.dirname(__FILE__) + '/../lib/rhnh/yaml_helper'
+require File.dirname(__FILE__) + '/types'
+
+class TestYamlHelper < Test::Unit::TestCase
+ # Testing persistent attribute
+ def test_basic
+ obj = Basic.new
+ obj.a = 2
+ s = obj.to_yaml
+ obj2 = YAML::load(s)
+ assert_equal obj.a, obj2.a
+ end
+
+ def check_persist_square obj
+ obj.width = 2
+ obj.height = 3
+ obj.area = 5
+
+ s = obj.to_yaml
+ obj2 = YAML::load(s)
+ assert_equal obj.width, obj2.width
+ assert_equal obj.height, obj2.height
+
+ obj2
+ end
+
+ def test_persist_attribute
+ obj2 = check_persist_square Square.new
+ assert_nil obj2.area
+ end
+
+ def test_persist_multi_attribute
+ obj2 = check_persist_square Square2.new
+ assert_nil obj2.area
+ end
+
+ def test_persist_attribute_inherited
+ obj = SuperSquare.new
+ obj.bonus = "WINNER"
+ obj2 = check_persist_square obj
+
+ assert_equal obj.bonus, obj2.bonus
+ end
+
+ # Testing post_deserialize
+ def test_post_deserialize
+ obj2 = check_persist_square Square3.new
+ assert_equal obj2.height * obj2.width, obj2.area
+ end
+
+ def init_squares num
+ arr = []
+ for i in (0..num)
+ sqr = Square3.new
+ sqr.width = 2 + (i * 2)
+ sqr.height = 3 + (i * 2)
+ arr.push(sqr)
+ end
+ arr
+ end
+
+ def test_array_post_deserialize
+ sqr, sqr2 = init_squares(2)
+
+ s = [sqr, sqr2, nil].to_yaml
+ obj2 = YAML::load(s)
+ for obj in obj2
+ assert_equal obj.width * obj.height, obj.area if obj
+ end
+ end
+
+ def test_hash_post_deserialize
+ sqr, sqr2 = init_squares(2)
+
+ s = { :one => sqr, :two => sqr2, :three => nil }.to_yaml
+ obj2 = YAML::load(s)
+ for obj in obj2.values
+ assert_equal obj.width * obj.height, obj.area if obj
+ end
+ end
+
+ def test_cyclic_post_deserialize
+ sqr = SquareCyclic.new
+ s = sqr.to_yaml
+ begin
+ obj = YAML::load(s)
+ rescue
+ flunk "Infinite loop"
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.