Module: OpenStates::Model::ClassMethods
- Defined in:
- lib/openstates/model.rb
Instance Method Summary collapse
- #api_method ⇒ Object
- #find(id) ⇒ Object
- #from_hash(hash) {|instance| ... } ⇒ Object
- #hash_attr_accessor(*symbols) ⇒ Object
- #id_key ⇒ Object
- #where(options = {}) ⇒ Object
Instance Method Details
#api_method ⇒ Object
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
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_key ⇒ Object
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( = {}) return [] if .empty? OpenStates.send(api_method, ).map do |leg_hash| from_hash(leg_hash) end end |