Sha256: de19a1ba415fb5929db3fc41e25db195353251327168cea36316dbd14fde47fd

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

#
# Author: Waldemar Hummer (hummer@dsg.tuwien.ac.at)
#

require "active_record"

module Toaster
  class KeyValuePair < ActiveRecord::Base
    self.inheritance_column = :type

    serialize :value, JSON

    def initialize(hash)
      if !hash[:type]
        type = IgnoreProperty.to_s
      end
      super(hash)
      @attributes_cache = {} if !@attributes_cache # fixes bug in active_record v4.1
    end

    def self.get_as_hash(list)
      attrs = {}
      list.each do |a|
        attrs[a.key] = a.value
      end
      return attrs
    end

    def self.from_hash(hash, clazz=KeyValuePair)
      result = []
      return result if !hash
      hash.each { |key,value|
        result << clazz.new(
          :key => key,
          :value => value
        )
      }
      return result
    end

    def self.flat_attributes_from_hash(hash, clazz=KeyValuePair)
      result = []
      return result if !hash
      SystemState.get_flat_attributes(hash).each do |key,value|
        result << clazz.new(
          :key => key,
          :value => value
        )
      end
      return result
    end

    def to_s
      return "#{self.class}(#{key}=#{value})"
    end

    def hash
      begin
        return id if id
      rescue
      end
      h = 0
      h += key.hash rescue 0
      h += value.hash rescue 0
      h += type.hash rescue 0
      h += data_type.hash rescue 0
      return h
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cloud-toaster-1.1.6 lib/toaster/model/key_value_pair.rb