AR2H 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/AR2H.gemspec +1 -1
- data/lib/AR2H.rb +1 -0
- data/lib/AR2H/concerns/active_record/base.rb +5 -2
- data/lib/AR2H/concerns/active_record/relation.rb +5 -2
- data/lib/AR2H/executor.rb +26 -10
- data/lib/AR2H/logger.rb +7 -0
- data/lib/AR2H/presenters/collection.rb +5 -7
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ef4662b01dddfa99efdc01e395b75490a4fe7b2
|
4
|
+
data.tar.gz: 4422ee47ef65b3aa1d5188d3d009f911eb1be652
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9fb88d0f765aa69e8f6d0e4b8523c4e07e505fb666ca1a02c2f27a502e54049967d101157de1e7ab426fa117f11ed84e078f842eea456be96b4bb674ee8c943d
|
7
|
+
data.tar.gz: 5d4ea13f2f580f8c9bdefa84fef462030504b396fdbec84d5dbec00f4c271c23b5597cc61ffc722b57572bc1730b1e730a14591db24953c6528961e9f85dfba5
|
data/AR2H.gemspec
CHANGED
data/lib/AR2H.rb
CHANGED
@@ -6,8 +6,11 @@ module AR2H
|
|
6
6
|
module Base
|
7
7
|
extend ActiveSupport::Concern
|
8
8
|
|
9
|
-
def present(presenter
|
10
|
-
|
9
|
+
def present(presenter: :default)
|
10
|
+
model = self.class
|
11
|
+
ActiveSupport::Notifications.instrument('ar2h.presenting', model: model, presenter: presenter) do
|
12
|
+
Executor.run(self, presenter)
|
13
|
+
end
|
11
14
|
end
|
12
15
|
|
13
16
|
def presenters
|
@@ -6,8 +6,11 @@ module AR2H
|
|
6
6
|
module Relation
|
7
7
|
extend ActiveSupport::Concern
|
8
8
|
|
9
|
-
def present(presenter
|
10
|
-
|
9
|
+
def present(presenter: :default)
|
10
|
+
model = self.model.to_s.pluralize
|
11
|
+
ActiveSupport::Notifications.instrument('ar2h.presenting', model: model, presenter: presenter) do
|
12
|
+
Executor.run(self, presenter, multiple = true)
|
13
|
+
end
|
11
14
|
end
|
12
15
|
end
|
13
16
|
|
data/lib/AR2H/executor.rb
CHANGED
@@ -1,12 +1,27 @@
|
|
1
1
|
module AR2H
|
2
2
|
class Executor
|
3
|
-
|
4
|
-
|
3
|
+
class << self
|
4
|
+
def run(object, presenter_name, multiple = false)
|
5
|
+
presenter = object.presenters.find(presenter_name)
|
6
|
+
unless presenter
|
7
|
+
model = multiple ? object.model : object.class
|
8
|
+
raise ArgumentError.new("#{model}: presenter #{presenter_name} not found")
|
9
|
+
end
|
10
|
+
|
11
|
+
case multiple
|
12
|
+
when false
|
13
|
+
new(object, presenter).execute
|
14
|
+
when true
|
15
|
+
object.map { |el| new(el, presenter).execute }
|
16
|
+
else
|
17
|
+
raise ArgumentError.new('Bad multiple argument')
|
18
|
+
end
|
19
|
+
end
|
5
20
|
end
|
6
21
|
|
7
22
|
def initialize(object, presenter)
|
8
23
|
@object = object
|
9
|
-
@presenter =
|
24
|
+
@presenter = presenter
|
10
25
|
@result = Hash.new
|
11
26
|
end
|
12
27
|
|
@@ -27,17 +42,18 @@ module AR2H
|
|
27
42
|
attrs.each { |attr| attr(attr) }
|
28
43
|
end
|
29
44
|
|
30
|
-
def has_one(model,
|
31
|
-
_has_branch(model,
|
45
|
+
def has_one(model, presenter: nil)
|
46
|
+
_has_branch(model, presenter, false)
|
32
47
|
end
|
33
48
|
|
34
|
-
def has_many(model,
|
35
|
-
_has_branch(model,
|
49
|
+
def has_many(model, presenter: nil)
|
50
|
+
_has_branch(model, presenter, true)
|
36
51
|
end
|
37
52
|
|
38
|
-
def _has_branch(
|
39
|
-
child_presenter =
|
40
|
-
|
53
|
+
def _has_branch(model, presenter, multiple) # :nodoc:
|
54
|
+
child_presenter = presenter || @presenter.name
|
55
|
+
child_object = object.send(model)
|
56
|
+
@result[model] = Executor.run(child_object, child_presenter, multiple)
|
41
57
|
end
|
42
58
|
end
|
43
59
|
end
|
data/lib/AR2H/logger.rb
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
ActiveSupport::Notifications.subscribe('ar2h.presenting') do |_, started, finished, _, payload|
|
2
|
+
time = (1000.0 * (finished - started)).round(1)
|
3
|
+
model = payload[:model]
|
4
|
+
presenter = payload[:presenter]
|
5
|
+
message = " Presented #{model} as #{presenter} in #{time}ms"
|
6
|
+
ActionController::Base.logger.info message
|
7
|
+
end if defined? 'action_controller'
|
@@ -1,14 +1,14 @@
|
|
1
1
|
module AR2H
|
2
2
|
module Presenters
|
3
3
|
class Collection
|
4
|
-
def initialize(
|
5
|
-
@
|
4
|
+
def initialize(model)
|
5
|
+
@model = model
|
6
6
|
@presenters = Array.new
|
7
7
|
end
|
8
8
|
|
9
9
|
def add(presenter)
|
10
10
|
if self.include? presenter.name
|
11
|
-
raise Exception.new("#{@
|
11
|
+
raise Exception.new("#{@model}: presenter #{presenter.name} is already exists")
|
12
12
|
else
|
13
13
|
@presenters << presenter
|
14
14
|
end
|
@@ -19,7 +19,7 @@ module AR2H
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def list
|
22
|
-
@presenters.map{ |presenter| presenter.name }
|
22
|
+
@presenters.map { |presenter| presenter.name }
|
23
23
|
end
|
24
24
|
|
25
25
|
def include?(name)
|
@@ -27,9 +27,7 @@ module AR2H
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def find(name)
|
30
|
-
|
31
|
-
raise Exception.new("#{@model_name}: presenter #{name} not found") unless presenter
|
32
|
-
presenter
|
30
|
+
@presenters.find { |presenter| presenter.name == name }
|
33
31
|
end
|
34
32
|
end
|
35
33
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: AR2H
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eugene Yak
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -132,6 +132,7 @@ files:
|
|
132
132
|
- lib/AR2H/concerns/active_record/base.rb
|
133
133
|
- lib/AR2H/concerns/active_record/relation.rb
|
134
134
|
- lib/AR2H/executor.rb
|
135
|
+
- lib/AR2H/logger.rb
|
135
136
|
- lib/AR2H/presenters/collection.rb
|
136
137
|
- lib/AR2H/presenters/presenter.rb
|
137
138
|
homepage: https://gitlab.com/yak/AR2H
|