presenter_rails 0.0.2 → 1.0.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: b6e00843174079f2bc4d60bb07ea2ecc553a6087
4
- data.tar.gz: 39f1c037955517799a8d12f5bc57476edceb744e
3
+ metadata.gz: 7369b035085593e7c248402cfc1fba20a1dfb15a
4
+ data.tar.gz: 0e785985be1e307d9d88ee6159521fab37b6fdd2
5
5
  SHA512:
6
- metadata.gz: 59378922fa39c27917f60bb737165527651cd43302b4f68abe2f8bbf4870677ff4087318c764b353b09865e3f543ee3f13cd4e5aef4de39eca5f4beab843e809
7
- data.tar.gz: 377ce22e472bef7a617c9a9e4bc8a0b0645543f8f32d01af13e65bbbeea63491a6838fc2b8a8eb2064415861923556cb810fb6194c2680834803b9d5854af282
6
+ metadata.gz: 8750fc439086bce43b647378fab34d1f2205a149574f036a3333ab2f7084090710ca431744618d1492a9b56064df1d043b7fca7001192aa17208cefd1c7463ac
7
+ data.tar.gz: 51bd7a3ba559607f64f9667a07cc54da40415abb49b862d105e038cbf153f4f3790ae76f99b5c10bb729b4427a689a697cfb4811f365daa0dd6060f2d2ae356f
@@ -4,21 +4,23 @@ module PresenterRails
4
4
  include SimpleMemoizer
5
5
 
6
6
  module ClassMethods
7
- def present(*methods)
8
- methods.flatten!
9
- presenter_methods = *Presenter.presenter_method_names(methods)
10
- enforce_private_methods!(presenter_methods)
11
7
 
12
- presenter_method *methods
13
- memoize presenter_methods
8
+ # Can take a list of presenter methods, or a name and block for a presenter method
9
+ def present(*methods, &block)
10
+ presenter_methods = methods.map {|name| Presenter.method_name_for(name) }
11
+ define_presenter_method!(presenter_methods, &block) if block_given?
12
+
13
+ expose_presenter *methods
14
+ memoize *presenter_methods
14
15
  end
15
16
 
16
- def presenter_method(*method_names)
17
+ # Exposes a presenter method to the view for each provided name
18
+ def expose_presenter(*method_names)
17
19
  presenters_module = Module.new do
18
20
  method_names.each do |name|
19
21
  module_eval <<-ruby_eval, __FILE__, __LINE__ + 1
20
22
  def #{name}
21
- controller.send('#{Presenter.presenter_method_name(name)}')
23
+ controller.send('#{Presenter.method_name_for(name)}')
22
24
  end
23
25
  ruby_eval
24
26
  end
@@ -28,23 +30,20 @@ module PresenterRails
28
30
 
29
31
  private
30
32
 
31
- def enforce_private_methods!(methods)
32
- method_set = methods.to_set
33
- if self.instance_methods.any? {|method| method_set.include?(method) }
34
- methods = method_set.select {|method| self.instance_method?(method) }
35
- Kernel.abort "[ERROR] You are exposing presenters by the `#{methods.join(', ')}` method, " \
36
- "which is either a non private method or overrides an existing method of the same name. " \
37
- "Consider using a different presenter name\n" \
38
- "#{caller.first}"
33
+ # Defines a private presenter method that invokes the provided block
34
+ def define_presenter_method!(methods, &block)
35
+ if methods.size != 1
36
+ Kernel.abort "[ERROR] You are providing a block for the `#{methods.join(', ')}` methods, " \
37
+ "but you can only provide a block for a single presenter at a time.\n #{caller.second}"
39
38
  end
39
+ presenter_method = methods.first
40
+ define_method presenter_method, &block
41
+ private presenter_method
40
42
  end
41
43
  end
42
44
 
43
- def self.presenter_method_names(method_names)
44
- method_names.map {|name| presenter_method_name(name) }
45
- end
46
-
47
- def self.presenter_method_name(name)
45
+ # Name of the presenter methods
46
+ def self.method_name_for(name)
48
47
  "#{name}_presenter"
49
48
  end
50
49
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: presenter_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Máximo Mussini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-25 00:00:00.000000000 Z
11
+ date: 2014-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport