Class: Cushion
- Inherits:
-
HashWithIndifferentAccess
- Object
- HashWithIndifferentAccess
- Cushion
- Defined in:
- lib/cushion.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
Class Method Summary collapse
- .create_database(uri) ⇒ Object
- .database_created(true_or_false = nil) ⇒ Object
- .delete(uri) ⇒ Object
- .get(uri) ⇒ Object
- .post(uri, data = nil) ⇒ Object
- .put(uri, data = nil) ⇒ Object
- .server(host_and_port = nil) ⇒ Object
Instance Method Summary collapse
- #document_location(uri = nil) ⇒ Object
-
#initialize(*args, &block) ⇒ Cushion
constructor
A new instance of Cushion.
- #load ⇒ Object
- #revision ⇒ Object
- #save ⇒ Object
Constructor Details
#initialize(*args, &block) ⇒ Cushion
Returns a new instance of Cushion.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/cushion.rb', line 14 def initialize(*args, &block) if args[0].is_a?(String) uri = args.shift end if uri.nil? if self.class.ancestors[1..-1].include?(Cushion) uri = '/' << self.class.name.underscore.pluralize else raise "No database URI given" end end document_location uri data = args.shift super(data, &block) end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
12 13 14 |
# File 'lib/cushion.rb', line 12 def id @id end |
Class Method Details
.create_database(uri) ⇒ Object
41 42 43 44 |
# File 'lib/cushion.rb', line 41 def self.create_database uri put(uri) unless get(uri)['db_name'] database_created true end |
.database_created(true_or_false = nil) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/cushion.rb', line 46 def self.database_created true_or_false = nil if [ true, false ].include?(true_or_false) @database_created = true_or_false else @database_created end end |
.delete(uri) ⇒ Object
105 106 107 |
# File 'lib/cushion.rb', line 105 def delete uri JSON.parse(request(Net::HTTP::Delete.new(uri)).body) end |
.get(uri) ⇒ Object
87 88 89 |
# File 'lib/cushion.rb', line 87 def get uri JSON.parse(request(Net::HTTP::Get.new(uri)).body) end |
.post(uri, data = nil) ⇒ Object
98 99 100 101 102 103 |
# File 'lib/cushion.rb', line 98 def post uri, data = nil req = Net::HTTP::Post.new(uri) req["content-type"] = "application/json" req.body = data.to_json if data.present? JSON.parse(request(req).body) end |
.put(uri, data = nil) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/cushion.rb', line 91 def put uri, data = nil req = Net::HTTP::Put.new(uri) req["content-type"] = "application/json" req.body = data.to_json if data.present? JSON.parse(request(req).body) end |
.server(host_and_port = nil) ⇒ Object
83 84 85 |
# File 'lib/cushion.rb', line 83 def server host_and_port = nil host_and_port.present? ? @server = host_and_port : @server end |
Instance Method Details
#document_location(uri = nil) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cushion.rb', line 30 def document_location uri = nil return @document_location unless uri uri =~ /(http:\/\/)?([^:\/\?]+)?(:([0-9]+))?\/(\w+)(\/(\w+))?/ self.class.server [ $2 || 'localhost', $4 ? $4.to_i : 5984 ] @document_location = {} @document_location[:database] = $5 self.id = $7 @document_location[:uri] = "/#{@document_location[:database]}/#{id}" if id end |
#load ⇒ Object
58 59 60 61 62 63 |
# File 'lib/cushion.rb', line 58 def load doc = self.class.get(document_location[:uri]) self.id = doc.delete('_id') @revision = doc.delete('_rev') replace doc end |
#revision ⇒ Object
54 55 56 |
# File 'lib/cushion.rb', line 54 def revision @revision end |
#save ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cushion.rb', line 65 def save unless self.class.database_created self.class.create_database('/' << document_location[:database]) end if id.present? copy = @revision ? self.merge(_rev: @revision) : self.dup response = self.class.put(document_location[:uri], copy) @revision = response['rev'] else response = self.class.post('/' << document_location[:database], self) self.id = response['id'] @revision = response['rev'] @document_location[:uri] = "/#{@document_location[:database]}/#{id}" end @revision end |