Class: OfflineLookup::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/offline_lookup.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Builder



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

def initialize(options)
  @field = options[:field]
  @key = options[:key]
end

Instance Method Details

#field_for_key_method_nameObject

e.g. :name_for_id(id)



51
52
53
# File 'lib/offline_lookup.rb', line 51

def field_for_key_method_name
  sanitize "#{@field}_for_#{@key}"
end

#indentiy_method_name(value) ⇒ Object

e.g., :two_hour?



46
47
48
# File 'lib/offline_lookup.rb', line 46

def indentiy_method_name(value)
  lookup_method_name(value) + "?"
end

#key_for_field_method_nameObject

e.g. :id_for_name(name)



56
57
58
# File 'lib/offline_lookup.rb', line 56

def key_for_field_method_name
  sanitize "#{@key}_for_#{@field}"
end

#key_method_name(value) ⇒ Object

e.g., :two_hour_id



37
38
39
# File 'lib/offline_lookup.rb', line 37

def key_method_name(value)
  sanitize "#{value}_#{@key}"
end

#lookup_method_name(value) ⇒ Object



41
42
43
# File 'lib/offline_lookup.rb', line 41

def lookup_method_name(value)
  sanitize value.to_s
end

#sanitize(string) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/offline_lookup.rb', line 22

def sanitize(string)
  #:methodize went away. Where did it go?
  #1. Replace illegal chars and _ boundaries with " " boundary
  string = string.gsub(/[^a-zA-Z\d]+/," ").strip
  #2. Insert " " boundary at snake-case boundaries
  string.gsub!(/([a-z])([A-Z])/){|s| "#{$1} #{$2}"}
  #3. underscore
  string.gsub!(/\s+/, "_")
  string.downcase!
  #4. Append underscore if name begins with digit
  string = "_#{string}" if string.length == 0 || string[0] =~ /\d/
  return string
end