Class: OfflineLookup::Builder

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Builder

Returns a new instance of Builder.



68
69
70
71
# File 'lib/offline_lookup.rb', line 68

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

Instance Method Details

#field_for_key_method_nameObject

e.g. :name_for_id(id)



102
103
104
# File 'lib/offline_lookup.rb', line 102

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

#indentiy_method_name(value) ⇒ Object

e.g., :two_hour?



97
98
99
# File 'lib/offline_lookup.rb', line 97

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

#key_for_field_method_nameObject

e.g. :id_for_name(name)



107
108
109
# File 'lib/offline_lookup.rb', line 107

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

#key_method_name(value) ⇒ Object

e.g., :two_hour_id



88
89
90
# File 'lib/offline_lookup.rb', line 88

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

#lookup_method_name(value) ⇒ Object



92
93
94
# File 'lib/offline_lookup.rb', line 92

def lookup_method_name(value)
  sanitize value.to_s
end

#sanitize(string) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/offline_lookup.rb', line 73

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