Sha256: 19d444c7f959e363354a383b632d8e970dd4b8b758c892878fd85c5f8aafdcff
Contents?: true
Size: 1.7 KB
Versions: 13
Compression:
Stored size: 1.7 KB
Contents
module Spider; module Model # The ModelHash is a specialized hash for models. It is subclassed by Condition and Request. # It provides two functions: # * when given a BaseModel instance as a value, it will unwrap it setting its element-value pairs # * if the key is a dotted string, will split it and create sub-hashes. # Example: # cat = Cat.new(:name => 'Kitty', :color => 'black') # mh[:test] = cat # => {:test => {:name => 'Kitty', :color => 'black}} # mh['test.name'] = 'Devilish Kitty' # => {:test => {:name => 'Devilish Kitty', :color => 'black'}} class ModelHash < Hash alias :modelhash_orig_set :[]= # :nodoc: def initialize(hash=nil) super() merge!(hash) if (hash && hash.is_a?(Hash)) end # Returns a new instance when needed by an assignement. May be overridden by subclasses. def get_deep_obj return self.class.new end def []=(key, val) if (val.is_a?(BaseModel)) n = self.class.new val.each_val do |el, v| n[el] = v end val = n end key = key.name if key.class == Element parts = key.to_s.split('.', 2) return super(key.to_sym, val) unless parts[1] parts[0] = parts[0].to_sym self[parts[0]] = get_deep_obj unless self[parts[0]].is_a?(self.class) self[parts[0]][parts[1]] = val end def [](key) # TODO: deep key = key.name if key.class == Element super(key.to_sym) end end end; end;
Version data entries
13 entries across 13 versions & 1 rubygems