class Featuring::Serializable::Serializer

public

Feature flag serialization context (intended to be used through ‘Featuring::Serializable#serialize`).

Public Class Methods

new(features) click to toggle source
# File lib/featuring/serializable.rb, line 29
def initialize(features)
  @features = features
  @included = []
  @excluded = []
  @context = {}
end

Public Instance Methods

context(feature_flag, *args) click to toggle source
public

Provide context for serializing complex feature flags.

module Features
  extend Featuring::Declarable

  feature :some_complex_feature do |value|
    value == :some_value
  end
end

Features.serialize do |serializer|
  serializer.context :some_complex_feature, :some_value
end
=> {
  some_complex_feature: true
}
# File lib/featuring/serializable.rb, line 97
def context(feature_flag, *args)
  @context[feature_flag] = args
end
exclude(*feature_flags) click to toggle source
public

Exclude specific feature flags in the serialized result.

@example

module Features
  extend Featuring::Declarable

  feature :feature_1, true
  feature :feature_2, true
  feature :feature_3, true
end

Features.serialize do |serializer|
  serializer.exclude :feature_1, :feature_3
end
=> {
  feature_2: true
}
# File lib/featuring/serializable.rb, line 76
def exclude(*feature_flags)
  @excluded.concat(feature_flags)
end
include(*feature_flags) click to toggle source

Include only specific feature flags in the serialized result.

module Features
  extend Featuring::Declarable

  feature :feature_1, true
  feature :feature_2, true
  feature :feature_3, true
end

Features.serialize do |serializer|
  serializer.include :feature_1, :feature_3
end
=> {
  feature_1: true,
  feature_2: true
}
# File lib/featuring/serializable.rb, line 54
def include(*feature_flags)
  @included.concat(feature_flags)
end
to_h() click to toggle source
public

Returns a hash representation of feature flags.

# File lib/featuring/serializable.rb, line 103
def to_h
  @features.feature_flags.each_with_object({}) { |feature_flag, serialized|
    if serializable?(feature_flag)
      serialized[feature_flag] = @features.fetch_feature_flag_value(feature_flag, *@context[feature_flag])
    end
  }
end

Private Instance Methods

excluded?(feature_flag) click to toggle source
# File lib/featuring/serializable.rb, line 119
        def excluded?(feature_flag)
  @excluded.any? && @excluded.include?(feature_flag)
end
included?(feature_flag) click to toggle source
# File lib/featuring/serializable.rb, line 115
        def included?(feature_flag)
  @included.empty? || @included.include?(feature_flag)
end
serializable?(feature_flag) click to toggle source
# File lib/featuring/serializable.rb, line 111
        def serializable?(feature_flag)
  included?(feature_flag) && !excluded?(feature_flag)
end