Module: BlocRecord::Utility

Extended by:
Utility
Included in:
Utility
Defined in:
lib/bloc_record/utility.rb

Instance Method Summary collapse

Instance Method Details

#convert_keys(options) ⇒ Object



23
24
25
26
# File 'lib/bloc_record/utility.rb', line 23

def convert_keys(options)
  options.keys.each {|k| options[k.to_s] = options.delete(k) if k.kind_of?(Symbol)}
  options
end

#instance_variables_to_hash(obj) ⇒ Object



28
29
30
# File 'lib/bloc_record/utility.rb', line 28

def instance_variables_to_hash(obj)
  Hash[obj.instance_variables.map{ |var| ["#{var.to_s.delete('@')}", obj.instance_variable_get(var.to_s)]}]
end

#reload_obj(dirty_obj) ⇒ Object



32
33
34
35
36
37
# File 'lib/bloc_record/utility.rb', line 32

def reload_obj(dirty_obj)
  persisted_obj = dirty_obj.class.find_one(dirty_obj.id)
  dirty_obj.instance_variables.each do |instance_variable|
    dirty_obj.instance_variable_set(instance_variable, persisted_obj.instance_variable_get(instance_variable))
  end
end

#sql_strings(value) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/bloc_record/utility.rb', line 12

def sql_strings(value)
  case value
  when String
    "'#{value}'"
  when Numeric
    value.to_s
  else
    "null"
  end
end

#underscore(camel_cased_word) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/bloc_record/utility.rb', line 5

def underscore(camel_cased_word)
   string = camel_cased_word.gsub(/::/, '/')
   string.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
   string.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
   string.tr!("-", "_")
   string.downcase
end