fish0 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +137 -0
- data/Rakefile +23 -0
- data/lib/fish0/concerns/cacheable.rb +37 -0
- data/lib/fish0/concerns/paginatable.rb +25 -0
- data/lib/fish0/concerns/view_model.rb +20 -0
- data/lib/fish0/paginator.rb +37 -0
- data/lib/fish0/record_not_found.rb +4 -0
- data/lib/fish0/repository.rb +90 -0
- data/lib/fish0/version.rb +3 -0
- data/lib/fish0.rb +21 -0
- data/lib/tasks/fish0_tasks.rake +4 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 85525f8d2adf2b223e777689f28c07583ef03eca
|
4
|
+
data.tar.gz: 3ddae37272e67fb3b2a3f17601b140e3a768ed1d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2d5d8251fc0e4cf34676ecd69b3b8dfc663790374c88b166493ab0d3e8337a1a080db943d09ec4ff0329c5334dd8ce93de4701a2d6f7bffa135aae30e9448a52
|
7
|
+
data.tar.gz: 35753e2406ce9b9065c4ebd30e8310d17b864df541866959cf6af8400e60cf31cdfc7498e6bcfdd9bcb7c6153e0e552aad2cf6bab974e5a12e9ff3197b41a45a
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 Dmitry Zuev
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
# Fish0
|
2
|
+
|
3
|
+
The fish doesn't think because the fish knows everything.
|
4
|
+
|
5
|
+
Fish0 is the plugin for read-only content websites with MongoDB storage. Works perfect with Rambler&Co CQRS projects
|
6
|
+
|
7
|
+
## Configuration
|
8
|
+
|
9
|
+
```yml
|
10
|
+
# config/mongo.yml
|
11
|
+
|
12
|
+
development:
|
13
|
+
hosts:
|
14
|
+
- 'localhost:27017'
|
15
|
+
params:
|
16
|
+
:database: project_development
|
17
|
+
|
18
|
+
production:
|
19
|
+
hosts:
|
20
|
+
- 'db.project.tld:27017'
|
21
|
+
params:
|
22
|
+
:database: project_production
|
23
|
+
```
|
24
|
+
|
25
|
+
## Repository
|
26
|
+
|
27
|
+
### Basic repository usage
|
28
|
+
|
29
|
+
This code will get first article with `slug: 'content123'` from `articles` MongoDB collection, and return content with class `Article`.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
Fish0::Repository.new(:articles)
|
33
|
+
.where(slug: 'content123')
|
34
|
+
.first!
|
35
|
+
```
|
36
|
+
|
37
|
+
By default Fish0::Repository will coerce `:entity_class` from `:collection`, so you can skip this parameter.
|
38
|
+
|
39
|
+
### Writing your own repository
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
# app/services/article_repository.rb
|
43
|
+
class ArticleRepository < Fish0::Repository
|
44
|
+
def initialize
|
45
|
+
super(:articles)
|
46
|
+
end
|
47
|
+
|
48
|
+
def published
|
49
|
+
where(visible: true, published_at: { '$lt': DateTime.now })
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# app/controllers/articles_controller.rb
|
54
|
+
class ArticlesController < ApplicationController
|
55
|
+
# ...
|
56
|
+
|
57
|
+
def show
|
58
|
+
@article = ArticleRepository.new.where(slug: params[:slug]).first!
|
59
|
+
end
|
60
|
+
|
61
|
+
# ...
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
## Pagination
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
# app/controllers/articles_controller.rb
|
69
|
+
class ArticlesController < ApplicationController
|
70
|
+
include Fish0::Concerns::Paginatable
|
71
|
+
|
72
|
+
def index
|
73
|
+
@articles = paginate(ArticleRepository.new.published)
|
74
|
+
end
|
75
|
+
|
76
|
+
# ...
|
77
|
+
|
78
|
+
protected
|
79
|
+
|
80
|
+
def per_page
|
81
|
+
31
|
82
|
+
end
|
83
|
+
|
84
|
+
# ...
|
85
|
+
end
|
86
|
+
```
|
87
|
+
|
88
|
+
## ViewModel
|
89
|
+
|
90
|
+
ViewModel concern wraps Virtus around your models. It also adds `#to_partial_path` and `#type` methods. Method `#to_partial_path` helps render your models via `render` helper.
|
91
|
+
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
# app/models/article.rb
|
95
|
+
class Article
|
96
|
+
include Fish0::Concerns::ViewModel
|
97
|
+
|
98
|
+
attribute :headline, String
|
99
|
+
attribute :slug, String
|
100
|
+
attribute :content, Array[Hash]
|
101
|
+
attribute :published_at, DateTime
|
102
|
+
|
103
|
+
# ...
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
## Cacheable
|
108
|
+
|
109
|
+
If you want your models to support `#cache_key` method and use Rails caching, you should include Fish0::Concerns::Cacheable to such models.
|
110
|
+
|
111
|
+
Your model should respond to `:updated_at` with DateTime object.
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
# app/models/article.rb
|
115
|
+
class Article
|
116
|
+
# ...
|
117
|
+
include Fish0::Concerns::Cacheable
|
118
|
+
|
119
|
+
# ...
|
120
|
+
end
|
121
|
+
|
122
|
+
# app/controllers/articles_controller.rb
|
123
|
+
class ArticlesController < ApplicationController
|
124
|
+
# ...
|
125
|
+
|
126
|
+
def show
|
127
|
+
@article = ArticleRepository.new.where(slug: params[:slug]).first!
|
128
|
+
if stale?(@article)
|
129
|
+
respond_to do |format|
|
130
|
+
format.html
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
# ...
|
136
|
+
end
|
137
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Fish0'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
Bundler::GemHelper.install_tasks
|
23
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Fish0
|
2
|
+
module Concerns
|
3
|
+
module Cacheable
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
def cache_key(*timestamp_names)
|
8
|
+
case
|
9
|
+
when timestamp_names.any?
|
10
|
+
timestamp = max_updated_column_timestamp(timestamp_names)
|
11
|
+
timestamp = timestamp.utc.to_s(:nsec)
|
12
|
+
"#{self.class.to_s.tableize}/#{slug}-#{timestamp}"
|
13
|
+
when timestamp = max_updated_column_timestamp
|
14
|
+
timestamp = timestamp.utc.to_s(:nsec)
|
15
|
+
"#{self.class.to_s.tableize}/#{slug}-#{timestamp}"
|
16
|
+
else
|
17
|
+
"#{self.class.to_s.tableize}/#{slug}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def timestamp_attributes_for_update
|
24
|
+
[:updated_at]
|
25
|
+
end
|
26
|
+
|
27
|
+
def max_updated_column_timestamp(timestamp_names = timestamp_attributes_for_update)
|
28
|
+
timestamp_names
|
29
|
+
.map { |attr| self[attr] }
|
30
|
+
.compact
|
31
|
+
.map(&:to_time)
|
32
|
+
.max
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Fish0
|
2
|
+
module Concerns
|
3
|
+
module Paginatable
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
helper_method :page
|
8
|
+
|
9
|
+
protected
|
10
|
+
|
11
|
+
def page
|
12
|
+
@page ||= (params[:page].to_i || 1)
|
13
|
+
end
|
14
|
+
|
15
|
+
def paginate(collection)
|
16
|
+
Fish0::Paginator.new(collection, page_number: page, per_page: per_page).all
|
17
|
+
end
|
18
|
+
|
19
|
+
def per_page
|
20
|
+
22
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Fish0
|
2
|
+
module Concerns
|
3
|
+
module ViewModel
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
include Virtus::Model
|
8
|
+
attribute :type, String
|
9
|
+
|
10
|
+
def type
|
11
|
+
(super || '').demodulize.underscore
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_partial_path
|
15
|
+
"#{type}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Fish0
|
2
|
+
class Paginator
|
3
|
+
include Enumerable
|
4
|
+
delegate :each, :all, :skip, :limit, :padding, to: :collection
|
5
|
+
|
6
|
+
attr_reader :collection
|
7
|
+
|
8
|
+
def initialize(collection, page_number: 1, per_page: 22, padding: 0)
|
9
|
+
@collection = collection || (fail ArgumentError)
|
10
|
+
@per = per_page
|
11
|
+
@page = page_number - 1
|
12
|
+
@padding = padding
|
13
|
+
per(per_page)
|
14
|
+
page(page_number)
|
15
|
+
end
|
16
|
+
|
17
|
+
def page(value = @page)
|
18
|
+
@page = value
|
19
|
+
skip(@page * @per + @padding)
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
def per(value = @per)
|
24
|
+
@per = value
|
25
|
+
skip(@page * @per + @padding)
|
26
|
+
limit(@per)
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
def padding(value)
|
31
|
+
@padding = value
|
32
|
+
page
|
33
|
+
skip
|
34
|
+
self
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module Fish0
|
2
|
+
class Repository
|
3
|
+
attr_reader :source,
|
4
|
+
:collection,
|
5
|
+
:conditions,
|
6
|
+
:order,
|
7
|
+
:skip_quantity,
|
8
|
+
:limit_quantity,
|
9
|
+
:entity_class
|
10
|
+
|
11
|
+
include Enumerable
|
12
|
+
|
13
|
+
delegate :find, to: :source
|
14
|
+
delegate :each, to: :all
|
15
|
+
|
16
|
+
def initialize(collection, entity_class = nil)
|
17
|
+
fail ArgumentError, "you should provide collection name" unless collection
|
18
|
+
@collection = collection
|
19
|
+
@source = Fish0.mongo_reader[collection]
|
20
|
+
@conditions = {}
|
21
|
+
@order = {}
|
22
|
+
@limit_quantity = 0
|
23
|
+
@skip_quantity = 0
|
24
|
+
@entity_class = entity_class || String(collection).singularize.camelize.constantize
|
25
|
+
end
|
26
|
+
|
27
|
+
def all
|
28
|
+
fetch.map(&to_entity)
|
29
|
+
end
|
30
|
+
|
31
|
+
def projection(values)
|
32
|
+
@projection = values
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
def first
|
37
|
+
element = fetch.limit(1).first
|
38
|
+
to_entity.call(element) if element
|
39
|
+
end
|
40
|
+
|
41
|
+
def first!
|
42
|
+
first || fail(RecordNotFound, "can't find in #{collection} with #{conditions}")
|
43
|
+
end
|
44
|
+
|
45
|
+
def where(query)
|
46
|
+
self.conditions.merge!(query)
|
47
|
+
self
|
48
|
+
end
|
49
|
+
|
50
|
+
def search(string)
|
51
|
+
where('$text' => { '$search' => string })
|
52
|
+
self
|
53
|
+
end
|
54
|
+
|
55
|
+
def order_by(query)
|
56
|
+
order.merge!(query)
|
57
|
+
self
|
58
|
+
end
|
59
|
+
|
60
|
+
def limit(value)
|
61
|
+
@limit_quantity = value
|
62
|
+
self
|
63
|
+
end
|
64
|
+
|
65
|
+
def skip(value)
|
66
|
+
@skip_quantity = value
|
67
|
+
self
|
68
|
+
end
|
69
|
+
|
70
|
+
def follow_after(item)
|
71
|
+
fail ArgumentError unless item.respond_to?(:published_at)
|
72
|
+
|
73
|
+
order_by(published_at: -1).where(published_at: { '$lt': item.published_at })
|
74
|
+
end
|
75
|
+
|
76
|
+
protected
|
77
|
+
|
78
|
+
def fetch
|
79
|
+
scoped = find(conditions, sort: order)
|
80
|
+
scoped = scoped.projection(@projection) if @projection
|
81
|
+
scoped = scoped.skip(skip_quantity) if skip_quantity > 0
|
82
|
+
scoped = scoped.limit(limit_quantity) if limit_quantity > 0
|
83
|
+
scoped
|
84
|
+
end
|
85
|
+
|
86
|
+
def to_entity
|
87
|
+
-> (attrs) { entity_class.new(attrs) }
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/lib/fish0.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'mongo'
|
2
|
+
require 'fish0/version'
|
3
|
+
require 'fish0/record_not_found'
|
4
|
+
require 'fish0/repository'
|
5
|
+
require 'fish0/paginator'
|
6
|
+
require 'fish0/concerns/cacheable'
|
7
|
+
require 'fish0/concerns/paginatable'
|
8
|
+
require 'fish0/concerns/view_model'
|
9
|
+
|
10
|
+
module Fish0
|
11
|
+
class << self
|
12
|
+
def mongo_reader
|
13
|
+
Mongo::Logger.logger = mongo_config['logger'] || Rails.logger
|
14
|
+
Mongo::Client.new(mongo_config['hosts'], mongo_config['params'])
|
15
|
+
end
|
16
|
+
|
17
|
+
def mongo_config
|
18
|
+
@mongo_config || Rails.application.config_for(:mongo)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fish0
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dmitry Zuev
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mongo
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: virtus
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.35'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.35'
|
69
|
+
description: Plugin for read-only content websites with MongoDB storage. Works perfect
|
70
|
+
with Rambler&Co CQRS projects
|
71
|
+
email:
|
72
|
+
- d.zuev@rambler-co.ru
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- MIT-LICENSE
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- lib/fish0.rb
|
81
|
+
- lib/fish0/concerns/cacheable.rb
|
82
|
+
- lib/fish0/concerns/paginatable.rb
|
83
|
+
- lib/fish0/concerns/view_model.rb
|
84
|
+
- lib/fish0/paginator.rb
|
85
|
+
- lib/fish0/record_not_found.rb
|
86
|
+
- lib/fish0/repository.rb
|
87
|
+
- lib/fish0/version.rb
|
88
|
+
- lib/tasks/fish0_tasks.rake
|
89
|
+
homepage: https://github.com/rambler-digital-solutions/fish0
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.4.5.1
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Plugin for read-only content websites
|
113
|
+
test_files: []
|