class Google::Civic::Representation

Public Class Methods

has_many(association, options={}) click to toggle source
# File lib/google-civic/representation.rb, line 7
def has_many(association, options={})
  build_getter_for_ids(association)
  build_getter_for_collection(association, options)
end
new(representation, root_scope = nil) click to toggle source
Calls superclass method
# File lib/google-civic/representation.rb, line 43
def initialize(representation, root_scope = nil)
  @root_scope = root_scope || representation
  super(representation)
end

Private Class Methods

__singularized(word) click to toggle source
# File lib/google-civic/representation.rb, line 38
def __singularized(word)
  word.to_s.gsub(/s\z/, '')
end
build_getter_for_collection(association, options) click to toggle source
# File lib/google-civic/representation.rb, line 24
def build_getter_for_collection(association, options)
  class_eval %{
    def #{association}
      @__#{association} ||= #{__singularized(association)}_ids.map do |id|
        #{options[:class_name] || __singularized(association).capitalize}.new(__find_#{association}_by_id(id), @root_scope)
      end
    end

    def __find_#{association}_by_id(id)
      @root_scope.fetch("#{association}").fetch(id)
    end
  }
end
build_getter_for_ids(association) click to toggle source
# File lib/google-civic/representation.rb, line 14
def build_getter_for_ids(association)
  singularized_association = __singularized(association)

  class_eval %{
    def #{singularized_association}_ids
      self["#{singularized_association}Ids"] || []
    end
  }
end