beanbag 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/beanbag.rb +61 -0
- metadata +64 -0
data/lib/beanbag.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'couchrest'
|
2
|
+
|
3
|
+
class BeanBag
|
4
|
+
|
5
|
+
def initialize(args)
|
6
|
+
@data = args
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.found_by(criteria)
|
10
|
+
|
11
|
+
@@criteria = {} # initialize criteria
|
12
|
+
|
13
|
+
if criteria.is_a? Hash
|
14
|
+
@@criteria = criteria
|
15
|
+
elsif criteria.is_a? Array
|
16
|
+
criteria.each do |key|
|
17
|
+
@@criteria[key] = nil
|
18
|
+
end
|
19
|
+
elsif criteria.is_a? String
|
20
|
+
@@criteria[criteria] = nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.all
|
25
|
+
begin
|
26
|
+
view sprintf('%s/all_%ss', self.name.downcase, self.name.downcase)
|
27
|
+
rescue
|
28
|
+
# debugger
|
29
|
+
doc = database.get("_design/#{self.name.downcase}")
|
30
|
+
doc['views']["all_#{self.name.downcase}s"] = {}
|
31
|
+
|
32
|
+
view_string = "function(doc){"
|
33
|
+
if not @@criteria.empty?
|
34
|
+
view_string << "if("
|
35
|
+
conditions = []
|
36
|
+
@@criteria.each_pair do |name, value|
|
37
|
+
conditions << "doc.#{name.to_s}"
|
38
|
+
conditions << "doc.#{name.to_s} === '#{value.to_s}'" if defined? value
|
39
|
+
end
|
40
|
+
view_string << conditions.join(" && ")
|
41
|
+
view_string << ")"
|
42
|
+
end
|
43
|
+
view_string << "emit(doc['_id'], doc);}"
|
44
|
+
|
45
|
+
doc['views']["all_#{self.name.downcase}s"]['map'] = view_string
|
46
|
+
database.save_doc(doc)
|
47
|
+
retry
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.database
|
52
|
+
if not defined? @@database
|
53
|
+
@@database = CouchRest.new.database('artifacts')
|
54
|
+
end
|
55
|
+
@@database
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.view(view_name)
|
59
|
+
database.view view_name
|
60
|
+
end
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: beanbag
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Ryan Mitchell
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-03-31 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: BeanBag is a flexible wrapper around CouchRest that pads the metal just a little bit.
|
22
|
+
email: mitchellryanj@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/beanbag.rb
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: http://ryanjmitchell.net
|
33
|
+
licenses: []
|
34
|
+
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.3.7
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: BeanBag is a flexible wrapper around CouchRest that pads the metal just a little bit.
|
63
|
+
test_files: []
|
64
|
+
|