Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions lib/flipper/api/v1/decorators/feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ module Flipper
module Api
module V1
module Decorators
class Feature < SimpleDelegator
# Public: The feature being decorated.
alias_method :feature, :__getobj__
class Feature
def initialize(feature)
@feature = feature
end

# Public: Returns instance as hash that is ready to be json dumped.
def as_json(exclude_gates: false, exclude_gate_names: false)
result = {
'key' => key,
'state' => state.to_s,
'key' => @feature.key,
'state' => @feature.state.to_s,
}

unless exclude_gates
gate_values = feature.adapter.get(self)
result['gates'] = gates.map do |gate|
gate_values = @feature.adapter.get(@feature)
result['gates'] = @feature.gates.map do |gate|
Decorators::Gate.new(gate, gate_values[gate.key]).as_json(exclude_name: exclude_gate_names)
end
end
Expand Down
16 changes: 5 additions & 11 deletions lib/flipper/api/v1/decorators/gate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,18 @@ module Flipper
module Api
module V1
module Decorators
class Gate < SimpleDelegator
# Public the gate being decorated
alias_method :gate, :__getobj__

# Public: the value for the gate from the adapter.
attr_reader :value

class Gate
def initialize(gate, value = nil)
super gate
@gate = gate
@value = value
end

def as_json(exclude_name: false)
as_json = {
'key' => gate.key.to_s,
'key' => @gate.key.to_s,
'value' => value_as_json,
}
as_json['name'] = gate.name.to_s unless exclude_name
as_json['name'] = @gate.name.to_s unless exclude_name
as_json
end

Expand All @@ -30,7 +24,7 @@ def as_json(exclude_name: false)

# json doesn't like sets
def value_as_json
JSON_ARRAY_TYPES.include?(data_type) ? value.to_a : value
JSON_ARRAY_TYPES.include?(@gate.data_type) ? @value.to_a : @value
end
end
end
Expand Down