as_json_presentable 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 007b24299611eae692e4e5e14181cb7459162576
4
+ data.tar.gz: 5b1c8f3a28dcaa5ee785c35135ad4a7e51c5eb7e
5
+ SHA512:
6
+ metadata.gz: d78c0343bc8eb1371e921c52033064ff05abc36066d65f1ac739a3a97ecf2539bee37b08fcecb30e41592b72d97f5624b7313e92e662c2e42af140b1c8ba631b
7
+ data.tar.gz: fffe8e476ceadbac816c21db32f0979ba69aa31317865aa164addbc00cf52c285dc3317817c9343516b32d816cdc2c8897762fcb5892fa98bf091d35f33c0887
@@ -0,0 +1,55 @@
1
+ require 'as_json_presentable/presenter'
2
+
3
+ module AsJsonPresentable
4
+
5
+ def self.included(klass)
6
+ klass.extend ClassMethods
7
+ end
8
+
9
+ # Attempts to delegate to the presenter class's `#as_json`
10
+ # if it exists, and falls back to the parent class's
11
+ # implementation if it does not.
12
+ def as_json(options=nil)
13
+ if options && options[:presenter_action] && json_presenter_class
14
+ json_presenter_class.new(self).as_json(options)
15
+ else
16
+ super(options)
17
+ end
18
+ end
19
+
20
+ # Returns the presenter class, or nil if
21
+ # no presenter class exists
22
+ def json_presenter_class
23
+ self.class.json_presenter_class
24
+ end
25
+
26
+ module ClassMethods
27
+
28
+ # Allows the user to explicitly set the presenter class
29
+ # if the default naming convention isn't desired.
30
+ def define_json_presenter_class(klass)
31
+ @json_presenter_class = klass
32
+ end
33
+
34
+ # Returns the presenter class if it has been explicitly
35
+ # set or can be implied through naming conventions, or
36
+ # returns nil if no presenter class exists.
37
+ #
38
+ # By default, the presenter class is inferred to be
39
+ # named after the model class, but with the suffix
40
+ # of "Presenter".
41
+ #
42
+ # e.g. If you have a model class called `Foo`,
43
+ # its presenter is assumed to be called `FooPresenter`
44
+ #
45
+ # Alternatively, the presenter class can be explicitly
46
+ # defined with `::defined_json_presenter_class`
47
+ def json_presenter_class
48
+ return @json_presenter_class if defined?(@json_presenter_class)
49
+
50
+ klass_name = "#{self.name}Presenter"
51
+ @json_presenter_class = Module.const_get(klass_name) if Module.const_defined?(klass_name)
52
+ end
53
+ end
54
+
55
+ end
@@ -0,0 +1,45 @@
1
+ module AsJsonPresentable
2
+ # Base JSON presenter class
3
+ class Presenter
4
+ attr_reader :resource
5
+
6
+ def initialize(resource)
7
+ @resource = resource
8
+ end
9
+
10
+ # Looks for the option `:presenter_action`. If it exists,
11
+ # looks for a method called `#as_<presenter_action>_json`
12
+ # and delegates to that method.
13
+ # If `:presenter_action` isn't in the options, or it
14
+ # references a method that the presenter doesn't respond to,
15
+ # falls back to the resource's `#as_json` implementation.
16
+ #
17
+ # e.g.
18
+ #
19
+ # `presenter.as_json(presenter_action: :index)`
20
+ #
21
+ # will delegate to
22
+ #
23
+ # `presenter.as_index_json`
24
+ def as_json(options=nil)
25
+ presenter_action = (options || {}).delete(:presenter_action)
26
+
27
+ if has_presenter_method?(presenter_action)
28
+ send(presenter_method(presenter_action), options)
29
+ else
30
+ resource.as_json(options)
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def has_presenter_method?(presenter_action)
37
+ respond_to?(presenter_method(presenter_action))
38
+ end
39
+
40
+ def presenter_method(presenter_action)
41
+ "as_#{presenter_action}_json"
42
+ end
43
+
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: as_json_presentable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jason Rush
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: This is a simple implementation of the presenter pattern for JSON presentation.
56
+ email: jason@pandastrike.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/as_json_presentable.rb
62
+ - lib/as_json_presentable/presenter.rb
63
+ homepage: http://pandastrike.com/
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.2.2
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: JSON presenter
87
+ test_files: []
88
+ has_rdoc: