Harbor Documentation

Harbor::Contrib::Feature

Convenience class for bundling optional features in Harbor applications.

  class MyApplication::Features::Feature < Harbor::Contrib::Feature
    class << self
      attr_accessor :option
    end

    def self.enable
      if enabled = super
        require "necessary/files"
      end

      enabled
    end
  end

  MyApplication::Feature::Feature.enable do |feature|
    feature.option = true
  end

It can also be helpful to utilize Harbor::Router#merge! to bundle routes with the feature.

  class MyApplication::Features::Feature
    def self.routes(services)
      Harbor::Router.new do
        get("/feature") { |request, response| response.puts "Inside feature" }
      end
    end
  end

  class MyApplication
    def self.routes(services)
      Harbor::Router.new do
        get("/") { |request, response| response.puts "Inside MyApplication" }
        merge!(Features::Feature.routes(services)) if Features::Feature.enabled?
      end
    end
  end

Parent

Public Class Methods

enable()

      # File lib/harbor/contrib/feature.rb, line 46
46:       def self.enable
47:         return false if @enabled
48:         @enabled = true
49: 
50:         yield self if block_given?
51: 
52:         @enabled
53:       end

enabled?()

      # File lib/harbor/contrib/feature.rb, line 55
55:       def self.enabled?
56:         !!@enabled
57:       end