diff --git a/lib/this_feature/adapters/split_io.rb b/lib/this_feature/adapters/split_io.rb index 959f244..cfce8fc 100644 --- a/lib/this_feature/adapters/split_io.rb +++ b/lib/this_feature/adapters/split_io.rb @@ -30,8 +30,12 @@ def off?(flag_name, context: EMPTY_CONTEXT, data: {}, record: nil) treatment(flag_name, context: context, data: data, record: record).eql?('off') end - def get_treatment(flag_name, context: EMPTY_CONTEXT, data: {}, record: nil) - treatment(flag_name, context: context, data: data, record: record) + def treatment_value(flag_name, context: EMPTY_CONTEXT, data: {}, record: nil) + treatment_with_config(flag_name, context: context, data: data, record: record)[:treatment] + end + + def treatment_config(flag_name, context: EMPTY_CONTEXT, data: {}, record: nil) + treatment_with_config(flag_name, context: context, data: data, record: record)[:config] end private @@ -43,6 +47,11 @@ def treatment(flag_name, context: EMPTY_CONTEXT, data: {}, record: nil) client.get_treatment(context_key(context), flag_name, base_data.merge(data)) end + def treatment_with_config(flag_name, context: EMPTY_CONTEXT, data: {}, record: nil) + base_data = record ? ThisFeature.base_data_lambda.call(record) : {} + client.get_treatment_with_config(context_key(context), flag_name, base_data.merge(data)) + end + def context_key(context) return EMPTY_CONTEXT if context.nil? || context.eql?(EMPTY_CONTEXT) return context.send(context_key_method) unless context_key_method.nil? diff --git a/lib/this_feature/flag.rb b/lib/this_feature/flag.rb index 79515ca..f6a14c5 100644 --- a/lib/this_feature/flag.rb +++ b/lib/this_feature/flag.rb @@ -22,8 +22,12 @@ def control? adapter.control?(flag_name, context: context, data: data, record: record) end - def get_treatment - adapter.get_treatment(flag_name, context: context, data: data, record: record) + def treatment_value + adapter.treatment_value(flag_name, context: context, data: data, record: record) + end + + def treatment_config + adapter.treatment_config(flag_name, context: context, data: data, record: record) end end end diff --git a/spec/support/files/splits.yaml b/spec/support/files/splits.yaml index b7aba35..9249e9d 100644 --- a/spec/support/files/splits.yaml +++ b/spec/support/files/splits.yaml @@ -16,10 +16,12 @@ - treatment_feature: treatment: 'v1' keys: '1' - config: {'desc': 'this applies only to when there are many treatments'} + config: + plan_id: 1 - treatment_feature: - treatment: 'v2' - keys: '2' - config: {'desc': 'this applies only to when there are many treatments'} + treatment: 'v2' + keys: '2' + config: + plan_id: 2 diff --git a/spec/this_feature/adapters/split_io_adapter_spec.rb b/spec/this_feature/adapters/split_io_adapter_spec.rb index 7d8f2d3..f5480f6 100644 --- a/spec/this_feature/adapters/split_io_adapter_spec.rb +++ b/spec/this_feature/adapters/split_io_adapter_spec.rb @@ -40,9 +40,13 @@ context 'when org id is 1' do it 'returns the right treatment' do - expect(ThisFeature.flag(flag_name).get_treatment).to eq 'control_treatment' - expect(ThisFeature.flag(flag_name, context: org_id).get_treatment).to eq 'v1' - expect(ThisFeature.flag(flag_name, context: org_id, data: { a: :b }).get_treatment).to eq 'v1' + expect(ThisFeature.flag(flag_name).treatment_value).to eq 'control_treatment' + expect(ThisFeature.flag(flag_name, context: org_id).treatment_value).to eq 'v1' + + this_feature = ThisFeature.flag(flag_name, context: org_id, data: { a: :b }) + + expect(this_feature.treatment_value).to eq 'v1' + expect(JSON.parse(this_feature.treatment_config)).to eq({ 'plan_id' => 1 }) end end @@ -50,9 +54,13 @@ let(:org_id) { 2 } it 'returns the right treatment' do - expect(ThisFeature.flag(flag_name).get_treatment).to eq 'control_treatment' - expect(ThisFeature.flag(flag_name, context: org_id).get_treatment).to eq 'v2' - expect(ThisFeature.flag(flag_name, context: org_id, data: { a: :b }).get_treatment).to eq 'v2' + expect(ThisFeature.flag(flag_name).treatment_value).to eq 'control_treatment' + expect(ThisFeature.flag(flag_name, context: org_id).treatment_value).to eq 'v2' + + this_feature = ThisFeature.flag(flag_name, context: org_id, data: { a: :b }) + + expect(this_feature.treatment_value).to eq 'v2' + expect(JSON.parse(this_feature.treatment_config)).to eq({ 'plan_id' => 2 }) end end end