vizier 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7ae1adcb210a11987c89af85c6056d14f61ac9b7
4
+ data.tar.gz: a6ee77ad5cabd5e9c3cecea6aca48eb0e640e3b9
5
+ SHA512:
6
+ metadata.gz: db066fec049d40e28ad4bf0e60231175220e7855df95f3e7998b019e62bc27ec8cf225d72fa1268d0d855331ede97cb856378bb25bebeb79deefa6bad351e300
7
+ data.tar.gz: e4139ee987a2a38ec94baea80a9683bd2d8a7911af9847c97badcf6f967dd93c180bcebe3bb4ec25fefcd326390ad798968265eb0a2b933855df93e16ec3d256
data/.envrc ADDED
@@ -0,0 +1 @@
1
+ PATH_add bin
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ Gemfile.lock
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,30 @@
1
+ Rails:
2
+ Enabled: true
3
+
4
+ Rails/Delegate:
5
+ Enabled: false
6
+
7
+ # inherit_gem:
8
+ # rubocop-rails:
9
+ # - config/rails.yml
10
+
11
+ AllCops:
12
+ DisplayCopNames: true
13
+ TargetRubyVersion: 2.4
14
+ Exclude:
15
+ - 'bin/**/*'
16
+ - 'vendor/**/*'
17
+
18
+ Layout/MultilineMethodDefinitionBraceLayout:
19
+ EnforcedStyle: same_line
20
+
21
+ Metrics/LineLength:
22
+ Max: 100
23
+
24
+ Metrics/BlockLength:
25
+ Exclude:
26
+ - '*.gemspec'
27
+ ExcludedMethods: ['describe', 'context']
28
+
29
+ Style/StringLiterals:
30
+ Enabled: false
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in vizier.gemspec
8
+ gemspec
@@ -0,0 +1,27 @@
1
+ Copyright (c) 2018, The Regents of the University of Michigan.
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are
6
+ met:
7
+
8
+ * Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+ * Redistributions in binary form must reproduce the above copyright
11
+ notice, this list of conditions and the following disclaimer in the
12
+ documentation and/or other materials provided with the distribution.
13
+ * Neither the name of the The University of Michigan nor the
14
+ names of its contributors may be used to endorse or promote products
15
+ derived from this software without specific prior written permission.
16
+
17
+ THIS SOFTWARE IS PROVIDED BY THE REGENTS OF THE UNIVERSITY OF MICHIGAN AND
18
+ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19
+ NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OF THE
21
+ UNIVERSITY OF MICHIGAN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23
+ TO,PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,25 @@
1
+ [![Build Status](https://travis-ci.org/mlibrary/vizier.svg?branch=master)](https://travis-ci.org/mlibrary/vizier?branch=master)
2
+ [![Coverage Status](https://coveralls.io/repos/github/mlibrary/vizier/badge.svg?branch=master)](https://coveralls.io/github/mlibrary/vizier?branch=master)
3
+
4
+ # Vizier
5
+
6
+ Vizier helps you present information appropriately.
7
+
8
+ It is a small library for writing and using policy-aware presenters, especially
9
+ in Rails applications, conveniently.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'vizier'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ ## License
24
+
25
+ Vizier is licensed under the BSD-3-Clause license. See [LICENSE.md](LICENSE.md).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "vizier"
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__)
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rspec' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
12
+ load(bundle_binstub) if File.file?(bundle_binstub)
13
+
14
+ require "pathname"
15
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
16
+ Pathname.new(__FILE__).realpath)
17
+
18
+ require "rubygems"
19
+ require "bundler/setup"
20
+
21
+ load Gem.bin_path("rspec-core", "rspec")
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'vizier/version'
4
+
5
+ # Top level module for Vizier
6
+ module Vizier
7
+ # Just the constant for now
8
+ end
9
+
10
+ require 'vizier/null_presenter'
11
+ require 'vizier/read_only_policy'
12
+
13
+ require 'vizier/presenter_config'
14
+ require 'vizier/caching_presenter_config'
15
+ require 'vizier/default_presenter_config'
16
+
17
+ require 'vizier/collection_presenter'
18
+ require 'vizier/resource_presenter'
19
+ require 'vizier/presenter_factory'
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vizier
4
+ # A presenter configuration that caches the type lookup
5
+ #
6
+ # For development use reloading is convenient, but in production
7
+ # the constants should not be redefined. We cache them here to
8
+ # avoid repeated global lookups.
9
+ class CachingPresenterConfig < PresenterConfig
10
+ def type
11
+ @type ||= super
12
+ end
13
+
14
+ def presenter
15
+ @presenter ||= super
16
+ end
17
+
18
+ def policy
19
+ @policy ||= super
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vizier
4
+ # Base implementation for a Collection Presenter.
5
+ #
6
+ # This class can be extended in your application to inherit the convenience
7
+ # of delegating to a collection policy's resolved scope and having a default
8
+ # `present` method available for wrapping other objects according to the
9
+ # configured factory. To specify a factory, override `presenter_factory`.
10
+ class CollectionPresenter
11
+ include Enumerable
12
+
13
+ def initialize(policy, view, presenter_factory: PresenterFactory.new)
14
+ @policy = policy
15
+ @view = view
16
+ @presenter_factory = presenter_factory
17
+ end
18
+
19
+ def each
20
+ resources.each { |resource| yield resource }
21
+ end
22
+
23
+ def empty?
24
+ resources.empty?
25
+ end
26
+
27
+ protected
28
+
29
+ def resources
30
+ @resources ||= policy.resolve.map do |resource|
31
+ present(resource)
32
+ end
33
+ end
34
+
35
+ def present(resource)
36
+ presenter_factory[resource, policy.user, view]
37
+ end
38
+
39
+ attr_reader :policy, :view, :presenter_factory
40
+ end
41
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vizier
4
+ # A base implementation of a default presenter configuration, used by
5
+ # by PresenterFactory to provide a fall-through for unmapped types.
6
+ #
7
+ # This uses `NullPresenter` and `ReadOnlyPolicy` to have generic, fully
8
+ # delegating presenters with a `show?` permission passing unconditionally.
9
+ class DefaultPresenterConfig
10
+ attr_reader :config_type
11
+
12
+ def initialize(config_type)
13
+ @config_type = config_type
14
+ end
15
+
16
+ def for(type)
17
+ config_type.new(type, NullPresenter, ReadOnlyPolicy)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vizier
4
+ # Simple pass-through presenter that serves as a default.
5
+ #
6
+ # We may want to use something other than `.new` so this
7
+ # can actually return the object rather than a new instance
8
+ # of itself.
9
+ class NullPresenter < SimpleDelegator
10
+ def initialize(object, *)
11
+ __setobj__ object
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vizier
4
+ # Mapping for a given type to presenter and policy types.
5
+ #
6
+ # This configuration does not cache the constant lookups, so it supports
7
+ # reloading of the types in development. The CachingPresenterConfig should
8
+ # be used in production.
9
+ class PresenterConfig
10
+ def initialize(type, presenter, policy)
11
+ @type_name = type.to_s
12
+ @presenter_name = presenter.to_s
13
+ @policy_name = policy.to_s
14
+ end
15
+
16
+ def present(object, user, view)
17
+ presenter.new(policy.new(user, object), view)
18
+ end
19
+
20
+ def type
21
+ Object.const_get(@type_name)
22
+ end
23
+
24
+ def presenter
25
+ Object.const_get(@presenter_name)
26
+ end
27
+
28
+ def policy
29
+ Object.const_get(@policy_name)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vizier
4
+ # Factory for locating and creating a presenter based on an object's type.
5
+ class PresenterFactory
6
+ # Construct a factory with a mapping of types to their default presenters
7
+ # and policies.
8
+ #
9
+ # @param presenter_map [Hash] the mapping of classes of strings to a pair,
10
+ # where the first item is the default presenter type, and the second is the
11
+ # default policy type.
12
+ def initialize(
13
+ presenter_map = {},
14
+ config_type: PresenterConfig,
15
+ default_config: DefaultPresenterConfig.new(config_type))
16
+
17
+ @config_type = config_type
18
+ @default_config = default_config
19
+
20
+ @configs = Hash.new do |_configs, type|
21
+ default_config.for(type)
22
+ end
23
+
24
+ presenter_map.each do |type, config|
25
+ @configs[type.to_s] = config_type.new(type, config.first, config.last)
26
+ end
27
+ end
28
+
29
+ def [](object, user, view)
30
+ configs[object.class.to_s].present(object, user, view)
31
+ end
32
+
33
+ private
34
+
35
+ attr_accessor :configs, :config_type, :default_config
36
+ end
37
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vizier
4
+ # This is a basic/dummy policy that can be used as a default.
5
+ # There is no hard dependency on Checkpoint, but it has the same
6
+ # interface as a minimal resource policy.
7
+ class ReadOnlyPolicy
8
+ attr_reader :user, :resource
9
+
10
+ def initialize(user, resource)
11
+ @user = user
12
+ @resource = resource
13
+ end
14
+
15
+ def show?
16
+ true
17
+ end
18
+
19
+ def authorize!(action, message = nil)
20
+ raise NotAuthorizedError, message unless send(action)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vizier
4
+ # Base implementation for a Resource Presenter.
5
+ #
6
+ # This class can be extended in your application to inherit the convenience
7
+ # of delegating to a policy's resource and having a default `present` method
8
+ # available for wrapping other objects according to the configured factory.
9
+ # To specify a factory, override `presenter_factory`.
10
+ class ResourcePresenter < SimpleDelegator
11
+ extend Forwardable
12
+
13
+ def initialize(policy, view, presenter_factory: PresenterFactory.new)
14
+ @policy = policy
15
+ @view = view
16
+ @presenter_factory = presenter_factory
17
+ __setobj__ policy.resource
18
+ end
19
+
20
+ protected
21
+
22
+ def resource
23
+ policy.resource
24
+ end
25
+
26
+ def present(object)
27
+ presenter_factory[object, policy.user, view]
28
+ end
29
+
30
+ attr_reader :policy, :view, :presenter_factory
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vizier
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "vizier/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "vizier"
9
+ spec.version = Vizier::VERSION
10
+ spec.authors = ["Noah Botimer"]
11
+ spec.email = ["botimer@umich.edu"]
12
+ spec.license = "BSD-3-Clause"
13
+
14
+ spec.summary = <<~SUMMARY
15
+ Vizier supports building policy-aware presenters for the view layer of applications,
16
+ especially those using Rails.
17
+ SUMMARY
18
+
19
+ spec.description = <<~DESCRIPTION
20
+ Vizier helps you present the right information to your users. By using policies and configurable
21
+ defaults, it becomes easier to build presenters and be sure that model objects are wrapped in
22
+ presenters when they are delivered to view templates.
23
+ DESCRIPTION
24
+
25
+ spec.homepage = "https://github.com/mlibrary/vizier"
26
+
27
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
28
+ f.match(%r{^(test|spec|features)/})
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_development_dependency "bundler", "~> 1.16"
35
+ spec.add_development_dependency "coveralls", "~> 0.8"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "rspec", "~> 3.0"
38
+ spec.add_development_dependency "rubocop", "~> 0.52"
39
+ spec.add_development_dependency "rubocop-rails", "~> 1.1"
40
+ spec.add_development_dependency "rubocop-rspec", "~> 1.16"
41
+ end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vizier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Noah Botimer
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-05 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: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: coveralls
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.8'
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.52'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.52'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.1'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.1'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.16'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.16'
111
+ description: |
112
+ Vizier helps you present the right information to your users. By using policies and configurable
113
+ defaults, it becomes easier to build presenters and be sure that model objects are wrapped in
114
+ presenters when they are delivered to view templates.
115
+ email:
116
+ - botimer@umich.edu
117
+ executables: []
118
+ extensions: []
119
+ extra_rdoc_files: []
120
+ files:
121
+ - ".envrc"
122
+ - ".gitignore"
123
+ - ".rspec"
124
+ - ".rubocop.yml"
125
+ - ".travis.yml"
126
+ - Gemfile
127
+ - LICENSE.md
128
+ - README.md
129
+ - Rakefile
130
+ - bin/console
131
+ - bin/rspec
132
+ - bin/setup
133
+ - lib/vizier.rb
134
+ - lib/vizier/caching_presenter_config.rb
135
+ - lib/vizier/collection_presenter.rb
136
+ - lib/vizier/default_presenter_config.rb
137
+ - lib/vizier/null_presenter.rb
138
+ - lib/vizier/presenter_config.rb
139
+ - lib/vizier/presenter_factory.rb
140
+ - lib/vizier/read_only_policy.rb
141
+ - lib/vizier/resource_presenter.rb
142
+ - lib/vizier/version.rb
143
+ - vizier.gemspec
144
+ homepage: https://github.com/mlibrary/vizier
145
+ licenses:
146
+ - BSD-3-Clause
147
+ metadata: {}
148
+ post_install_message:
149
+ rdoc_options: []
150
+ require_paths:
151
+ - lib
152
+ required_ruby_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ required_rubygems_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ requirements: []
163
+ rubyforge_project:
164
+ rubygems_version: 2.6.13
165
+ signing_key:
166
+ specification_version: 4
167
+ summary: Vizier supports building policy-aware presenters for the view layer of applications,
168
+ especially those using Rails.
169
+ test_files: []