Class: Gapic::Presenters::ResourcePresenter

Inherits:
Object
  • Object
show all
Defined in:
lib/gapic/presenters/resource_presenter.rb

Overview

A presenter for proto resources.

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ ResourcePresenter

Returns a new instance of ResourcePresenter.

Parameters:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gapic/presenters/resource_presenter.rb', line 28

def initialize resource
  @resource = resource

  @patterns = resource.pattern.map do |template|
    segments = Gapic::PathTemplate.parse template
    OpenStruct.new(
      template:    template,
      segments:    segments,
      arguments:   arguments_for(segments),
      path_string: path_string_for(segments)
    )
  end

  @patterns.each do |pattern|
    # URI path template verification for expected proto resource usage
    if named_arg_patterns? pattern.segments
      raise ArgumentError, "only resources without named patterns are supported, " \
                          " not #{pattern.template}"
    elsif positional_args? pattern.segments
      raise ArgumentError, "only resources with named segments are supported, " \
                          " not #{pattern.template}"
    end
  end
end

Instance Method Details

#name ⇒ Object



53
54
55
# File 'lib/gapic/presenters/resource_presenter.rb', line 53

def name
  @resource.type.split("/").delete_if(&:empty?).last
end

#path_helper ⇒ Object



65
66
67
# File 'lib/gapic/presenters/resource_presenter.rb', line 65

def path_helper
  "#{ActiveSupport::Inflector.underscore name}_path"
end

#patterns ⇒ Object



61
62
63
# File 'lib/gapic/presenters/resource_presenter.rb', line 61

def patterns
  @patterns
end

#type ⇒ Object



57
58
59
# File 'lib/gapic/presenters/resource_presenter.rb', line 57

def type
  @resource.type
end