hc-presenter 0.1.0
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 +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/hc-presenter.gemspec +25 -0
- data/lib/hc/presenter/base.rb +55 -0
- data/lib/hc/presenter/helper_methods.rb +11 -0
- data/lib/hc/presenter/version.rb +5 -0
- data/lib/hc/presenter.rb +28 -0
- metadata +69 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 44fa3d12848ef04a66f2a3393a5641f4d5a97e753ca89edf3cdaa67925336a93
|
4
|
+
data.tar.gz: '0872b4725677dc311b8dae5111702701313bd2f2acd76d3cce1be81fed732167'
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a151ba2e00ef1334c4aa10d00696e5495f78ebc39acb85e9aa1e9c12f8b01a4a2881847718dcd18773a5612e3549bec79147ee3f6ef6c892daf72188296656b9
|
7
|
+
data.tar.gz: d841741b698b9913d04d166cc6c80896324bcae50b16fa366ab31c804aa3d193c4b15709b1392a56c16bc724d47c7fdfea215244f653b9bf9d71bba6cb2f570b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Hockey Community
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# HC::Presenter
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/hc/presenter`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'hc-presenter'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install hc-presenter
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hc-presenter.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hc/presenter"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "hc/presenter/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hc-presenter"
|
8
|
+
spec.version = "0.1.0"
|
9
|
+
spec.authors = ["Jack Hayter"]
|
10
|
+
spec.email = ["jack@hockey-community.com"]
|
11
|
+
|
12
|
+
spec.summary = "A super simple object serializer"
|
13
|
+
spec.description = "Transforms objects and collections into hashes for rendering as JSON"
|
14
|
+
spec.homepage = "https://github.com/HockeyCommunity/presenter"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler"
|
25
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module HC
|
3
|
+
module Presenter
|
4
|
+
|
5
|
+
# Creates a platform for other presenters to serialize objects into hashes
|
6
|
+
#
|
7
|
+
class Base
|
8
|
+
|
9
|
+
# Initialization of required and optional instance variables
|
10
|
+
#
|
11
|
+
def initialize(object:, controller_context: nil, options: {})
|
12
|
+
@object = object
|
13
|
+
@options = options.try(:to_h).to_h.symbolize_keys
|
14
|
+
@controller_context = controller_context
|
15
|
+
end
|
16
|
+
|
17
|
+
# Abstraction method for nicer top-level access to object context with a
|
18
|
+
# friendly name, plus allow access to any options that were passed in.
|
19
|
+
#
|
20
|
+
def self.presents(name)
|
21
|
+
define_method(name) do
|
22
|
+
@object
|
23
|
+
end
|
24
|
+
define_method(:options) do
|
25
|
+
@options
|
26
|
+
end
|
27
|
+
define_method(:controller_context) do
|
28
|
+
@controller_context
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def present(object, options: nil, method: nil)
|
33
|
+
if @controller_context
|
34
|
+
@controller_context.present(object, options: options || @options, method: method)
|
35
|
+
else
|
36
|
+
HC::Presenter.present(object, method: method, options: options)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
# This method passes off missing methods to the controller context,
|
43
|
+
# thus allowing us access to that controller's methods in the presenters.
|
44
|
+
# This should be used with care, and should only really be used for
|
45
|
+
# authorization control and permissions-based conditional content.
|
46
|
+
#
|
47
|
+
def method_missing(*args, &block)
|
48
|
+
return nil unless @controller_context
|
49
|
+
@controller_context.send(*args, &block)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
data/lib/hc/presenter.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require "hc/presenter/base"
|
2
|
+
require "hc/presenter/helper_methods"
|
3
|
+
require "hc/presenter/version"
|
4
|
+
|
5
|
+
module HC
|
6
|
+
module Presenter
|
7
|
+
|
8
|
+
def self.present(object, klass: nil, controller_context: nil, method: nil, options: {})
|
9
|
+
if object.nil?
|
10
|
+
return object
|
11
|
+
elsif object.is_a?(DateTime) || object.is_a?(Time)
|
12
|
+
return object.to_formatted_s(:iso8601)
|
13
|
+
elsif object.respond_to?(:count)
|
14
|
+
content = object.collect{ |o| present(o, klass: klass, controller_context: controller_context, method: method, options: options) }
|
15
|
+
return content
|
16
|
+
else
|
17
|
+
klass ||= "#{object.class}Presenter".constantize
|
18
|
+
presenter_object = klass.new(object: object, controller_context: controller_context, options: options)
|
19
|
+
if !method.nil? && presenter_object.respond_to?(method)
|
20
|
+
presenter_object.send(method)
|
21
|
+
else
|
22
|
+
presenter_object.send(:format)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hc-presenter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jack Hayter
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
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
|
+
description: Transforms objects and collections into hashes for rendering as JSON
|
28
|
+
email:
|
29
|
+
- jack@hockey-community.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- Gemfile
|
36
|
+
- LICENSE.txt
|
37
|
+
- README.md
|
38
|
+
- Rakefile
|
39
|
+
- bin/console
|
40
|
+
- bin/setup
|
41
|
+
- hc-presenter.gemspec
|
42
|
+
- lib/hc/presenter.rb
|
43
|
+
- lib/hc/presenter/base.rb
|
44
|
+
- lib/hc/presenter/helper_methods.rb
|
45
|
+
- lib/hc/presenter/version.rb
|
46
|
+
homepage: https://github.com/HockeyCommunity/presenter
|
47
|
+
licenses:
|
48
|
+
- MIT
|
49
|
+
metadata: {}
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubygems_version: 3.0.3
|
66
|
+
signing_key:
|
67
|
+
specification_version: 4
|
68
|
+
summary: A super simple object serializer
|
69
|
+
test_files: []
|