Sha256: 14c05c78a3e7d0e3cdd981063489dee9d838b4abc3d86517344398b6cec7fda6

Contents?: true

Size: 766 Bytes

Versions: 2

Compression:

Stored size: 766 Bytes

Contents

require "yaml"

module YamlSerialization
  
  def self.included(base)
    base.extend ClassMethods
  end
  
    
  module ClassMethods  
    def load_from_file(file_name)
      YAML.load_file(file_name)
    end
  end
  
  def save_to_file(file_name)
    File.open(file_name, 'w') do |file|
      YAML.dump(self, file)
    end
  end
  
end

class ::Class
  yaml_as "tag:ruby.yaml.org,2002:class"
  
  def Class.yaml_new( klass, tag, val )
    if String === val
      val.split(/::/).inject(Object) {|m, n| m.const_get(n)}
    else
      raise YAML::TypeError, "Invalid Class: " + val.inspect
    end
  end
  
  def to_yaml( opts = {} )
    YAML::quick_emit( nil, opts ) { |out|
      out.scalar( "tag:ruby.yaml.org,2002:class", self.name, :plain )
    }
  end
  
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
danielsdeleo-basset-1.0.4 lib/basset/yaml_serialization.rb
rjspotter-basset-1.0.5 lib/basset/yaml_serialization.rb