-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/t5' into master
- Loading branch information
Showing
14 changed files
with
231 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'dry-struct' | ||
require_relative '../initializer/dry_types' | ||
|
||
require 'boxenn/errors' | ||
|
||
module Boxenn | ||
class Entity < Dry::Struct | ||
alias assign_attributes new | ||
|
||
def self.primary_keys | ||
raise UndefinePrimaryKeys.new(class_name: self.class.name) | ||
end | ||
|
||
def primary_keys_hash | ||
if !self.class.primary_keys.all? { |s| attributes.key? s} | ||
raise UnassignPrimaryKeys.new(class_name: self.class.name) | ||
else | ||
attributes.slice(*self.class.primary_keys) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'dry/initializer' | ||
|
||
module Boxenn | ||
module Repositories | ||
class Factory | ||
|
||
extend Dry::Initializer | ||
|
||
param :entity, default: proc { nil } | ||
|
||
def build | ||
raise NotImplementedError | ||
end | ||
|
||
def primary_keys | ||
entity.primary_keys | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'dry/initializer' | ||
|
||
module Boxenn | ||
module Repositories | ||
class Query | ||
|
||
extend Dry::Initializer | ||
|
||
param :relation, default: proc { nil } | ||
|
||
def collect | ||
raise NotImplementedError | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Boxenn | ||
module Repositories | ||
class RecordMapper | ||
def build | ||
raise NotImplementedError | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'dry/initializer' | ||
|
||
module Boxenn | ||
module Repositories | ||
class SourceWrapper | ||
|
||
extend Dry::Initializer | ||
|
||
param :source, default: proc { nil } | ||
|
||
def find_by(primary_keys) | ||
raise NotImplementedError | ||
end | ||
|
||
def save(primary_keys, attributes) | ||
raise NotImplementedError | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'dry-types' | ||
|
||
module Types | ||
include Dry.Types() | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require 'boxenn/entity' | ||
|
||
RSpec.describe Boxenn::Entity do | ||
|
||
describe '#primary_keys_hash' do | ||
context 'when method is called without defining primary key' do | ||
class Test1 < Boxenn::Entity | ||
|
||
end | ||
let(:entity) { Test1.new } | ||
subject { -> { entity.primary_keys_hash } } | ||
|
||
it { is_expected.to raise_error(Boxenn::UndefinePrimaryKeys) } | ||
end | ||
|
||
context 'when method is called without assigning primary key' do | ||
class Test2 < Boxenn::Entity | ||
def self.primary_keys | ||
[:id1].freeze | ||
end | ||
end | ||
let(:entity) { Test2.new } | ||
subject { -> { entity.primary_keys_hash } } | ||
|
||
it { is_expected.to raise_error(Boxenn::UnassignPrimaryKeys) } | ||
end | ||
|
||
context 'when method is called with valid primary key' do | ||
context 'accepts single primary key' do | ||
class Test3 < Boxenn::Entity | ||
def self.primary_keys | ||
[:id1].freeze | ||
end | ||
|
||
attribute :id1, Types::Integer.default(1) | ||
end | ||
|
||
let(:entity) { Test3.new } | ||
subject { entity.primary_keys_hash } | ||
|
||
it { is_expected.to eq({ id1: 1 }) } | ||
end | ||
|
||
context 'accepts multiple primary key' do | ||
class Test4 < Boxenn::Entity | ||
def self.primary_keys | ||
[:id1, :id2].freeze | ||
end | ||
|
||
attribute :id1, Types::Integer.default(1) | ||
attribute :id2, Types::Integer.default(2) | ||
end | ||
|
||
let(:entity) { Test4.new } | ||
subject { entity.primary_keys_hash } | ||
|
||
it { is_expected.to eq({ id1: 1, id2: 2 }) } | ||
end | ||
end | ||
end | ||
end |
6 changes: 3 additions & 3 deletions
6
spec/unit/query_spec.rb → spec/unit/repositories/query_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.