Sha256: 18bd3f15062ba35a2169e5965eef3cf6aa6e58450b8b7ce19f8b7ffddad0d02b

Contents?: true

Size: 1.27 KB

Versions: 4

Compression:

Stored size: 1.27 KB

Contents

module Spigot
  module Map
    class Service
      attr_reader :name
      attr_accessor :resources

      def initialize(name)
        @name = name.to_s.underscore.to_sym
        @resources = []
      end

      def self.service(name, &block)
        service = find(name) || Spigot::Map::Service.new(name)
        service.instance_eval(&block) if block_given?
        current_map.update(name, service)
      end

      def self.resource(name, &block)
        service(:any) { resource(name, &block) }
      end

      def self.find(name)
        current_map.service(name)
      end

      def resource(name, &block)
        resources << Spigot::Map::Resource.new(name, &block)
      end

      def reset
        @resources = []
      end

      def [](name)
        resources.find { |r| r.instance_variable_get(:@name).to_sym == name.to_sym }
      end

      def self.extract(params)
        return params if current_map.nil?
        return [nil, params] if params.is_a?(Array)

        name    = params.keys.first
        service = current_map.service(name)

        if service.nil?
          return [nil, params]
        else
          return [service.name.to_sym, params[name]]
        end
      end

      private

      def self.current_map
        Spigot.config.map
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spigot-0.3.3 lib/spigot/map/service.rb
spigot-0.3.2 lib/spigot/map/service.rb
spigot-0.3.1 lib/spigot/map/service.rb
spigot-0.3.0 lib/spigot/map/service.rb