Sha256: 2234279af0d32a347bebfe53e6b80840719efba89f4e4ce24db6e783e1072957

Contents?: true

Size: 1.18 KB

Versions: 8

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module V1
  module Resources
    module Concerns
      module Href
        extend ActiveSupport::Concern

        # Base module where the href concern will grab constants from
        included do
          def self.base_module
            ::V1
          end
        end

        module ClassMethods
          mutex = Mutex.new
          
          def endpoint_path_template
            # memoize a templated path for an endpoint, like
            # /users/%{id}
            return @endpoint_path_template if @endpoint_path_template # rubocop:disable ThreadSafety/InstanceVariableInClassMethod

            mutex.synchronize do
              return @endpoint_path_template if @endpoint_path_template # rubocop:disable ThreadSafety/InstanceVariableInClassMethod

              path = self.base_module.const_get(:Endpoints).const_get(model.name.split(':').last.pluralize).canonical_path.route.path
              @endpoint_path_template = path.names.inject(path.to_s) { |p, name| p.sub(':' + name, "%{#{name}}") }  
            end
          end
        end

        def href
          format(self.class.endpoint_path_template, id: id)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
praxis-2.0.0 tasks/thor/templates/generator/example_app/app/v1/concerns/href.rb
praxis-2.0.pre.40 tasks/thor/templates/generator/example_app/app/v1/concerns/href.rb
praxis-2.0.pre.39 tasks/thor/templates/generator/example_app/app/v1/concerns/href.rb
praxis-2.0.pre.38 tasks/thor/templates/generator/example_app/app/v1/concerns/href.rb
praxis-2.0.pre.37 tasks/thor/templates/generator/example_app/app/v1/concerns/href.rb
praxis-2.0.pre.36 tasks/thor/templates/generator/example_app/app/v1/concerns/href.rb
praxis-2.0.pre.35 tasks/thor/templates/generator/example_app/app/v1/concerns/href.rb
praxis-2.0.pre.34 tasks/thor/templates/generator/example_app/app/v1/concerns/href.rb