Skip to content

Commit

Permalink
chore/handle_key_not_found- chore: support render unless key not found
Browse files Browse the repository at this point in the history
  • Loading branch information
comictvn committed Nov 23, 2023
1 parent 70660ce commit d452969
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ property :name, default: 'Paladin'
- `render_nil` to render or skip nil value (default is false):
```ruby
# will render { name: nil } if name is nil
```
- `render_if_key_found` to render or skip if key not found (default is false):
```ruby
property :name, render_nil: true
```
- `representer` to use different representer for nested objects.
Expand Down
5 changes: 5 additions & 0 deletions lib/simple_representer/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def initialize(field, options)
end

def call(representer)
@representer = representer

return if options[:if] && !representer.instance_exec(&options[:if])

value = process(representer)
Expand All @@ -21,6 +23,8 @@ def call(representer)

private

attr_reader :representer

def nested_representer(value)
return options[:representer].for_collection(value).to_h if value.is_a?(Array)

Expand All @@ -29,6 +33,7 @@ def nested_representer(value)

def build_field(value)
return if value.nil? && !options.fetch(:render_nil, false)
return if value.nil? && options.fetch(:render_if_key_found, false) && !representer.represented.respond_to?(field)

[(options[:as] || field).to_sym, value]
end
Expand Down
5 changes: 3 additions & 2 deletions spec/simple_representer/simple_representer/definable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Test
include SimpleRepresenter::Definable

defaults render_nil: true
defaults render_if_key_found: true

property :name, if: -> { true }
computed :id, as: :uid
Expand All @@ -17,9 +18,9 @@ class Test
expect(subject.size).to eq(2)
expect(subject[0]).to be_kind_of(SimpleRepresenter::Property)
expect(subject[0].field).to eq(:name)
expect(subject[0].options.keys).to include(:if, :render_nil)
expect(subject[0].options.keys).to include(:if, :render_nil, :render_if_key_found)
expect(subject[1]).to be_kind_of(SimpleRepresenter::Computed)
expect(subject[1].field).to eq(:id)
expect(subject[1].options).to eq({ as: :uid, render_nil: true })
expect(subject[1].options).to eq({ as: :uid, render_nil: true, render_if_key_found: true })
end
end
22 changes: 22 additions & 0 deletions spec/simple_representer/simple_representer/property_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require_relative '../../../lib/simple_representer/representer'
require_relative '../../../lib/simple_representer/property'

RSpec.describe SimpleRepresenter::Property do
Expand Down Expand Up @@ -85,4 +86,25 @@ class NestedRepresenter < SimpleRepresenter::Representer
it { is_expected.to eq([:name, nil]) }
end
end

context 'with render_if_key_found option' do
let(:representer) { double({ represented: OpenStruct.new(name: 'Paladin') }) }

context 'should render field if key is found by default' do
let(:options) { {} }
it { is_expected.to eq([:name, 'Paladin']) }
end

context 'should not render field if render_if_key_found is true' do
let(:representer) { double({ represented: OpenStruct.new(inside_name: 'Paladin') }) }
let(:options) { { render_if_key_found: true, render_nil: true } }
it { is_expected.to eq(nil) }
end

context 'should render field if render_if_key_found is false' do
let(:representer) { double({ represented: OpenStruct.new(inside_name: 'Paladin') }) }
let(:options) { { render_if_key_found: false, render_nil: true } }
it { is_expected.to eq([:name, nil]) }
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
RSpec.describe SimpleRepresenter::Representer do
class CompanyRepresenter < SimpleRepresenter::Representer
defaults render_nil: true
defaults render_if_key_found: false
property :name
end

Expand Down

0 comments on commit d452969

Please sign in to comment.