Class: Hash
- Inherits:
-
Object
show all
- Defined in:
- lib/stella/core_ext.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.from_array(array = []) ⇒ Object
533
534
535
536
537
538
539
|
# File 'lib/stella/core_ext.rb', line 533
def self.from_array(array = [])
h = Hash.new
array.size.times do |t|
h[t] = array[t]
end
h
end
|
Instance Method Details
#dump(format) ⇒ Object
499
500
501
|
# File 'lib/stella/core_ext.rb', line 499
def dump(format)
respond_to?(:"to_#{format}") ? send(:"to_#{format}") : raise("Unknown format")
end
|
#flatten ⇒ Object
Courtesy of Julien Genestoux
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
# File 'lib/stella/core_ext.rb', line 472
def flatten
params = {}
stack = []
each do |k, v|
if v.is_a?(Hash)
stack << [k,v]
elsif v.is_a?(Array)
stack << [k,Hash.from_array(v)]
else
params[k] = v
end
end
stack.each do |parent, hash|
hash.each do |k, v|
if v.is_a?(Hash)
stack << ["#{parent}[#{k}]", v]
else
params["#{parent}[#{k}]"] = v
end
end
end
params
end
|
#to_http_params ⇒ Object
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
|
# File 'lib/stella/core_ext.rb', line 506
def to_http_params
params = ''
stack = []
each do |k, v|
if v.is_a?(Hash)
stack << [k,v]
elsif v.is_a?(Array)
stack << [k,Hash.from_array(v)]
else
params << "#{k}=#{v}&"
end
end
stack.each do |parent, hash|
hash.each do |k, v|
if v.is_a?(Hash)
stack << ["#{parent}[#{k}]", URI::Escape.escape(v)]
else
params << "#{parent}[#{k}]=#{URI::Escape.escape(v)}&"
end
end
end
params.chop!
params
end
|
#to_json(*args) ⇒ Object
466
467
468
|
# File 'lib/stella/core_ext.rb', line 466
def to_json(*args)
Yajl::Encoder.encode(self)
end
|