rom-couchdb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8da918149d6b9e7f3fcaf2be59e248a85412c761
4
+ data.tar.gz: c8bbf55bce104b0749e0b24233dcee7422099eb8
5
+ SHA512:
6
+ metadata.gz: c4fdad583c91fb7656a306366ed68775403fcc27cec198449041f086740900d9bb6d820d783dafa2510e5600cd52a121ec5d51a99bb7fd41d243b4bd36f047ba
7
+ data.tar.gz: 7fd6573ecd427e38f0ae9cccfc8905ef77092183969615c2007e077199a8bf3fba759f12c5d4a84ca2aa5374674baaf469a1c56ab2e9ff8644d7874d4a10b164
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,6 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'rom-couchdb.gemspec'
4
+ - 'test/cases/unit/*'
5
+ - 'test/cases/integration/*'
6
+ - 'test/support/*'
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ services:
5
+ - couchdb
6
+ before_script:
7
+ - curl -X PUT localhost:5984/testdb
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rom-couchdb.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rom', git: 'https://github.com/rom-rb/rom.git', branch: 'master'
8
+ gem 'byebug'
9
+ gem 'activesupport'
10
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Hunter Madison
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Rom::CouchDB
2
+
3
+ [![Build Status](https://travis-ci.org/hmadison/rom-couchdb.svg)](https://travis-ci.org/hmadison/rom-couchdb)
4
+
5
+ Work in progress for CouchDB support for ROM. Uses [couchrest](https://github.com/couchrest/couchrest) as the connection adapter.
6
+
7
+ ## Example
8
+
9
+ ```ruby
10
+ class Color
11
+ attr_reader :name, :code, :id, :rev
12
+
13
+ def initialize(attributes)
14
+ puts attributes
15
+ @name, @code, @id, @rev = attributes.values_at(:name, :code, :id, :rev)
16
+ end
17
+ end
18
+
19
+ ROM.setup(:couchdb, 'testdb')
20
+
21
+ class Colors < ROM::Relation[:couchdb]
22
+ def find_id(id)
23
+ find_by_id(id)
24
+ end
25
+
26
+ def find_view(name, params)
27
+ find_by_view(name, params)
28
+ end
29
+ end
30
+
31
+ class ColorMapper < ROM::Mapper
32
+ relation :colors
33
+ register_as :entity
34
+
35
+ model Color
36
+
37
+ attribute :id, from: :'_id'
38
+ attribute :rev, from: :'_rev'
39
+ attribute :name
40
+ attribute :code
41
+ end
42
+
43
+ class CreateColor < ROM::Commands::Create[:couchdb]
44
+ register_as :create
45
+ relation :colors
46
+ result :one
47
+ end
48
+
49
+ class UpdateColor < ROM::Commands::Update[:couchdb]
50
+ register_as :update
51
+ relation :colors
52
+ result :one
53
+ end
54
+
55
+ rom = ROM.finalize.env
56
+ color_commands = rom.command(:colors)
57
+ color_relations = rom.relation(:colors)
58
+
59
+
60
+ color = color_commands.as(:entity).create.call(name: "Red", code: 8)
61
+ update = color_commands.as(:entity).create.call(name: "Blue", code: 9, _id: color.id, _rev: color.rev)
62
+
63
+ find = color_relations.as(:entity).find_id(color.id).one
64
+ all_docs = color_relations.find_view('_all_docs', {})
65
+ ```
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ task default: [:test]
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ t.test_files = FileList['test/cases/*/*_test.rb']
9
+ t.verbose = true
10
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'rom/couchdb'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require 'pry'
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,15 @@
1
+ require 'rom'
2
+
3
+ require 'rom/couchdb/version'
4
+ require 'rom/couchdb/repository'
5
+ require 'rom/couchdb/dataset'
6
+ require 'rom/couchdb/relation'
7
+ require 'rom/couchdb/commands'
8
+
9
+ module ROM
10
+ # CouchDB support for ROM
11
+ module CouchDB
12
+ end
13
+ end
14
+
15
+ ROM.register_adapter(:couchdb, ROM::CouchDB)
@@ -0,0 +1,45 @@
1
+ require 'rom/commands'
2
+
3
+ module ROM
4
+ module CouchDB
5
+ module Commands
6
+ # CouchDB create command
7
+ class Create < ROM::Commands::Create
8
+ def collection
9
+ relation.dataset
10
+ end
11
+
12
+ def execute(object)
13
+ document = object.dup
14
+ collection << document
15
+ [document]
16
+ end
17
+ end
18
+
19
+ # CouchDB update command
20
+ class Update < ROM::Commands::Update
21
+ def collection
22
+ relation.dataset
23
+ end
24
+
25
+ def execute(object)
26
+ document = object.dup
27
+ collection << (document)
28
+ [document]
29
+ end
30
+ end
31
+
32
+ # CouchDB delete command
33
+ class Delete < ROM::Commands::Delete
34
+ def collection
35
+ relation.dataset
36
+ end
37
+
38
+ def execute(object)
39
+ collection.delete(object)
40
+ [object]
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,65 @@
1
+ require 'couchrest'
2
+
3
+ require 'rom/support/options'
4
+ require 'rom/support/data_proxy'
5
+
6
+ module ROM
7
+ module CouchDB
8
+ # CouchDB Dataset
9
+ class Dataset
10
+ include ROM::Options
11
+ include ROM::DataProxy
12
+
13
+ attr_reader :results
14
+
15
+ option :connection, reader: true
16
+
17
+ def initialize(data, options = {})
18
+ @data = data
19
+ super
20
+ end
21
+
22
+ def insert(object)
23
+ input = stringify_proc.call(object.dup)
24
+ resp = @connection.save_doc(input)
25
+ # send back the id and revision of the document
26
+ object[:_id] = resp['id']
27
+ object[:_rev] = resp['rev']
28
+ self
29
+ end
30
+ alias_method :<<, :insert
31
+
32
+ def delete(object)
33
+ input = stringify_proc.call(object.dup)
34
+ @connection.delete_doc(input)
35
+ self
36
+ end
37
+
38
+ def count
39
+ @data.count
40
+ end
41
+
42
+ def to_a
43
+ @data
44
+ end
45
+
46
+ def find_by_id(id, params = {})
47
+ document = @connection.get(id, params).to_hash
48
+ self.class.new([document], connection: @connection)
49
+ end
50
+
51
+ def find_by_view(name, params = {})
52
+ results = @connection.view(name, params)
53
+ self.class.new([results], connection: @connection)
54
+ end
55
+
56
+ def stringify_proc
57
+ Transproc(:stringify_keys)
58
+ end
59
+
60
+ def self.row_proc
61
+ Transproc(:symbolize_keys)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,14 @@
1
+ require 'rom/relation'
2
+
3
+ module ROM
4
+ module CouchDB
5
+ # CouchDB relation support
6
+ class Relation < ROM::Relation
7
+ forward :insert, :find_by_id, :find_by_view
8
+
9
+ def count
10
+ dataset.count
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,27 @@
1
+ require 'couchrest'
2
+
3
+ module ROM
4
+ module CouchDB
5
+ # CouchDB repository for ROM
6
+ class Repository < ROM::Repository
7
+ attr_reader :connection, :sets
8
+
9
+ def initialize(uri)
10
+ @connection = CouchRest.database(uri)
11
+ @sets = {}
12
+ end
13
+
14
+ def dataset(name)
15
+ @sets[name] = Dataset.new([], connection: @connection)
16
+ end
17
+
18
+ def [](name)
19
+ @sets.fetch(name)
20
+ end
21
+
22
+ def dataset?(name)
23
+ @sets.include?(name)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,6 @@
1
+ module ROM
2
+ # CouchDB support for ROM
3
+ module CouchDB
4
+ VERSION = '0.1.0'
5
+ end
6
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rom/couchdb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'rom-couchdb'
8
+ spec.version = ROM::CouchDB::VERSION
9
+ spec.authors = ['Hunter Madison']
10
+ spec.email = ['hunterglenmadison@icloud.com']
11
+
12
+ spec.summary = 'CouchDB support for ROM'
13
+ spec.homepage = 'https://github.com/hmadison'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject {|f| f.match(%r{^(test)/})}
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_runtime_dependency 'rom', '~> 0.6.2'
22
+ spec.add_runtime_dependency 'couchrest'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.9'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rom-couchdb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hunter Madison
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rom
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.6.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: couchrest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description:
70
+ email:
71
+ - hunterglenmadison@icloud.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rubocop.yml"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/rom/couchdb.rb
86
+ - lib/rom/couchdb/commands.rb
87
+ - lib/rom/couchdb/dataset.rb
88
+ - lib/rom/couchdb/relation.rb
89
+ - lib/rom/couchdb/repository.rb
90
+ - lib/rom/couchdb/version.rb
91
+ - rom-couchdb.gemspec
92
+ homepage: https://github.com/hmadison
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.5
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: CouchDB support for ROM
116
+ test_files: []
117
+ has_rdoc: