Class: Gapic::Presenters::ResourcePresenter
- Inherits:
-
Object
- Object
- Gapic::Presenters::ResourcePresenter
- Defined in:
- lib/gapic/presenters/resource_presenter.rb
Overview
A presenter for proto resources.
Defined Under Namespace
Classes: PatternPresenter
Class Method Summary collapse
-
.dedup_patterns(patterns) ⇒ Array<PatternPresenter>
Deduplicates pattern that have the same
arguments_key
.
Instance Method Summary collapse
- #dup ⇒ Object
-
#initialize(resource) ⇒ ResourcePresenter
constructor
A new instance of ResourcePresenter.
- #name ⇒ Object
- #path_helper ⇒ Object
- #patterns ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(resource) ⇒ ResourcePresenter
Returns a new instance of ResourcePresenter.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/gapic/presenters/resource_presenter.rb', line 26 def initialize resource @resource = resource all_patterns = resource.pattern.map { |pattern| PatternPresenter.new pattern } # Keep only patterns that can be used to create path helpers all_useful_patterns = all_patterns.filter(&:useful_for_helpers?) # Remove patterns where key is duplicated @patterns = ResourcePresenter.dedup_patterns all_useful_patterns end |
Class Method Details
.dedup_patterns(patterns) ⇒ Array<PatternPresenter>
Deduplicates pattern that have the same arguments_key
. Our design for the "paths" helper
only allows for one pattern per arguments_key
.
If patterns with the same arguments_key
are detected, the shortest is taken. If there is
a tie, the lexicographically first is taken.
69 70 71 72 73 74 75 |
# File 'lib/gapic/presenters/resource_presenter.rb', line 69 def self.dedup_patterns patterns patterns.group_by(&:arguments_key).map do |_arguments_key, group| group.min_by do |pattern| [pattern.pattern.length, pattern.pattern] end end end |
Instance Method Details
#dup ⇒ Object
38 39 40 |
# File 'lib/gapic/presenters/resource_presenter.rb', line 38 def dup ResourcePresenter.new @resource end |
#name ⇒ Object
42 43 44 |
# File 'lib/gapic/presenters/resource_presenter.rb', line 42 def name @resource.type.split("/").delete_if(&:empty?).last end |
#path_helper ⇒ Object
54 55 56 |
# File 'lib/gapic/presenters/resource_presenter.rb', line 54 def path_helper "#{ActiveSupport::Inflector.underscore name}_path" end |
#patterns ⇒ Object
50 51 52 |
# File 'lib/gapic/presenters/resource_presenter.rb', line 50 def patterns @patterns end |
#type ⇒ Object
46 47 48 |
# File 'lib/gapic/presenters/resource_presenter.rb', line 46 def type @resource.type end |