Module: Arcade::Support::Hash

Included in:
Hash
Defined in:
lib/support/conversions.rb

Instance Method Summary collapse

Instance Method Details

#allocate_model(autoload = Config.autoload) ⇒ Object



224
225
226
# File 'lib/support/conversions.rb', line 224

def allocate_model( autoload = Config.autoload )
  _allocate_model( self , autoload )
end

#from_dbObject



204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/support/conversions.rb', line 204

def from_db
  # populate the hash by converting keys: stings to symbols, values: preprocess through :from_db
  map do |k,v|
    orient_k = if  k.to_s.to_i.to_s == k.to_s
                 k.to_i
               else
                 k.to_sym
               end

    [orient_k, v.from_db]
  end.to_h
end

#to_dbObject

converts :abc => anything to “abc” => anything

converts nn => anything to nn => anything

leaves “abc” => anything untouched



190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/support/conversions.rb', line 190

def to_db   # converts hast from activeorient to db
  map do | k, v|
    orient_k =  case k
                when Numeric
                  k
                when Symbol, String
                  k.to_s
                else
                  raise "not supported key: #{k} -- must a sting, symbol or number"
                end
    [orient_k, v.to_db]
  end.to_h
end

#to_humanObject



217
218
219
220
221
222
# File 'lib/support/conversions.rb', line 217

def to_human
  "< " + map do  | k,v |
    vv = v.is_a?( Arcade::Base ) ? v.to_human[1..-2] :  v.to_s
     k.to_s + ": "  + vv
  end.join( "\n    " ) + " >"
end

#to_orObject

converts a hash to a string appropiate to include in raw queries



231
232
233
# File 'lib/support/conversions.rb', line 231

def to_or
  "{ " + to_db.map{|k,v| "#{k.to_or}: #{v.to_or}"}.join(',') + "}"
end