class GreatSchools::Model

GreatSchools Base Model

Public Class Methods

new(attributes = {}) click to toggle source

Base initializer, map camelCased XML attributes from the GreatSchools API response to the appropriately underscored attribute setters.

# File lib/great_schools/model.rb, line 24
def initialize(attributes = {})
  attributes.each do |key, value|
    key = underscore(key)
    send("#{key}=", value) if respond_to?("#{key}=")
  end
end

Protected Class Methods

parameterize(string) click to toggle source

Makes a URL slug from the string.

Replaces dashes with underscores, spaces with dashes, and URL encodes any special characters.

Examples

parameterize('San Francisco')       # => 'San-Francisco'
parameterize('Cardiff-By-The-Sea')  # => 'Cardiff_By_The_Sea'
# File lib/great_schools/model.rb, line 17
def parameterize(string)
  CGI.escape(string.gsub('-', '_').gsub(' ', '-'))
end

Protected Instance Methods

underscore(word) click to toggle source

Makes an underscored, lowercase form from the expression in the string.

Examples

underscore('myACRONYMString') # => 'my_acronym_string'
# File lib/great_schools/model.rb, line 38
def underscore(word)
  word = word.to_s.dup # unfreeze any frozen strings
  word.gsub!(/([a-z])([A-Z])/,'\1_\2')          # myACRONYMString   => my_ACRONYMString
  word.gsub!(/([A-Z]+)(?=[A-Z][a-z])/, '\1_\2') # my_ACRONYMString  => my_ACRONYM_String
  word.downcase                                 # my_ACRONYM_String => my_acronym_string
end