chic 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rubocop.yml +8 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +103 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/chic.gemspec +30 -0
- data/lib/chic.rb +50 -0
- data/lib/chic/configuration.rb +25 -0
- data/lib/chic/errors.rb +6 -0
- data/lib/chic/formats.rb +106 -0
- data/lib/chic/formatter.rb +12 -0
- data/lib/chic/formatters.rb +3 -0
- data/lib/chic/formatters/nil.rb +24 -0
- data/lib/chic/helpers.rb +3 -0
- data/lib/chic/helpers/view.rb +26 -0
- data/lib/chic/presentable.rb +26 -0
- data/lib/chic/presenter.rb +61 -0
- data/lib/chic/presenters.rb +3 -0
- data/lib/chic/presenters/nil.rb +8 -0
- data/lib/chic/presents.rb +125 -0
- data/lib/chic/version.rb +5 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a6ee27baaf52ccaffd000c9ffc37ba80e929ab93386c2f1dc2fb3d419d9cd86e
|
4
|
+
data.tar.gz: aebe4ade08bfff97f1fa3f4a96334dc4cb5cb9efdcbc108d6b00e1f81e781f1c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 200473240b8f99cbef6229c0181a7e820df796b744f199f0b25bcb22656109c0338b14c63385f8fe6c6eaa06027bc97a33edd8e0635f995eb974d1635f449b48
|
7
|
+
data.tar.gz: 1b0dc1cc823cfd555e19f4c6aea0306a852fcef8ff9f66bb1c3f1c7189a6003368fb7f5497a825311c24dc65d8551e3779cd53f0c1da2f4192d8ac6f358f6115
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at team@collabcollective.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Lawrance Shepstone
|
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,103 @@
|
|
1
|
+
# Chic
|
2
|
+
|
3
|
+
Opinionated presentation layer comprised of presenters and formatters.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'chic'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install chic
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Being Presentable
|
22
|
+
|
23
|
+
Make objects easily presentable by:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class Foo
|
27
|
+
include Chic::Presentable
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
### Creating Presenters
|
32
|
+
|
33
|
+
Present presentables with a presenter by inheriting from `Chic::Presenter`:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
class FooPresenter < Chic::Presenter
|
37
|
+
# ...
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
You can also include `Chic::Presents` and `Chic::Formats` as needed:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
class FooPresenter
|
45
|
+
include Chic::Formats
|
46
|
+
include Chic::Presents
|
47
|
+
# ...
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
**Note:** You need to make sure that the object being presented and the context in which it is being presented, for example the view, are accessible through `object` and `context` on the presenter instance respectively.
|
52
|
+
|
53
|
+
### Using Presenters
|
54
|
+
|
55
|
+
Presenters should be instantiated from views using `.present`:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
<% FooPresenter.present @foo do |foo_presenter, _foo| %>
|
59
|
+
<!-- ... -->
|
60
|
+
<% end %>
|
61
|
+
```
|
62
|
+
|
63
|
+
Collections can be presented using `.present_each`:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
<% FooPresenter.present_each @foos do |foo_presenter, _foo| %>
|
67
|
+
<!-- ... -->
|
68
|
+
<% end %>
|
69
|
+
```
|
70
|
+
|
71
|
+
If you've made use of the view helpers, you can drop the class name:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
<% present @foo do |foo_presenter, _foo| %>
|
75
|
+
<!-- ... -->
|
76
|
+
<% end %>
|
77
|
+
```
|
78
|
+
|
79
|
+
And:
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
<% present_each @foo do |foo_presenter, _foo| %>
|
83
|
+
<!-- ... -->
|
84
|
+
<% end %>
|
85
|
+
```
|
86
|
+
|
87
|
+
## Development
|
88
|
+
|
89
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
90
|
+
|
91
|
+
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).
|
92
|
+
|
93
|
+
## Contributing
|
94
|
+
|
95
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/collcoll/chic. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
96
|
+
|
97
|
+
## License
|
98
|
+
|
99
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
100
|
+
|
101
|
+
## Code of Conduct
|
102
|
+
|
103
|
+
Everyone interacting in the Chic project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/chic/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'chic'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/chic.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'chic/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'chic'
|
9
|
+
spec.version = Chic::VERSION
|
10
|
+
spec.authors = ['Lawrance Shepstone']
|
11
|
+
spec.email = ['lawrance@collabcollective.com']
|
12
|
+
|
13
|
+
spec.summary = 'Opinionated presentation layer comprised of presenters and formatters.'
|
14
|
+
spec.homepage = 'https://github.com/collcoll/chic'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = 'exe'
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ['lib']
|
25
|
+
|
26
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
27
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
28
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
29
|
+
spec.add_development_dependency 'rubocop', '>= 0.49.0'
|
30
|
+
end
|
data/lib/chic.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'chic/version'
|
4
|
+
require 'chic/errors'
|
5
|
+
require 'chic/helpers'
|
6
|
+
|
7
|
+
require 'chic/formats'
|
8
|
+
require 'chic/formatter'
|
9
|
+
require 'chic/formatters'
|
10
|
+
|
11
|
+
require 'chic/presents'
|
12
|
+
require 'chic/presenter'
|
13
|
+
require 'chic/presenters'
|
14
|
+
require 'chic/presentable'
|
15
|
+
|
16
|
+
require 'chic/configuration'
|
17
|
+
|
18
|
+
# = Chic
|
19
|
+
#
|
20
|
+
# Opinionated presentation layer comprised of presenters and formatters.
|
21
|
+
#
|
22
|
+
# @see Chic::Presenter
|
23
|
+
# @see Chic::Presentable
|
24
|
+
# @see Chic::Helpers::View
|
25
|
+
module Chic
|
26
|
+
module_function
|
27
|
+
|
28
|
+
# Gets the configuration object.
|
29
|
+
#
|
30
|
+
# If none was set, a new configuration object is instantiated and returned.
|
31
|
+
#
|
32
|
+
# @return [Configuration] the configuration object
|
33
|
+
#
|
34
|
+
# @see Configuration
|
35
|
+
def configuration
|
36
|
+
@configuration ||= Configuration.new
|
37
|
+
end
|
38
|
+
|
39
|
+
# Allows for configuring the library using a block.
|
40
|
+
#
|
41
|
+
# @example Configuration using a block
|
42
|
+
# Chic.configure do |config|
|
43
|
+
# # ...
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# @see Configuration
|
47
|
+
def configure
|
48
|
+
yield configuration if block_given?
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Chic
|
4
|
+
# A configuration object used to define various options.
|
5
|
+
#
|
6
|
+
# * This is usually not instantiated directly, but rather by way of calling +Chic.configure+.
|
7
|
+
#
|
8
|
+
# @see Chic.configure
|
9
|
+
class Configuration
|
10
|
+
FORMATTERS = {
|
11
|
+
nil: Formatters::Nil
|
12
|
+
}.freeze
|
13
|
+
|
14
|
+
attr_writer :logger,
|
15
|
+
:formatters
|
16
|
+
|
17
|
+
def formatters
|
18
|
+
@formatters ||= FORMATTERS.dup
|
19
|
+
end
|
20
|
+
|
21
|
+
def logger
|
22
|
+
@logger ||= Logger.new(STDOUT)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/chic/errors.rb
ADDED
data/lib/chic/formats.rb
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Chic
|
4
|
+
module Formats
|
5
|
+
class << self
|
6
|
+
def included(parent)
|
7
|
+
parent.extend ClassMethods
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
private
|
13
|
+
|
14
|
+
def format_options(options = {})
|
15
|
+
@format_options = options
|
16
|
+
end
|
17
|
+
|
18
|
+
def formats(*attributes, **options)
|
19
|
+
_validate_formats_options!(attributes, options)
|
20
|
+
attributes.each do |attribute|
|
21
|
+
_define_formats_method(attribute, options)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def _define_formats_method(attribute, options)
|
26
|
+
define_method attribute do
|
27
|
+
value = _formats_options_value(attribute, options)
|
28
|
+
format(value, with: options[:with], **(options[:options] || {}))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def _validate_formats_options!(attributes, options)
|
33
|
+
return if options.nil?
|
34
|
+
|
35
|
+
_raise_formats_options_not_valid 'options must be a hash', attributes \
|
36
|
+
unless options.is_a?(Hash)
|
37
|
+
|
38
|
+
_validate_formats_options_with!(attributes, options)
|
39
|
+
_validate_formats_options_value!(attributes, options)
|
40
|
+
_validate_formats_options_options!(attributes, options)
|
41
|
+
end
|
42
|
+
|
43
|
+
# rubocop: disable Metrics/AbcSize
|
44
|
+
# rubocop: disable Metrics/CyclomaticComplexity
|
45
|
+
def _validate_formats_options_with!(attributes, options)
|
46
|
+
_raise_formats_options_not_valid '`with` must be a symbol or a class', attributes \
|
47
|
+
if options.key?(:with) && !options[:with].is_a?(Symbol) && !options[:with].is_a?(Class)
|
48
|
+
|
49
|
+
_raise_formats_options_not_valid "`with` formatter :#{options[:with]} doesn't exist", attributes \
|
50
|
+
if options.key?(:with) && options[:with].is_a?(Symbol) && !Chic.configuration.formatters.key?(options[:with])
|
51
|
+
end
|
52
|
+
# rubocop: enable Metrics/AbcSize
|
53
|
+
# rubocop: enable Metrics/CyclomaticComplexity
|
54
|
+
|
55
|
+
def _validate_formats_options_value!(attributes, options)
|
56
|
+
_raise_formats_options_not_valid '`value` must be a symbol or a lambda', attributes \
|
57
|
+
if options.key?(:value) && !options[:value].is_a?(Symbol) && !options[:value].is_a?(Proc)
|
58
|
+
end
|
59
|
+
|
60
|
+
def _validate_formats_options_options!(attributes, options)
|
61
|
+
_raise_formats_options_not_valid '`options` must be a hash', attributes \
|
62
|
+
if options.key?(:options) && !options[:options].is_a?(Hash)
|
63
|
+
end
|
64
|
+
|
65
|
+
def _raise_formats_options_not_valid(message, attributes)
|
66
|
+
raise FormatsOptionsNotValid,
|
67
|
+
"#{name} `formats` [#{attributes.map { |a| ":#{a}" }.join(', ')}]: #{message}"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def _formatters
|
74
|
+
Chic.configuration.formatters
|
75
|
+
end
|
76
|
+
|
77
|
+
def _formats_options_value(attribute, options)
|
78
|
+
return object&.public_send(attribute) unless options.is_a?(Hash) && options.key?(:value)
|
79
|
+
|
80
|
+
if options[:value].is_a?(Symbol)
|
81
|
+
send(options[:value])
|
82
|
+
elsif options[:value].is_a?(Proc)
|
83
|
+
instance_exec(&options[:value])
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def _formatter_class(with)
|
88
|
+
case with
|
89
|
+
when Class
|
90
|
+
with
|
91
|
+
when Symbol
|
92
|
+
_formatters[with] || raise(FormatsOptionsNotValid, "Formatter :#{with} is not supported")
|
93
|
+
else
|
94
|
+
_formatters[:nil]
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def format(value, with: nil, **options)
|
99
|
+
_formatter_class(with).new(value).tap do |formatter|
|
100
|
+
options.each do |option, argument|
|
101
|
+
formatter.public_send(option, argument) if formatter.respond_to?(option)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Chic
|
4
|
+
module Formatters
|
5
|
+
class Nil < Formatter
|
6
|
+
BLANK_VALUE = '(No Value)'
|
7
|
+
|
8
|
+
def blank_value(value)
|
9
|
+
@blank_value = value
|
10
|
+
self
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
value.blank? ? (@blank_value || BLANK_VALUE) : value
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def value
|
20
|
+
object&.to_s
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/chic/helpers.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Chic
|
4
|
+
module Helpers
|
5
|
+
# View helpers to make it easy to instantiate presenters from views:
|
6
|
+
#
|
7
|
+
# <% present @foo do |foo_presenter, _foo| %>
|
8
|
+
# <!- ... -->
|
9
|
+
# <% end %>
|
10
|
+
#
|
11
|
+
# And:
|
12
|
+
#
|
13
|
+
# <% present_each @foos do |foo_presenter, _foo| %>
|
14
|
+
# <!- ... -->
|
15
|
+
# <% end %>
|
16
|
+
module View
|
17
|
+
def present(object, &block)
|
18
|
+
Chic::Presentable.presenter_for(object)&.present(object, &block)
|
19
|
+
end
|
20
|
+
|
21
|
+
def present_each(objects, &block)
|
22
|
+
objects.each { |o| present(o, &block) }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Chic
|
4
|
+
module Presentable
|
5
|
+
def presenter(context = nil)
|
6
|
+
@presenter ||= presenter_class.new(self, context)
|
7
|
+
end
|
8
|
+
|
9
|
+
def presenter_class
|
10
|
+
"#{respond_to?(:model_name) ? send(:model_name) : self.class.name}Presenter".constantize
|
11
|
+
end
|
12
|
+
|
13
|
+
module_function
|
14
|
+
|
15
|
+
def presenter_for(object)
|
16
|
+
if object.respond_to?(:presenter_class)
|
17
|
+
object.presenter_class
|
18
|
+
else
|
19
|
+
"#{object&.model_name || object.class.name}Presenter".constantize
|
20
|
+
end
|
21
|
+
rescue NameError, LoadError
|
22
|
+
Chic.configuration.logger&.debug "Couldn't find a presenter for '#{object.class.name}'"
|
23
|
+
nil
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Chic
|
4
|
+
# == Presenting Associations
|
5
|
+
#
|
6
|
+
# Use `presents` and `formats` declarations to present and format associations and attributes.
|
7
|
+
#
|
8
|
+
# @example Presenter inferred from type or attribute name
|
9
|
+
# FooPresenter < Chic::Presenter
|
10
|
+
# presents :bar
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# @example Presenter declared explicitly
|
14
|
+
# FooPresenter < Chic::Presenter
|
15
|
+
# presents bar: BarPresenter
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# @example Association declared using a different value
|
19
|
+
# FooPresenter < Chic::Presenter
|
20
|
+
# presents bar: {
|
21
|
+
# with: BarPresenter,
|
22
|
+
# value: -> { load_bar }
|
23
|
+
# }
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# === ActiveRecord::Relation
|
27
|
+
#
|
28
|
+
# When presenting `ActiveRecord::Relation` associations:
|
29
|
+
#
|
30
|
+
# @example Don't call relation methods through the presenter
|
31
|
+
# foo.presenter.bars.count
|
32
|
+
#
|
33
|
+
# @example Do call relation methods directly
|
34
|
+
# foo.bars.count
|
35
|
+
#
|
36
|
+
# == Formatting Attributes
|
37
|
+
#
|
38
|
+
# @example Declare presented associations
|
39
|
+
# FooPresenter < Chic::Presenter
|
40
|
+
# presents bar: BarPresenter
|
41
|
+
#
|
42
|
+
# formats :title,
|
43
|
+
# with: :nil,
|
44
|
+
# options: {
|
45
|
+
# blank_value: '(No Title)'
|
46
|
+
# }
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
class Presenter
|
50
|
+
include Formats
|
51
|
+
include Presents
|
52
|
+
|
53
|
+
attr_reader :object,
|
54
|
+
:context
|
55
|
+
|
56
|
+
def initialize(object = nil, context = nil)
|
57
|
+
@object = object
|
58
|
+
@context = context
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Chic
|
4
|
+
module Presents
|
5
|
+
class << self
|
6
|
+
def included(parent)
|
7
|
+
parent.extend ClassMethods
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def present(object = nil, context = nil, &block)
|
13
|
+
presenter = new(object, context || _caller(&block))
|
14
|
+
yield(presenter, object) if block_given?
|
15
|
+
end
|
16
|
+
|
17
|
+
def present_each(objects, context = nil, &block)
|
18
|
+
context ||= _caller(&block)
|
19
|
+
objects.map do |object|
|
20
|
+
presenter = new(object, context)
|
21
|
+
yield(presenter, object) if block_given?
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def _caller(&block)
|
28
|
+
block.binding.eval('self') if block_given?
|
29
|
+
end
|
30
|
+
|
31
|
+
def presents(*attributes, **with_options)
|
32
|
+
attributes.each do |attribute|
|
33
|
+
_define_presents_method(attribute)
|
34
|
+
end
|
35
|
+
|
36
|
+
with_options.each do |attribute, options|
|
37
|
+
_validate_presents_options!(attribute, options)
|
38
|
+
_define_presents_with_options_method(attribute, options)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def _define_presents_method(attribute)
|
43
|
+
define_method attribute do
|
44
|
+
memoized = "@#{attribute}"
|
45
|
+
return instance_variable_get(memoized) if instance_variable_defined?(memoized)
|
46
|
+
|
47
|
+
result = present(object.public_send(attribute))
|
48
|
+
instance_variable_set(memoized, result)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def _define_presents_with_options_method(attribute, options)
|
53
|
+
define_method attribute do
|
54
|
+
memoized = "@#{attribute}"
|
55
|
+
return instance_variable_get(memoized) if instance_variable_defined?(memoized)
|
56
|
+
|
57
|
+
with = _presents_options_presenter_class(options)
|
58
|
+
value = _presents_options_value(attribute, options)
|
59
|
+
presenter = present(value, with: with)
|
60
|
+
instance_variable_set(memoized, presenter)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# rubocop: disable Metrics/AbcSize
|
65
|
+
# rubocop: disable Metrics/CyclomaticComplexity
|
66
|
+
# rubocop: disable Metrics/PerceivedComplexity
|
67
|
+
def _validate_presents_options!(attribute, options)
|
68
|
+
_raise_presents_options_not_valid 'options must be a hash or a class', attribute \
|
69
|
+
unless options.is_a?(Hash) || options.is_a?(Class)
|
70
|
+
|
71
|
+
return unless options.is_a?(Hash)
|
72
|
+
|
73
|
+
_raise_presents_options_not_valid '`with` must be a class', attribute \
|
74
|
+
if options.key?(:with) && !options[:with].is_a?(Class)
|
75
|
+
|
76
|
+
_raise_presents_options_not_valid '`value` must be a symbol or a lambda', attribute \
|
77
|
+
if options.key?(:value) && !options[:value].is_a?(Symbol) && !options[:value].is_a?(Proc)
|
78
|
+
end
|
79
|
+
# rubocop: enable Metrics/AbcSize
|
80
|
+
# rubocop: enable Metrics/CyclomaticComplexity
|
81
|
+
# rubocop: enable Metrics/PerceivedComplexity
|
82
|
+
|
83
|
+
def _raise_presents_options_not_valid(message, attribute)
|
84
|
+
raise PresentsOptionsNotValid,
|
85
|
+
"#{name} `presents` :#{attribute}: #{message}"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
def present(object, with: nil)
|
92
|
+
if object.is_a?(Enumerable)
|
93
|
+
object.map { |o| present(o, with) }
|
94
|
+
elsif with.present?
|
95
|
+
with.new(object, context)
|
96
|
+
elsif object.nil?
|
97
|
+
nil_presenter(object)
|
98
|
+
else
|
99
|
+
Chic::Presentable.presenter_for(object)&.new(object, context) || nil_presenter(object)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def present_with(object, with)
|
104
|
+
with.is_a?(Class) ? with.new(object, context) : nil_presenter(object)
|
105
|
+
end
|
106
|
+
|
107
|
+
def nil_presenter(object)
|
108
|
+
Presenters::Nil.new(object, context)
|
109
|
+
end
|
110
|
+
|
111
|
+
def _presents_options_presenter_class(options)
|
112
|
+
options.is_a?(Class) ? options : options[:with]
|
113
|
+
end
|
114
|
+
|
115
|
+
def _presents_options_value(attribute, options)
|
116
|
+
return object&.public_send(attribute) unless options.is_a?(Hash) && options.key?(:value)
|
117
|
+
|
118
|
+
if options[:value].is_a?(Symbol)
|
119
|
+
send(options[:value])
|
120
|
+
elsif options[:value].is_a?(Proc)
|
121
|
+
instance_exec(&options[:value])
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
data/lib/chic/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: chic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lawrance Shepstone
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-06-16 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: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.49.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.49.0
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- lawrance@collabcollective.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rubocop.yml"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- chic.gemspec
|
87
|
+
- lib/chic.rb
|
88
|
+
- lib/chic/configuration.rb
|
89
|
+
- lib/chic/errors.rb
|
90
|
+
- lib/chic/formats.rb
|
91
|
+
- lib/chic/formatter.rb
|
92
|
+
- lib/chic/formatters.rb
|
93
|
+
- lib/chic/formatters/nil.rb
|
94
|
+
- lib/chic/helpers.rb
|
95
|
+
- lib/chic/helpers/view.rb
|
96
|
+
- lib/chic/presentable.rb
|
97
|
+
- lib/chic/presenter.rb
|
98
|
+
- lib/chic/presenters.rb
|
99
|
+
- lib/chic/presenters/nil.rb
|
100
|
+
- lib/chic/presents.rb
|
101
|
+
- lib/chic/version.rb
|
102
|
+
homepage: https://github.com/collcoll/chic
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.7.6
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Opinionated presentation layer comprised of presenters and formatters.
|
126
|
+
test_files: []
|