Skip to content

Commit

Permalink
Implement Associations::HasOne, HasMany.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepingkingstudios committed Nov 27, 2023
1 parent b1e7634 commit b3a81ce
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/cuprum/collections/associations/has_many.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require 'cuprum/collections/association'
require 'cuprum/collections/associations'

module Cuprum::Collections::Associations
# Object representing a has_many association.
class HasMany < Cuprum::Collections::Association
# (see Cuprum::Collections::Association#initialize)
def initialize(**params)
params.delete(:plural)
params.delete(:singular)

super(**params, singular: false)
end

private

def ignored_parameters
@ignored_parameters ||= Set.new(IGNORED_PARAMETERS + %i[singular])
end
end
end
23 changes: 23 additions & 0 deletions lib/cuprum/collections/associations/has_one.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require 'cuprum/collections/association'
require 'cuprum/collections/associations'

module Cuprum::Collections::Associations
# Object representing a has_one association.
class HasOne < Cuprum::Collections::Association
# (see Cuprum::Collections::Association#initialize)
def initialize(**params)
params.delete(:plural)
params.delete(:singular)

super(**params, singular: true)
end

private

def ignored_parameters
@ignored_parameters ||= Set.new(IGNORED_PARAMETERS + %i[singular])
end
end
end
27 changes: 27 additions & 0 deletions spec/cuprum/collections/associations/has_many_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require 'cuprum/collections/associations/has_many'
require 'cuprum/collections/rspec/contracts/association_contracts'

require 'support/book'
require 'support/grimoire'
require 'support/scoped_book'

RSpec.describe Cuprum::Collections::Associations::HasMany do
include Cuprum::Collections::RSpec::Contracts::AssociationContracts

subject(:association) { described_class.new(**constructor_options) }

let(:name) { 'books' }
let(:constructor_options) { { name: name } }

include_contract 'should be a has association'

describe '#plural?' do
it { expect(association.plural?).to be true }
end

describe '#singular?' do
it { expect(association.singular?).to be false }
end
end
27 changes: 27 additions & 0 deletions spec/cuprum/collections/associations/has_one_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require 'cuprum/collections/associations/has_one'
require 'cuprum/collections/rspec/contracts/association_contracts'

require 'support/book'
require 'support/grimoire'
require 'support/scoped_book'

RSpec.describe Cuprum::Collections::Associations::HasOne do
include Cuprum::Collections::RSpec::Contracts::AssociationContracts

subject(:association) { described_class.new(**constructor_options) }

let(:name) { 'book' }
let(:constructor_options) { { name: name } }

include_contract 'should be a has association'

describe '#plural?' do
it { expect(association.plural?).to be false }
end

describe '#singular?' do
it { expect(association.singular?).to be true }
end
end

0 comments on commit b3a81ce

Please sign in to comment.