Skip to content

Commit

Permalink
Schema and Field #==
Browse files Browse the repository at this point in the history
  • Loading branch information
ismasan committed Jan 30, 2024
1 parent bf356b9 commit 4581ce6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/parametric/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def initialize(key, registry = Parametric.registry)
@policies = []
end

def ==(other)
other.is_a?(Field) && key == other.key && policies == other.policies && meta_data == other.meta_data
end

def meta(hash = nil)
@meta_data = @meta_data.merge(hash) if hash.is_a?(Hash)
self
Expand Down
4 changes: 4 additions & 0 deletions lib/parametric/policy_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,9 @@ def meta_data
def key
@policy.key
end

def ==(other)
key == other.key && meta_data == other.meta_data
end
end
end
4 changes: 4 additions & 0 deletions lib/parametric/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def initialize(options = {}, &block)
@after_hooks = []
end

def ==(other)
other.is_a?(Schema) && fields == other.fields
end

def schema
self
end
Expand Down
18 changes: 18 additions & 0 deletions spec/field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ def has_error(key, message)
end
end

describe '#==' do
it 'compares by key' do
field_a = described_class.new(:a_key, registry)
field_b = described_class.new(:a_key, registry)
field_c = described_class.new(:another_key, registry)
expect(field_a).to eq(field_b)
expect(field_a).not_to eq(field_c)
end

it 'compares by policies' do
field_a = described_class.new(:a_key, registry).type(:integer)
field_b = described_class.new(:a_key, registry).type(:integer)
field_c = described_class.new(:a_key, registry).type(:integer).options([1,2,3])
expect(field_a).to eq(field_b)
expect(field_a).not_to eq(field_c)
end
end

describe "#policy" do
it "coerces integer" do
subject.policy(:integer)
Expand Down
23 changes: 23 additions & 0 deletions spec/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,29 @@
end
end

specify '#==' do
schema2 = described_class.new do
field(:title).policy(:string).present
field(:price).policy(:integer).meta(label: "A price")
field(:status).policy(:string).options(['visible', 'hidden'])
field(:tags).policy(:split).policy(:array)
field(:description).policy(:string)
field(:variants).policy(:array).schema do
field(:name).policy(:string).present
field(:sku)
field(:stock).policy(:integer).default(1)
field(:available_if_no_stock).policy(:boolean).policy(:flexible_bool).default(false)
end
end

schema3 = described_class.new do
field(:title).policy(:string).present
end

expect(subject).to eq schema2
expect(subject).not_to eq schema3
end

def resolve(schema, payload, &block)
yield schema.resolve(payload)
end
Expand Down

0 comments on commit 4581ce6

Please sign in to comment.