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

リポジトリ #6

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem 'activerecord'
gem 'debase'
gem 'guard'
gem 'guard-minitest'
Expand All @@ -13,7 +14,9 @@ gem 'launchy'
gem 'minitest'
gem 'minitest-reporters'
gem 'rake'
gem 'rom'
gem 'rom-sql'
gem 'rubocop', require: false
gem 'ruby-debug-ide'
gem 'simplecov', require: false, group: :tes
gem 'simplecov', require: false, group: :test
gem 'sqlite3'
80 changes: 80 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,16 +1,56 @@
GEM
remote: https://rubygems.org/
specs:
activemodel (6.0.2)
activesupport (= 6.0.2)
activerecord (6.0.2)
activemodel (= 6.0.2)
activesupport (= 6.0.2)
activesupport (6.0.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.2)
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
ansi (1.5.0)
ast (2.4.0)
builder (3.2.4)
coderay (1.1.2)
concurrent-ruby (1.1.6)
debase (0.2.4.1)
debase-ruby_core_source (>= 0.10.2)
debase-ruby_core_source (0.10.9)
docile (1.3.2)
dry-configurable (0.11.5)
concurrent-ruby (~> 1.0)
dry-core (~> 0.4, >= 0.4.7)
dry-equalizer (~> 0.2)
dry-container (0.7.2)
concurrent-ruby (~> 1.0)
dry-configurable (~> 0.1, >= 0.1.3)
dry-core (0.4.9)
concurrent-ruby (~> 1.0)
dry-equalizer (0.3.0)
dry-inflector (0.2.0)
dry-initializer (3.0.3)
dry-logic (1.0.6)
concurrent-ruby (~> 1.0)
dry-core (~> 0.2)
dry-equalizer (~> 0.2)
dry-struct (1.3.0)
dry-core (~> 0.4, >= 0.4.4)
dry-equalizer (~> 0.3)
dry-types (~> 1.3)
ice_nine (~> 0.11)
dry-types (1.4.0)
concurrent-ruby (~> 1.0)
dry-container (~> 0.3)
dry-core (~> 0.4, >= 0.4.4)
dry-equalizer (~> 0.3)
dry-inflector (~> 0.1, >= 0.1.2)
dry-logic (~> 1.0, >= 1.0.2)
ffi (1.12.2)
ffi (1.12.2-x64-mingw32)
formatador (0.2.5)
Expand All @@ -33,6 +73,9 @@ GEM
guard-shell (0.7.1)
guard (>= 2.0.0)
guard-compat (~> 1.0)
i18n (1.8.2)
concurrent-ruby (~> 1.0)
ice_nine (0.11.2)
jaro_winkler (1.5.4)
launchy (2.4.3)
addressable (~> 2.3)
Expand Down Expand Up @@ -63,6 +106,34 @@ GEM
rb-fsevent (0.10.3)
rb-inotify (0.10.1)
ffi (~> 1.0)
rom (5.2.4)
rom-changeset (~> 5.2, >= 5.2.3)
rom-core (~> 5.2, >= 5.2.3)
rom-repository (~> 5.2, >= 5.2.2)
rom-changeset (5.2.3)
dry-core (~> 0.4)
rom-core (~> 5.2)
transproc (~> 1.0, >= 1.1.0)
rom-core (5.2.3)
concurrent-ruby (~> 1.1)
dry-container (~> 0.7)
dry-core (~> 0.4)
dry-equalizer (~> 0.2)
dry-inflector (~> 0.1)
dry-initializer (~> 3.0, >= 3.0.1)
dry-struct (~> 1.0)
dry-types (~> 1.0)
transproc (~> 1.0, >= 1.1.0)
rom-repository (5.2.2)
dry-core (~> 0.4)
dry-initializer (~> 3.0, >= 3.0.1)
rom-core (~> 5.2, >= 5.2.2)
rom-sql (3.2.0)
dry-core (~> 0.4)
dry-equalizer (~> 0.2)
dry-types (~> 1.0)
rom-core (~> 5.2, >= 5.2.1)
sequel (>= 4.49)
rubocop (0.79.0)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
Expand All @@ -73,20 +144,27 @@ GEM
ruby-debug-ide (0.7.0)
rake (>= 0.8.1)
ruby-progressbar (1.10.1)
sequel (5.33.0)
shellany (0.0.1)
simplecov (0.18.2)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov-html (0.12.0)
sqlite3 (1.4.2)
thor (1.0.1)
thread_safe (0.3.6)
transproc (1.1.1)
tzinfo (1.2.6)
thread_safe (~> 0.1)
unicode-display_width (1.6.1)
zeitwerk (2.3.0)

PLATFORMS
ruby
x64-mingw32

DEPENDENCIES
activerecord
debase
guard
guard-minitest
Expand All @@ -96,6 +174,8 @@ DEPENDENCIES
minitest
minitest-reporters
rake
rom
rom-sql
rubocop
ruby-debug-ide
simplecov
Expand Down
16 changes: 16 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

require 'active_record'
require 'rake/testtask'
require 'rubocop/rake_task'
require './lib/db.rb'
RuboCop::RakeTask.new

task default: [:guard]
Expand Down Expand Up @@ -29,4 +31,18 @@ end
desc 'Show Coverate Report'
task :coverage_report do
sh 'launchy ./coverage/index.html'
end

namespace :db do
DB.connect

desc 'CreateDatabase'
task :create do
DB.create
end

desc 'DropDatabase'
task :drop do
DB.destroy
end
end
43 changes: 43 additions & 0 deletions lib/db.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

class InitialSchema < ActiveRecord::Migration[4.2]
def self.up
create_table :users do |t|
t.string :name
end
end

def self.down
drop_table :users
end
end

class DB
class << self
def connect
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: 'sns.db'
)
end

def create
InitialSchema.migrate(:up)
end

def destroy
InitialSchema.migrate(:down)
end
end
end

class TestDB < DB
class << self
def connect
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: ':memory:'
)
end
end
end
9 changes: 7 additions & 2 deletions lib/sns.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# frozen_string_literal: true

require 'active_record'
require 'sqlite3'
require 'securerandom'
require './lib/db.rb'
require './lib/user_id.rb'
require './lib/user_name.rb'
require './lib/user.rb'
require './lib/user_service.rb'
require 'sqlite3'
require 'securerandom'
require './lib/user_repository.rb'

DB.connect
28 changes: 8 additions & 20 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
# frozen_string_literal: true

# User
class User
attr_reader :id, :name

def initialize(user_name:)
@id = UserId.new(SecureRandom.uuid.to_str)
@name = user_name
end

def change_name(name)
raise if name.nil?

@name = name
end

def eql?(other)
@id == other.id
class User < ActiveRecord::Base
def user_name
@user_name = UserName.new(name)
end

def ==(other)
other.equal?(self) || other.instance_of?(self.class) && other.id == id
def user_name=(user_name)
self.name = user_name.value
@user_name = user_name
end

def hash
id.hash
def change_name(name)
self.name = name
end
end
27 changes: 27 additions & 0 deletions lib/user_repository.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

class InitialSchema < ActiveRecord::Migration[4.2]
def self.up
create_table :users do |t|
t.string :name
end
end

def self.down
drop_table :users
end
end

class UserRepository
def initialize; end

def save(user)
user.save
end

def find(user)
User.find_by(name: user.name)
end

def destroy; end
end
10 changes: 6 additions & 4 deletions lib/user_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

# UserService
class UserService
def initialize(user_repository:)
@repository = user_repository
end

def exist?(user)
db = SQLite3::Database.new('sns.db')
sql = 'SELECT * FROM USERS WHERE name = :name'
result = db.execute(sql, name: user.name.value)
!result.empty?
result = @repository.find(user)
result.present?
end
end
7 changes: 6 additions & 1 deletion main.rb
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
require './test/user_test.rb'
# frozen_string_literal: true

require './test/user_test'
require './test/user_service_test'
require './test/activerecord_test'
require './test/rom_test'
43 changes: 43 additions & 0 deletions test/activerecord_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require './test/test_helper'
require './lib/sns.rb'

class ActiverecordTest < Minitest::Test
describe '学習用テスト' do
def setup
TestDB.connect
TestDB.create
end

def test_ユーザーを登録する
User.create(user_name: UserName.new('Bob'))
user = User.find_by(name: 'Bob')

assert_equal 'Bob', user.name
end

def test_ユーザーを更新する
User.create(user_name: UserName.new('Bob'))
user = User.find_by(name: 'Bob')
user.name = 'Alice'
user.save
user = User.find_by(name: 'Alice')

assert_equal 'Alice', user.name
end

def test_ユーザーを削除する
User.create(user_name: UserName.new('Bob'))
user = User.find_by(name: 'Bob')
user.destroy
user = User.find_by(name: 'Bob')

assert user.nil?
end

def teardown
TestDB.destroy
end
end
end
Loading