Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/Initialize Relation with :qualified_name parameter. #54

Merged
merged 1 commit into from
Nov 25, 2023
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: 11 additions & 4 deletions lib/cuprum/collections/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ def resolve_parameters(parameters, **ambiguous)

# Methods for resolving a relations's naming and entity class from options.
module Parameters # rubocop:disable Metrics/ModuleLength
PARAMETER_KEYS = %i[entity_class name qualified_name].freeze
private_constant :PARAMETER_KEYS

class << self
# @overload resolve_parameters(entity_class: nil, singular_name: nil, name: nil, qualified_name: nil)
# Helper method for resolving a Relation's required parameters.
Expand Down Expand Up @@ -262,10 +265,14 @@ def validate_parameter(value, as:)
value.to_s
end

def validate_parameters(**params) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
unless has_key?(params, :entity_class) || has_key?(params, :name)
raise ArgumentError, "name or entity class can't be blank"
end
def validate_parameter_keys(params)
return if PARAMETER_KEYS.any? { |key| has_key?(params, key) }

raise ArgumentError, "name or entity class can't be blank"
end

def validate_parameters(**params) # rubocop:disable Metrics/MethodLength
validate_parameter_keys(params)

if has_key?(params, :entity_class)
validate_entity_class(params[:entity_class])
Expand Down
9 changes: 9 additions & 0 deletions lib/cuprum/collections/rspec/contracts/relation_contracts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,15 @@ module ShouldValidateTheParametersContract
end
end

describe 'with qualified_name: value and name, entity_class: nil' do
it 'should not raise an exception' do
expect do
call_method(entity_class: nil, name: nil, qualified_name: 'books')
end
.not_to raise_error
end
end

describe 'with singular_name: an Object' do
let(:error_message) { 'singular name is not a String or a Symbol' }

Expand Down
Loading