Module: Picasa::Utils

Included in:
Presenter::Base
Defined in:
lib/picasa/utils.rb

Class Method Summary collapse

Class Method Details

.array_wrap(object) ⇒ Object

Ported from activesupport gem


26
27
28
29
30
31
32
33
34
# File 'lib/picasa/utils.rb', line 26

def array_wrap(object)
  if object.nil?
    []
  elsif object.respond_to?(:to_ary)
    object.to_ary || [object]
  else
    [object]
  end
end

.inline_query(query) ⇒ Object


56
57
58
59
60
61
# File 'lib/picasa/utils.rb', line 56

def inline_query(query)
  query.map do |key, value|
    dasherized = key.to_s.gsub("_", "-")
    "#{CGI.escape(dasherized)}=#{CGI.escape(value.to_s)}"
  end.join("&")
end

.map_to_boolean(value) ⇒ Object


48
49
50
51
52
53
54
# File 'lib/picasa/utils.rb', line 48

def map_to_boolean(value)
  return unless value
  case value
    when "true"  then true
    when "false" then false
  end
end

.map_to_date(value) ⇒ Object


44
45
46
# File 'lib/picasa/utils.rb', line 44

def map_to_date(value)
  value && DateTime.parse(value)
end

.map_to_float(value) ⇒ Object


40
41
42
# File 'lib/picasa/utils.rb', line 40

def map_to_float(value)
  value && value.to_f
end

.map_to_integer(value) ⇒ Object


36
37
38
# File 'lib/picasa/utils.rb', line 36

def map_to_integer(value)
  value && value.to_i
end

.retrieve(hash, keys) ⇒ Object


15
16
17
18
19
20
21
22
23
# File 'lib/picasa/utils.rb', line 15

def retrieve(hash, keys)
  return if !hash.kind_of?(Hash) || !hash.has_key?(keys.first)

  if keys.size == 1
    hash[keys.first]
  elsif keys.size > 1
    Utils.retrieve(hash[keys.first], keys[1..-1])
  end
end

.safe_retrieve(hash, *keys) ⇒ Object


6
7
8
9
10
11
12
13
# File 'lib/picasa/utils.rb', line 6

def safe_retrieve(hash, *keys)
  result = retrieve(hash, keys)
  if result.kind_of?(Hash) && result.has_key?('$t') && result.keys.size == 1
    result['$t']
  else
    result
  end
end