Module: OpenStates::Model::ClassMethods

Defined in:
lib/openstates/model.rb

Instance Method Summary collapse

Instance Method Details

#api_methodObject

Raises:

  • (NotImplementedError)


42
43
44
# File 'lib/openstates/model.rb', line 42

def api_method
  raise NotImplementedError
end

#find(id) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/openstates/model.rb', line 24

def find(id)
  return if !id

  id_hash = Hash.new
  id_hash[id_key] = id
  response = OpenStates.send(api_method, id_hash)

  from_hash(response)
end

#from_hash(hash) {|instance| ... } ⇒ Object

Yields:

  • (instance)


8
9
10
11
12
13
# File 'lib/openstates/model.rb', line 8

def from_hash(hash)
  instance = new
  instance.populate_from_hash!(hash)
  yield instance if block_given?
  instance
end

#hash_attr_accessor(*symbols) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/openstates/model.rb', line 15

def hash_attr_accessor(*symbols)
  attr_writer(*symbols)
  symbols.each do |symbol|
    define_method(symbol) do
      instance_variable_get("@#{symbol}")
    end
  end
end

#id_keyObject

Raises:

  • (NotImplementedError)


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

def id_key
  raise NotImplementedError
end

#where(options = {}) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/openstates/model.rb', line 34

def where(options = {})
  return [] if options.empty?

  OpenStates.send(api_method, options).map do |leg_hash|
    from_hash(leg_hash)
  end
end