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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 78cd604ee19ed84f6cc042874bb82930dc92c408
4
- data.tar.gz: 7d3a2974b396088dac1a9af9e969f231013c29e4
3
+ metadata.gz: 8ef4662b01dddfa99efdc01e395b75490a4fe7b2
4
+ data.tar.gz: 4422ee47ef65b3aa1d5188d3d009f911eb1be652
5
5
  SHA512:
6
- metadata.gz: 595e245e38640802f4156a3b5af38bc6ef73a5ec1c695acc4e381972eaa28ecc8d9433834532448468e3f29b8c4ddbc992d7c19b454502d8b2afbc7f95f55c06
7
- data.tar.gz: b84c618822e9105e0992d75e6baa3834356d018b0e8a047cdc7eb15ff04746efd2f01e78f06aa65dc90ac926fbded3ae5fe66fc2f4c79e56fdf0f20c6d2c396d
6
+ metadata.gz: 9fb88d0f765aa69e8f6d0e4b8523c4e07e505fb666ca1a02c2f27a502e54049967d101157de1e7ab426fa117f11ed84e078f842eea456be96b4bb674ee8c943d
7
+ data.tar.gz: 5d4ea13f2f580f8c9bdefa84fef462030504b396fdbec84d5dbec00f4c271c23b5597cc61ffc722b57572bc1730b1e730a14591db24953c6528961e9f85dfba5
data/AR2H.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'AR2H'
7
- spec.version = '0.0.1'
7
+ spec.version = '0.0.2'
8
8
  spec.authors = ['Eugene Yak']
9
9
  spec.email = ['GeneAYak@gmail.com']
10
10
 
data/lib/AR2H.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'AR2H/executor'
2
+ require 'AR2H/logger'
2
3
 
3
4
  require 'AR2H/concerns/active_record/base'
4
5
  require 'AR2H/concerns/active_record/relation'
@@ -6,8 +6,11 @@ module AR2H
6
6
  module Base
7
7
  extend ActiveSupport::Concern
8
8
 
9
- def present(presenter = :default)
10
- Executor.run(self, presenter)
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 = :default)
10
- self.map { |e| Executor.run(e, presenter) }
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
- def self.run(object, presenter)
4
- self.new(object, presenter).execute
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 = object.presenters.find 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, &block)
31
- _has_branch(model, &block)
45
+ def has_one(model, presenter: nil)
46
+ _has_branch(model, presenter, false)
32
47
  end
33
48
 
34
- def has_many(model, &block)
35
- _has_branch(model, &block)
49
+ def has_many(model, presenter: nil)
50
+ _has_branch(model, presenter, true)
36
51
  end
37
52
 
38
- def _has_branch(child, &block) # :nodoc:
39
- child_presenter = block ? block.call : @presenter.name
40
- @result[child] = object.send(child).present(child_presenter)
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
@@ -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(model_name)
5
- @model_name = model_name
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("#{@model_name}: presenter #{presenter.name} is already exists")
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
- presenter = @presenters.find{ |hasher| hasher.name == name }
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.1
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-29 00:00:00.000000000 Z
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