Module: ActiveGist::ClassMethods
- Included in:
- ActiveGist
- Defined in:
- lib/active_gist/class_methods.rb
Instance Method Summary collapse
- #all(type = :all) ⇒ Object
- #count(type = :all) ⇒ Object
- #create(options = {}) ⇒ Object
- #create!(options = {}) ⇒ Object
- #find(id) ⇒ Object
- #first(type = :all) ⇒ Object
- #last(type = :all) ⇒ Object
- #load(hash) ⇒ Object
Instance Method Details
#all(type = :all) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/active_gist/class_methods.rb', line 32 def all(type = :all) case type when :all then JSON.parse api.get(:accept => 'application/json') when :public then JSON.parse api['public'].get(:accept => 'application/json') when :starred then JSON.parse api['starred'].get(:accept => 'application/json') else raise ArgumentError, "Unknown type: #{type.inspect} (expected one of [:all, :public, :starred])" end.collect do |hash| load hash end end |
#count(type = :all) ⇒ Object
18 19 20 |
# File 'lib/active_gist/class_methods.rb', line 18 def count(type = :all) all(type).count end |
#create(options = {}) ⇒ Object
6 7 8 |
# File 'lib/active_gist/class_methods.rb', line 6 def create( = {}) new().tap { |gist| gist.save } end |
#create!(options = {}) ⇒ Object
2 3 4 |
# File 'lib/active_gist/class_methods.rb', line 2 def create!( = {}) new().tap { |gist| gist.save! } end |
#find(id) ⇒ Object
22 23 24 |
# File 'lib/active_gist/class_methods.rb', line 22 def find(id) load JSON.parse(api[id].get(:accept => 'application/json')) end |
#first(type = :all) ⇒ Object
10 11 12 |
# File 'lib/active_gist/class_methods.rb', line 10 def first(type = :all) all(type).first end |
#last(type = :all) ⇒ Object
14 15 16 |
# File 'lib/active_gist/class_methods.rb', line 14 def last(type = :all) all(type).last end |
#load(hash) ⇒ Object
26 27 28 29 30 |
# File 'lib/active_gist/class_methods.rb', line 26 def load(hash) new(hash).tap do |instance| instance.changed_attributes.clear end end |