Skip to content

Commit

Permalink
Refactor plan & plan_points
Browse files Browse the repository at this point in the history
We now set parents during initialization of plan and its points
  • Loading branch information
aglushkov committed Oct 11, 2023
1 parent 553eff1 commit 3c75035
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 22 deletions.
17 changes: 7 additions & 10 deletions lib/serega/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module ClassMethods
#
def call(opts)
max_cache_size = serializer_class.config.max_cached_plans_per_serializer_count
return new(opts) if max_cache_size.zero?
return new(nil, opts) if max_cache_size.zero?

cached_plan_for(opts, max_cache_size)
end
Expand All @@ -33,7 +33,7 @@ def cached_plan_for(opts, max_cache_size)
@cache ||= {}
cache_key = construct_cache_key(opts)

plan = @cache[cache_key] ||= new(opts)
plan = @cache[cache_key] ||= new(nil, opts)
@cache.shift if @cache.length > max_cache_size
plan
end
Expand Down Expand Up @@ -61,25 +61,23 @@ module InstanceMethods
# @return [SeregaPlanPoint, nil]
attr_reader :parent_plan_point

# Sets new parent plan point
# @return [SeregaPlanPoint] new parent plan point
attr_writer :parent_plan_point

# Serialization points
# @return [Array<SeregaPlanPoint>] points to serialize
attr_reader :points

#
# Instantiate new serialization plan.
#
# @param modifiers Serialization parameters
# @param parent_plan_point [SeregaPlanPoint, nil] Parent plan_point
# @param modifiers [Hash] Serialization parameters
# @option modifiers [Hash] :only The only attributes to serialize
# @option modifiers [Hash] :except Attributes to hide
# @option modifiers [Hash] :with Hidden attributes to serialize additionally
#
# @return [SeregaPlan] Serialization plan
#
def initialize(modifiers)
def initialize(parent_plan_point, modifiers)
@parent_plan_point = parent_plan_point
@points = attributes_points(modifiers)
end

Expand Down Expand Up @@ -107,8 +105,7 @@ def attributes_points(modifiers)
{only: only[name], with: with[name], except: except[name]}
end

point = serializer_class::SeregaPlanPoint.new(attribute, child_fields)
point.plan = self
point = serializer_class::SeregaPlanPoint.new(self, attribute, child_fields)
points << point.freeze
end

Expand Down
10 changes: 5 additions & 5 deletions lib/serega/plan_point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module InstanceMethods

# Link to current plan this point belongs to
# @return [SeregaAttribute] Current plan
attr_accessor :plan
attr_reader :plan

# Shows current attribute
# @return [SeregaAttribute] Current attribute
Expand Down Expand Up @@ -44,6 +44,7 @@ module InstanceMethods
#
# Initializes plan point
#
# @param plan [SeregaPlan] Current plan this point belongs to
# @param attribute [SeregaAttribute] Attribute to construct plan point
# @param modifiers Serialization parameters
# @option modifiers [Hash] :only The only attributes to serialize
Expand All @@ -52,7 +53,8 @@ module InstanceMethods
#
# @return [SeregaPlanPoint] New plan point
#
def initialize(attribute, modifiers = nil)
def initialize(plan, attribute, modifiers = nil)
@plan = plan
@attribute = attribute
@modifiers = modifiers
set_normalized_vars
Expand All @@ -79,9 +81,7 @@ def prepare_child_plan

fields = modifiers || FROZEN_EMPTY_HASH

plan = serializer::SeregaPlan.new(fields)
plan.parent_plan_point = self
plan
serializer::SeregaPlan.new(self, fields)
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/serega/object_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def serialize(object)
end

context "when no nested serializers" do
let(:plan) { serializer_class::SeregaPlan.new({only: {foo: {}}}) }
let(:plan) { serializer_class::SeregaPlan.new(nil, {only: {foo: {}}}) }

it "serializes object to hash" do
expect(serialize(1)).to eq(foo: "bar")
Expand All @@ -42,7 +42,7 @@ def serialize(object)
end

context "with nested serializers" do
let(:plan) { serializer_class::SeregaPlan.new({only: {foo: {}, hash: {}, array: {}}}) }
let(:plan) { serializer_class::SeregaPlan.new(nil, {only: {foo: {}, hash: {}, array: {}}}) }

before do
serializer_class.attribute(:hash, serializer: serializer_class, const: 1, hide: true)
Expand Down
6 changes: 3 additions & 3 deletions spec/serega/plugins/batch/lib/modules/plan_point_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
attribute = serializer.attribute :foo,
batch: {loader: loader, key: :id}

batch = serializer::SeregaPlanPoint.new(attribute, []).batch
batch = serializer::SeregaPlanPoint.new("plan", attribute, []).batch

expect(batch).to eq attribute.batch
end
Expand All @@ -25,15 +25,15 @@
attribute = serializer.attribute :foo,
batch: {loader: :loader_name, key: :id}

batch = serializer::SeregaPlanPoint.new(attribute, []).batch
batch = serializer::SeregaPlanPoint.new("plan", attribute, []).batch
expect(batch[:loader]).to eq loader
end

it "raises error when loader not defined" do
attribute = serializer.attribute :foo,
batch: {loader: :loader_name, key: :id}

expect { serializer::SeregaPlanPoint.new(attribute) }.to raise_error Serega::SeregaError,
expect { serializer::SeregaPlanPoint.new("plan", attribute) }.to raise_error Serega::SeregaError,
start_with("Batch loader with name `:loader_name` was not defined")
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/serega/plugins/if/if_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
before { serializer.plugin :if }

def point(attribute)
attribute.class.serializer_class::SeregaPlanPoint.new(attribute, nil)
attribute.class.serializer_class::SeregaPlanPoint.new("plan", attribute, nil)
end

describe "#satisfy_if_conditions?" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

describe "PlanPointMethods" do
def plan_point(attribute, modifiers = nil)
attribute.class.serializer_class::SeregaPlanPoint.new(attribute, modifiers)
attribute.class.serializer_class::SeregaPlanPoint.new("plan", attribute, modifiers)
end

it "delegates #preloads_path to attribute" do
Expand Down

0 comments on commit 3c75035

Please sign in to comment.