relaxo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.md +35 -0
  2. data/bin/relaxo +66 -0
  3. data/lib/relaxo/version.rb +29 -0
  4. data/lib/relaxo.rb +0 -0
  5. metadata +65 -0
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ Relaxo
2
+ ======
3
+
4
+ * Author: Samuel G. D. Williams (<http://www.oriontransfer.co.nz>)
5
+ * Copyright (C) 2010, 2011 Samuel G. D. Williams.
6
+ * Released under the MIT license.
7
+
8
+ Relaxo is a simple gem to assist with loading CouchDB data from YAML files.
9
+
10
+ relaxo http://localhost:5984/test design.yaml sample.yaml
11
+
12
+ Where `design.yaml` and `sample.yaml` contain lists of valid documents.
13
+
14
+ License
15
+ -------
16
+
17
+ Copyright (c) 2010, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
18
+
19
+ Permission is hereby granted, free of charge, to any person obtaining a copy
20
+ of this software and associated documentation files (the "Software"), to deal
21
+ in the Software without restriction, including without limitation the rights
22
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
23
+ copies of the Software, and to permit persons to whom the Software is
24
+ furnished to do so, subject to the following conditions:
25
+
26
+ The above copyright notice and this permission notice shall be included in
27
+ all copies or substantial portions of the Software.
28
+
29
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
35
+ THE SOFTWARE.
data/bin/relaxo ADDED
@@ -0,0 +1,66 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'couchrest'
4
+ require 'yaml'
5
+
6
+ OPTIONS = {
7
+ :existing => :merge
8
+ }
9
+
10
+ $db = CouchRest.database!(ARGV.shift)
11
+ $documents = YAML::load(ARGF)
12
+
13
+ unless Array === $documents
14
+ $documents = [$documents]
15
+ end
16
+
17
+ $stats = {
18
+ :saved => 0,
19
+ :errors => 0,
20
+ :total => 0
21
+ }
22
+
23
+ $documents.each do |document|
24
+ begin
25
+ $stderr.puts "Loading #{document.inspect}"
26
+ existing_document = nil
27
+
28
+ if document && document['_id']
29
+ existing_document = $db.get(document['_id'])
30
+ end
31
+
32
+ if existing_document
33
+ if OPTIONS[:existing] == :replace
34
+ $stderr.puts "Replacing existing document..."
35
+ # The document is replaced entirely with the incoming data.
36
+ document['_rev'] = existing_document['_rev']
37
+ elsif OPTIONS[:existing] == :update
38
+ $stderr.puts "Updating existing document..."
39
+ # Values in `existing_document` take priority
40
+ document.update(existing_document)
41
+ elsif OPTIONS[:existing] == :merge
42
+ $stderr.puts "Merging existing document..."
43
+ # Values in `document` take priority, except for `_rev`.
44
+ document.update(existing_document) do |key, oldval, newval|
45
+ key == '_rev' ? newval : oldval
46
+ end
47
+ end
48
+ end
49
+
50
+ $stderr.puts "Saving #{document.inspect}"
51
+ $result = $db.save_doc(document)
52
+
53
+ $stats[:saved] += 1
54
+ rescue RestClient::BadRequest => ex
55
+ $stderr.puts ex.inspect
56
+
57
+ $stats[:errors] += 1
58
+ end
59
+
60
+ $stats[:total] += 1
61
+ end
62
+
63
+ $stderr.puts "#{$stats[:saved]} document(s) saved out of #{$stats[:total]}."
64
+ if $stats[:errors] > 0
65
+ $stderr.puts "#{$stats[:errors]} errors occurred!"
66
+ end
@@ -0,0 +1,29 @@
1
+ # Copyright (c) 2012 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ module Relaxo
22
+ module VERSION
23
+ MAJOR = 0
24
+ MINOR = 1
25
+ TINY = 0
26
+
27
+ STRING = [MAJOR, MINOR, TINY].join('.')
28
+ end
29
+ end
data/lib/relaxo.rb ADDED
File without changes
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: relaxo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Samuel Williams
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: couchrest
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description:
31
+ email: samuel.williams@oriontransfer.co.nz
32
+ executables:
33
+ - relaxo
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - bin/relaxo
38
+ - lib/relaxo/version.rb
39
+ - lib/relaxo.rb
40
+ - README.md
41
+ homepage: http://www.oriontransfer.co.nz/gems/relaxo
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.23
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Relaxo is a helper for loading and working with CouchDB.
65
+ test_files: []