Class: Featuring::Serializable::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/featuring/serializable.rb

Overview

public

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

Instance Method Summary collapse

Constructor Details

#initialize(features) ⇒ Serializer

Returns a new instance of Serializer.



29
30
31
32
33
34
# File 'lib/featuring/serializable.rb', line 29

def initialize(features)
  @features = features
  @included = []
  @excluded = []
  @context = {}
end

Instance Method Details

#context(feature_flag, *args) ⇒ Object

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
}


97
98
99
# File 'lib/featuring/serializable.rb', line 97

def context(feature_flag, *args)
  @context[feature_flag] = args
end

#exclude(*feature_flags) ⇒ Object

public

Exclude specific feature flags in the serialized result.

Examples:

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
}


76
77
78
# File 'lib/featuring/serializable.rb', line 76

def exclude(*feature_flags)
  @excluded.concat(feature_flags)
end

#include(*feature_flags) ⇒ Object

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
}


54
55
56
# File 'lib/featuring/serializable.rb', line 54

def include(*feature_flags)
  @included.concat(feature_flags)
end

#to_hObject

public

Returns a hash representation of feature flags.



103
104
105
106
107
108
109
# 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