presenter_object 0.1.7 → 0.2.0

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: 4df026043272b232dbca2282927d636dd3591f69
4
- data.tar.gz: c7524f4981010b561c2ed04a43a763a19fba59ab
3
+ metadata.gz: 518f904deecb811d75fd12215e6cac057607d178
4
+ data.tar.gz: 39c9f1668de7b993ba331b7b023827f1c264855a
5
5
  SHA512:
6
- metadata.gz: c58db3651c539442b1b907bb8a99609d4eca0d8c495b01652524fb3f6ff0256c9ab80889a4259f9bc23ab1f282291c55cbe185e62e45826cce288ce2ad159db3
7
- data.tar.gz: 4deff8422e18b7bff8eccfdc87cfb67b02d418fbca9215a7622a82b2339f02ed760b938cb6d3d72769ef505c4ef992a77333f81b5be1128ac2d478d970c9fca0
6
+ metadata.gz: 3b8f0bef1612d629afc6ff87521218c61aa3360ad2ee5f194d49015f092e38b6e17030d90bcc1875915734ca1c1fe0fc41b0ec5f04b4b8d1eb28538997f22adb
7
+ data.tar.gz: a8d9f016f2459174104ae83d20ef9e7ebe1b9bb2d2fc4e52ec04154071dd3363a7decfd14047b69bdbf68f96a91c4f3e22216aebe6781c566432fb14e44c26b8
@@ -17,6 +17,9 @@
17
17
  # document.class # is the wrapped object's class, helps it behave like models do in forms, link helpers etc.
18
18
  #
19
19
  class PresenterObject::Base
20
+ include PresenterObject::Delegation
21
+ attr_reader :object, :class, :to_param, :view_context
22
+
20
23
  class << self
21
24
  def presents(class_name)
22
25
  @class_name = class_name.to_s.classify
@@ -32,30 +35,10 @@ class PresenterObject::Base
32
35
  end
33
36
  end
34
37
 
35
- attr_reader :object, :class, :to_param, :view_context
36
-
37
38
  def initialize(object, view_context = nil)
38
39
  @object = object
39
40
  @class = object.class # impersonate object's class
40
41
  @to_param = object.id # let link helpers know the object's id
41
42
  @view_context = view_context
42
43
  end
43
-
44
- def method_missing(name, *args, &block)
45
- if @object.respond_to? name
46
- @object.send name, *args, &block
47
- elsif @view_context.respond_to? name
48
- @view_context.send name, *args, &block
49
- else
50
- super
51
- end
52
- end
53
-
54
- def respond_to?(name, include_private = false)
55
- if @object.respond_to?(name, include_private) || @view_context.respond_to?(name, include_private)
56
- true
57
- else
58
- super
59
- end
60
- end
61
44
  end
@@ -0,0 +1,28 @@
1
+ # Presenter superclass for collections. Create your own collection presenter by sub-classing `PresenterObject::Collection`.
2
+ #
3
+ # e.g:
4
+ #
5
+ # class DocumentCollectionPresenter << PresenterObject::Collection
6
+ # .. add methods here ..
7
+ # end
8
+ #
9
+ # Create a presenter instance by giving it a model instance e.g:
10
+ #
11
+ # documents = DocumentCollectionPresenter.new Document.where(publish: true)
12
+ # documents.method_that_presents_collection
13
+ #
14
+ class PresenterObject::Collection
15
+ include Enumerable
16
+ include PresenterObject::Delegation
17
+ attr_reader :collection, :view_context
18
+ alias_method :object, :collection
19
+
20
+ def initialize(collection, view_context = nil)
21
+ @collection = collection
22
+ @view_context = view_context
23
+ end
24
+
25
+ def each
26
+ @collection.each { |object| yield object.presenterize view_context }
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ module PresenterObject::Delegation
2
+ def method_missing(name, *args, &block)
3
+ if object.respond_to? name
4
+ object.send name, *args, &block
5
+ elsif view_context.respond_to? name
6
+ view_context.send name, *args, &block
7
+ else
8
+ super
9
+ end
10
+ end
11
+
12
+ def respond_to?(name, include_private = false)
13
+ if object.respond_to?(name, include_private) || view_context.respond_to?(name, include_private)
14
+ true
15
+ else
16
+ super
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module PresenterObject
2
- VERSION = "0.1.7"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,6 +1,10 @@
1
+ require "active_support/core_ext/string/inflections"
2
+ require "active_support/concern"
1
3
  require "presenter_object/version"
4
+ require "presenter_object/delegation"
2
5
  require "presenter_object/base"
3
6
  require "presenter_object/presentable"
7
+ require "presenter_object/collection"
4
8
 
5
9
  module PresenterObject
6
10
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: presenter_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Diego Salazar
@@ -82,6 +82,8 @@ files:
82
82
  - bin/setup
83
83
  - lib/presenter_object.rb
84
84
  - lib/presenter_object/base.rb
85
+ - lib/presenter_object/collection.rb
86
+ - lib/presenter_object/delegation.rb
85
87
  - lib/presenter_object/presentable.rb
86
88
  - lib/presenter_object/version.rb
87
89
  - presenter_object.gemspec