Sha256: ac05695bba37c8fdd93e4327f91aea70912fb9c9e9a65a330eaa9b736e186688

Contents?: true

Size: 1018 Bytes

Versions: 11

Compression:

Stored size: 1018 Bytes

Contents

require 'active_support'

module HermesMessengerOfTheGods
  module EndpointBuilder
    def self.build(name, klass, *args)
      klass.new(name, *args)
    end

    module Helpers
      extend ActiveSupport::Concern

      included do
        def endpoints
          @endpoints ||= self.class.build_endpoints
        end
      end

      class_methods do
        def is_valid_endpoint_creation_method(method)
          method.to_s =~ /(\w+)_endpoint\z/ &&
            ("HermesMessengerOfTheGods::Endpoints::#{$1.camelize}".constantize rescue nil)
        end

        def method_missing(method, *args, &blk)
          klass = is_valid_endpoint_creation_method(method)
          if klass
            {klass: klass, args: args}
          else
            super(method, *args, &blk)
          end
        end

        def build_endpoints
          self.endpoints.each.with_object({}) do |(k,v), hsh|
            hsh[k] = EndpointBuilder.build(k, v[:klass], *v[:args])
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
hermes_messenger_of_the_gods-2.4.0.pre2 lib/hermes_messenger_of_the_gods/endpoint_builder.rb
hermes_messenger_of_the_gods-2.4.0.pre1 lib/hermes_messenger_of_the_gods/endpoint_builder.rb
hermes_messenger_of_the_gods-2.3.4 lib/hermes_messenger_of_the_gods/endpoint_builder.rb
hermes_messenger_of_the_gods-2.3.3 lib/hermes_messenger_of_the_gods/endpoint_builder.rb
hermes_messenger_of_the_gods-2.3.2 lib/hermes_messenger_of_the_gods/endpoint_builder.rb
hermes_messenger_of_the_gods-2.3.1 lib/hermes_messenger_of_the_gods/endpoint_builder.rb
hermes_messenger_of_the_gods-2.3.0 lib/hermes_messenger_of_the_gods/endpoint_builder.rb
hermes_messenger_of_the_gods-2.2.0 lib/hermes_messenger_of_the_gods/endpoint_builder.rb
hermes_messenger_of_the_gods-2.1.1 lib/hermes_messenger_of_the_gods/endpoint_builder.rb
hermes_messenger_of_the_gods-2.1.0 lib/hermes_messenger_of_the_gods/endpoint_builder.rb
hermes_messenger_of_the_gods-2.0.0 lib/hermes_messenger_of_the_gods/endpoint_builder.rb