Sha256: c54f5ad813f96d30c55c29147ae0ff84bd14312e7f06baec9d34917b1d759309

Contents?: true

Size: 1.18 KB

Versions: 9

Compression:

Stored size: 1.18 KB

Contents

# Author: Stephen Sykes
# http://pennysmalls.com

require 'mysql'

class Mysql::Result; class RowHash; end; end

require 'slim_attrib_ext'

class Mysql::Result
  class RowHash
    def marshal_dump
      to_hash
    end
    
    def marshal_load(hash)
      @real_hash = hash
    end

    alias_method :include?, :has_key?

    def keys
      @real_hash ? @real_hash.keys : @field_indexes.keys
    end

    # If you want to do anything other than [], []=, dup, keys and has_key? then 
    # we'll handle that by doing the operation on a real ruby hash.
    # This should be the exception though, and the efficiencies of using slim-attributes
    # are lost when this happens.
    def to_hash
      return @real_hash if @real_hash
      @real_hash = {}
      @field_indexes.each_pair {|name, index| @real_hash[name] = fetch_by_index(index)}
      @field_indexes = nil
      @real_hash
    end

    def to_a
      to_hash.to_a
    end
    
    # Load up all the attributes before a freeze
    alias_method :regular_freeze, :freeze
    
    def freeze
      to_hash.freeze
      regular_freeze
    end
    
    def method_missing(name, *args, &block)
      to_hash.send(name, *args, &block)
    end
  end
end

Version data entries

9 entries across 9 versions & 3 rubygems

Version Path
ghazel-slim-attributes-0.6.3.1 lib/slim_attributes.rb
sdsykes-slim-attributes-0.6.2 lib/slim_attributes.rb
sdsykes-slim-attributes-0.6.3 lib/slim_attributes.rb
sdsykes-slim-attributes-0.6.4 lib/slim_attributes.rb
sdsykes-slim-attributes-0.6.6 lib/slim_attributes.rb
slim-attributes-0.6.6 lib/slim_attributes.rb
slim-attributes-0.6.4 lib/slim_attributes.rb
slim-attributes-0.6.0 lib/slim_attributes.rb
slim-attributes-0.5.0 lib/slim_attributes.rb