Sha256: 656e89300c8c316bd523d3561c9a62376b8e80c0b35a2f3ab9ec48d6fa57960f

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

require 'delegate'
require 'flipper/ui/decorators/gate'
require 'flipper/ui/util'

module Flipper
  module UI
    module Decorators
      class Feature < SimpleDelegator
        include Comparable

        # Public: The feature being decorated.
        alias_method :feature, :__getobj__

        # Public: Returns name titleized.
        def pretty_name
          @pretty_name ||= Util.titleize(name)
        end

        # Public: Returns instance as hash that is ready to be json dumped.
        def as_json
          gate_values = feature.gate_values
          {
            'id' => name.to_s,
            'name' => pretty_name,
            'state' => state.to_s,
            'description' => description,
            'gates' => gates.map { |gate|
              Decorators::Gate.new(gate, gate_values[gate.key]).as_json
            },
          }
        end

        def color_class
          case feature.state
          when :on
            "text-open"
          when :off
            "text-closed"
          when :conditional
            "text-pending"
          end
        end

        def enabled_gates
          feature.gates.select { |gate|
            gate.enabled?(feature.gate_values[gate.key])
          }
        end

        def enabled_gate_names
          enabled_gates.map { |gate| Util.titleize(gate.key) }.sort.join(', ')
        end

        StateSortMap = {
          :on => 1,
          :conditional => 2,
          :off => 3,
        }

        def <=>(other)
          if state == other.state
            key <=> other.key
          else
            StateSortMap[state] <=> StateSortMap[other.state]
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
flipper-ui-0.2.0.beta4 lib/flipper/ui/decorators/feature.rb
flipper-ui-0.2.0.beta3 lib/flipper/ui/decorators/feature.rb