rrepo 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: 12359bf008f2b41798bb287f98d2547d6abb0290
4
+ data.tar.gz: 4e29612943200c1075afd22293d5813e4de0167f
5
+ SHA512:
6
+ metadata.gz: c9137938df3e3a1a91076994fea53c5f278a9f1e732f593ef171bb413dc51dfbf4c8a17626780acd78ca5b4e99a8f7f39d5d3d2ac381b41ec0a9e609ad43f871
7
+ data.tar.gz: e846dc717cc36710a6949c0d5025564cf427f001751f716ba416aeab4a5dc0cb4404e6cacc4bf1c0cc91d09f8ca8d4231bb87bb7742c5a26cdd5bdfb3464d777
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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.0
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bsh-persistence.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # RRepo
2
+
3
+ Repository pattern for ruby. RRepo is a very simple implementation of
4
+ the repository pattern.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'rrepo'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install rrepo
21
+
22
+ ## Development
23
+
24
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
25
+
26
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it ( https://github.com/[my-github-username]/bsh-persistence/fork )
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rrepo"
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,20 @@
1
+ require 'abstractize'
2
+
3
+ module RRepo
4
+ module Adapters
5
+ # An abstract adapter class
6
+ class Base
7
+ include Abstractize
8
+
9
+ def initialize(_options)
10
+ end
11
+
12
+ define_abstract_method :create
13
+ define_abstract_method :update
14
+ define_abstract_method :delete
15
+ define_abstract_method :all
16
+ define_abstract_method :find
17
+ define_abstract_method :clear
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,47 @@
1
+ require 'rrepo/adapters/base'
2
+ require 'mongo'
3
+
4
+ module RRepo
5
+ module Adapters
6
+ # A mongodb adapter
7
+ class Mongo < Base
8
+ def initialize(options)
9
+ db_name = options.delete(:db)
10
+ @client = ::Mongo::Client.new(options.delete(:host), options)
11
+ @db = @client[db_name]
12
+ end
13
+
14
+ def create(collection, model)
15
+ @db[collection.to_s].insert(model.to_hash)
16
+ end
17
+
18
+ def update(collection, model)
19
+ hash = model.to_hash
20
+ @db[collection.to_s].update(id_query(hash.delete(:_id)), hash)
21
+ end
22
+
23
+ def delete(collection, model)
24
+ id = model.to_hash[:_id]
25
+ @db[collection.to_s].remove(id_query(id))
26
+ end
27
+
28
+ def all(collection)
29
+ @db[collection.to_s].find
30
+ end
31
+
32
+ def find(collection, id)
33
+ @db[collection].find(id_query(id))
34
+ end
35
+
36
+ def clear(collection)
37
+ @db[collection].drop
38
+ end
39
+
40
+ protected
41
+
42
+ def id_query(id)
43
+ { _id: id }
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,6 @@
1
+ module RRepo
2
+ module Errors
3
+ class NonPersistedModelError < RuntimeError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,57 @@
1
+ require 'abstractize'
2
+ require 'active_support/inflector'
3
+ require 'active_support/core_ext/object/blank'
4
+ require 'rrepo/errors/non_persisted_model_error'
5
+
6
+ module RRepo
7
+ # An abstract repository implementation with common functions
8
+ class Repository
9
+ include Abstractize
10
+
11
+ attr_reader :collection, :adapter, :model_class
12
+
13
+ def initialize(adapter)
14
+ @adapter = adapter
15
+ class_name = self.class.name.demodulize
16
+ model_class_name = class_name[/.*(?=Repository)/]
17
+ @collection = model_class_name.underscore.pluralize.to_sym
18
+ return if class_name == 'Repository'
19
+ @model_class = model_class_name.constantize
20
+ end
21
+
22
+ def create(model)
23
+ return model if model._id.present?
24
+ id = adapter.create(collection, model)
25
+ model._id = id
26
+ model
27
+ end
28
+
29
+ def update(model)
30
+ unless model._id.present?
31
+ fail Errors::NonPersistedModelError, "#{model} is not persisted"
32
+ end
33
+ adapter.update(collection, model)
34
+ end
35
+
36
+ def delete(model)
37
+ unless model._id.present?
38
+ fail Errors::NonPersistedModelError, "#{model} is not persisted"
39
+ end
40
+ adapter.delete(collection, model)
41
+ end
42
+
43
+ def all
44
+ result = adapter.all(collection)
45
+ result.map(&model_class.method(:new))
46
+ end
47
+
48
+ def find(id)
49
+ result = adapter.find(collection, id)
50
+ model_class.new(result)
51
+ end
52
+
53
+ def clear
54
+ adapter.clear(collection)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ module RRepo
2
+ VERSION = "0.1.0"
3
+ end
data/lib/rrepo.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "rrepo/version"
2
+
3
+ module RRepo
4
+ # Your code goes here...
5
+ end
data/rrepo.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rrepo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rrepo"
8
+ spec.version = RRepo::VERSION
9
+ spec.authors = ["Joakim Reinert"]
10
+ spec.email = ["mail@jreinert.com"]
11
+
12
+ spec.summary = 'Simple gem implementing the repository pattern'
13
+ spec.homepage = 'https://github.com/jreinert/rrepo'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ if spec.respond_to?(:metadata)
21
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
22
+ end
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.8"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency 'database_cleaner', '~> 1.4'
27
+
28
+ spec.add_dependency 'mongo', '~> 1.12'
29
+ spec.add_dependency 'bson_ext', '~> 1.12'
30
+ spec.add_dependency 'activesupport', '~> 4.2'
31
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rrepo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joakim Reinert
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: database_cleaner
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mongo
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.12'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bson_ext
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.12'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.12'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4.2'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4.2'
97
+ description:
98
+ email:
99
+ - mail@jreinert.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".ruby-version"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - lib/rrepo.rb
114
+ - lib/rrepo/adapters/base.rb
115
+ - lib/rrepo/adapters/mongo.rb
116
+ - lib/rrepo/errors/non_persisted_model_error.rb
117
+ - lib/rrepo/repository.rb
118
+ - lib/rrepo/version.rb
119
+ - rrepo.gemspec
120
+ homepage: https://github.com/jreinert/rrepo
121
+ licenses: []
122
+ metadata:
123
+ allowed_push_host: https://rubygems.org
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.4.5
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Simple gem implementing the repository pattern
144
+ test_files: []