Module: Lamed::Helper

Included in:
Controller, ObjectLoader
Defined in:
lib/lamed/helper.rb

Instance Method Summary collapse

Instance Method Details

#camelize_path(path) ⇒ Object

Camel case a string with / separator(s)



23
24
25
26
27
# File 'lib/lamed/helper.rb', line 23

def camelize_path(path)
  cameled_path = path.split("/").collect {|a| camelize_string(a).intern}
  cameled_path.delete_if { |s| s == :""}
  return cameled_path
end

#camelize_string(str) ⇒ Object

-=-=-=-= String Helper =-=-=-=- Camel case a string with _ separator(s)



17
18
19
20
# File 'lib/lamed/helper.rb', line 17

def camelize_string(str)
  cameled_string = str.split("_").map {|s| s.capitalize }.join
  return cameled_string
end

#class_to_path(klass) ⇒ Object



41
42
43
44
45
# File 'lib/lamed/helper.rb', line 41

def class_to_path(klass)
  klass_str = klass.to_s.split("::").collect { |s| uncamelize_string s }
  klass_path = File.join("/", klass_str)
  return klass_path
end

#mysql_time(str) ⇒ Object

Convert strings into a usable MySQL time object.



48
49
50
51
52
53
# File 'lib/lamed/helper.rb', line 48

def mysql_time(str)
  str[/(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/]
  year = $1.to_i; month = $2.to_i; day = $3.to_i; hour = $4.to_i; min = $5.to_i; sec = $6.to_i
  new_time = Time.local(year,month,day,hour,min,sec)
  return new_time
end

#sorted_array(array = self) ⇒ Object

-=-=-=-=Array Helper =-=-=-=-



56
57
58
59
60
61
62
63
64
65
# File 'lib/lamed/helper.rb', line 56

def sorted_array(array = self)
  count_hash = array.inject({}) { |h,(k,v)| h[k] = 0 if h[k].nil?; h[k] += 1; h }
  sorted_array = count_hash.to_a.collect { |a|
      a.reverse
    }.sort.reverse.collect { |a|
      a.reverse
    }
  sorted_array.collect! { |a| a[0] }
  return sorted_array
end

#sorted_hash(array = self) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/lamed/helper.rb', line 67

def sorted_hash(array = self)
  count_hash = array.inject({}) { |h,(k,v)| h[k] = 0 if h[k].nil?; h[k] += 1; h }
  sorted_array = count_hash.to_a.collect { |a|
      a.reverse
    }.sort.reverse.collect { |a|
      a.reverse
    }
  sorted_group = sorted_array.inject ({}) { |h,(k,v)|
      h[k] = v
      h
    }
  return sorted_group
end

#sorted_weighted_hash(array = self) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/lamed/helper.rb', line 81

def sorted_weighted_hash(array = self)
  sorted_group = array.sorted_group
  size = sorted_group.length
  new_hash = Hash.new
  sorted_group.each_pair { |k,v|
    new_hash[k] = v.to_f/size.to_f
  }
  return new_hash
end

#symbolize_hash_keys(hash = self) ⇒ Object

-=-=-=-= Hash Helper =-=-=-=- Changes keys that are strings in symbols. Goes two deep.



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

def symbolize_hash_keys(hash = self)
  symbolize_value = ->(value) { value.inject({}) {|h,(k,v)| h[(k.intern rescue k) || key] = v;h } }
  hash.inject({}) do |options, (key, value)|
    value = Hash === value ? symbolize_value.call(value) : value
    options[(key.to_sym rescue key) || key] = value
    options
  end
end

#uncamelize_path(cameled_path) ⇒ Object



35
36
37
38
39
# File 'lib/lamed/helper.rb', line 35

def uncamelize_path(cameled_path)
  uncameled_path = cameled_path.collect { |p| uncamelize_string(p.to_s) }
  path = uncameled_path.join("/").insert(0, "/")
  return path
end

#uncamelize_string(str) ⇒ Object



29
30
31
32
33
# File 'lib/lamed/helper.rb', line 29

def uncamelize_string(str)
  uncameled_string = (str.split('').collect {|c| c = c.upcase! == nil ? '_' + c.downcase : c.downcase}).join
  uncameled_string.slice!(0) if uncameled_string[0] == '_'
  return uncameled_string
end