Skip to content

Commit

Permalink
Merge pull request #54 from sleepingkingstudios/chore/relation-parame…
Browse files Browse the repository at this point in the history
…ters

Chore/Initialize Relation with :qualified_name parameter.
  • Loading branch information
sleepingkingstudios authored Nov 25, 2023
2 parents a1bb3d7 + 9e08ee1 commit 4ef78ab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
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

0 comments on commit 4ef78ab

Please sign in to comment.