Sha256: 4aba2348b112a68c2d9f07720f161303431d4bd47dcf59b406c1a549ffda5d27

Contents?: true

Size: 981 Bytes

Versions: 193

Compression:

Stored size: 981 Bytes

Contents

module ActiveRecord
  # :stopdoc:
  module Coders
    class YAMLColumn
      RESCUE_ERRORS = [ ArgumentError ]

      if defined?(Psych) && defined?(Psych::SyntaxError)
        RESCUE_ERRORS << Psych::SyntaxError
      end

      attr_accessor :object_class

      def initialize(object_class = Object)
        @object_class = object_class
      end

      def dump(obj)
        YAML.dump obj
      end

      def load(yaml)
        return object_class.new if object_class != Object && yaml.nil?
        return yaml unless yaml.is_a?(String) && yaml =~ /^---/
        begin
          obj = YAML.load(yaml)

          unless obj.is_a?(object_class) || obj.nil?
            raise SerializationTypeMismatch,
              "Attribute was supposed to be a #{object_class}, but was a #{obj.class}"
          end
          obj ||= object_class.new if object_class != Object

          obj
        rescue *RESCUE_ERRORS
          yaml
        end
      end
    end
  end
  # :startdoc
end

Version data entries

193 entries across 162 versions & 15 rubygems

Version Path
mdg-1.0.1 vendor/bundle/ruby/2.3.0/gems/activerecord-3.2.22.5/lib/active_record/coders/yaml_column.rb
activerecord-3.2.22.5 lib/active_record/coders/yaml_column.rb
activerecord-3.2.22.4 lib/active_record/coders/yaml_column.rb
activerecord-3.2.22.3 lib/active_record/coders/yaml_column.rb
activerecord-3.2.22.2 lib/active_record/coders/yaml_column.rb
activerecord-3.2.22.1 lib/active_record/coders/yaml_column.rb
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/coders/yaml_column.rb
activerecord-3.2.22 lib/active_record/coders/yaml_column.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/activerecord-3.2.12/lib/active_record/coders/yaml_column.rb
activerecord-3.2.21 lib/active_record/coders/yaml_column.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.18/lib/active_record/coders/yaml_column.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/coders/yaml_column.rb
apl-library-0.0.90 vendor/bundle/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/coders/yaml_column.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/activerecord-3.2.18/lib/active_record/coders/yaml_column.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/activerecord-3.2.18/lib/active_record/coders/yaml_column.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.18/lib/active_record/coders/yaml_column.rb
activerecord-3.2.20 lib/active_record/coders/yaml_column.rb
activerecord-3.2.19 lib/active_record/coders/yaml_column.rb
activerecord-3.2.18 lib/active_record/coders/yaml_column.rb
activerecord-3.2.17 lib/active_record/coders/yaml_column.rb