Permalink
Browse files

Create LostItem and FoundItem classes

LostItem and FoundItem subclass Items to provide simple classes which
only deal with lost and found items, respectively. This moves some
logic out of the main app methods.
  • Loading branch information...
mistydemeo committed Aug 11, 2012
1 parent 5435879 commit 376177ee16b5e327ef94842150da52cd07db5fd6
Showing with 42 additions and 18 deletions.
  1. +4 −18 app.rb
  2. +38 −0 models/items.rb
View
22 app.rb
@@ -39,7 +39,7 @@
validate_count
- return search_response_for Items.where(:kind => "lost", :item => params["q"]).limit params["count"]
+ return search_response_for LostItem.where(:item => params["q"]).limit params["count"]
end
get "/lost/searchfound" do
@@ -50,7 +50,7 @@
validate_count
- return search_response_for Items.where(:kind => "found", :item => params["q"]).limit params["count"]
+ return search_response_for FoundItem.where(:item => params["q"]).limit params["count"]
end
# This should really be POST, but Poignant Guide's API didn't distinguish
@@ -60,14 +60,7 @@
return invalid_input_response_for missing_inputs
end
- item = Items.new do |i|
- i.name = params["name"]
- i.item = params["item"]
- i.location = params["seen"]
- i.description = params["desc"]
- i.kind = "lost"
- i.public_id = Brooklynt.new.to_i
- end
+ item = LostItem.new params
item.save
@@ -80,14 +73,7 @@
return invalid_input_response_for missing_inputs
end
- item = Items.new do |i|
- i.name = params["name"]
- i.item = params["item"]
- i.location = params["at"]
- i.description = params["desc"]
- i.kind = "found"
- i.public_id = Brooklynt.new.to_i
- end
+ item = FoundItem.new params
item.save
View
@@ -16,4 +16,42 @@ def to_hash
:public_id => public_id
}
end
+end
+
+class LostItem < Items
+ def initialize opts
+ properties = {
+ name: opts["name"],
+ item: opts["item"],
+ location: opts["seen"],
+ description: opts["desc"],
+ kind: "lost",
+ public_id: Brooklynt.new.to_i
+ }
+ super properties
+ end
+
+ def self.where query
+ query[:kind] = "lost"
+ super query
+ end
+end
+
+class FoundItem < Items
+ def initialize opts
+ properties = {
+ name: opts["name"],
+ item: opts["item"],
+ location: opts["at"],
+ description: opts["desc"],
+ kind: "found",
+ public_id: Brooklynt.new.to_i
+ }
+ super properties
+ end
+
+ def self.where query
+ query[:kind] = "found"
+ super query
+ end
end

0 comments on commit 376177e

Please sign in to comment.