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

[WIP] Update to ROM 3.x #15

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
18 changes: 12 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
language: ruby
dist: trusty
cache: bundler
bundler_args: --without tools
script: "bundle exec rake ci"
env:
- CODECLIMATE_REPO_TOKEN=a49f28520e49a35d73c9b6ed3d78ae110e4a5d05b9f154b999578a5175add09d
global:
- CODECLIMATE_REPO_TOKEN=a49f28520e49a35d73c9b6ed3d78ae110e4a5d05b9f154b999578a5175add09d
- JRUBY_OPTS='--dev -J-Xmx1024M'
after_success:
- - '[ "${TRAVIS_JOB_NUMBER#*.}" = "1" ] && [ "$TRAVIS_BRANCH" = "master" ] && bundle exec codeclimate-test-reporter'
rvm:
- 2.0
- 2.1
- 2.4.0
- 2.3
- 2.2
- 2.3.0
- rbx-2
- jruby-9000
- rbx-3
- jruby-9.1.7.0
- ruby-head
matrix:
allow_failures:
- rvm: rbx-3
- rvm: ruby-head
notifications:
webhooks:
Expand Down
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ source 'https://rubygems.org'
gemspec

group :test do
gem 'dry-struct'
gem 'rom', git: 'https://github.com/rom-rb/rom.git', branch: 'master'
gem 'virtus'
gem 'rom-repository', git: 'https://github.com/rom-rb/rom-repository', branch: 'master'
gem 'rspec', '~> 3.1'
gem 'codeclimate-test-reporter', require: false
gem 'inflecto'
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Or install it yourself as:

## Usage

See `spec/integration/repository_spec.rb` or [examples/](https://github.com/rom-rb/rom-csv/tree/master/examples) folder for a sample usage.
See `spec/integration/gateway_spec.rb` or [examples/](https://github.com/rom-rb/rom-csv/tree/master/examples) folder for a sample usage.

## License

Expand Down
1 change: 0 additions & 1 deletion examples/find_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
csv_file = ARGV[0] || File.expand_path("./users.csv", File.dirname(__FILE__))

configuration = ROM::Configuration.new(:csv, csv_file)
configuration.use(:macros)

configuration.relation(:users) do
def by_name(name)
Expand Down
3 changes: 2 additions & 1 deletion lib/rom/csv/commands/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ class Create < ROM::Commands::Create
def execute(tuples)
insert_tuples = [tuples].flatten.map do |tuple|
attributes = input[tuple]
validator.call(attributes)
attributes.to_h
end

insert(insert_tuples)
insert_tuples
end

private

def insert(tuples)
tuples.each { |tuple| dataset << new_row(tuple) }
dataset.sync!
Expand Down
2 changes: 2 additions & 0 deletions lib/rom/csv/commands/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def execute
dataset.data
end

private

def dataset
relation.dataset
end
Expand Down
3 changes: 2 additions & 1 deletion lib/rom/csv/commands/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ class Update < ROM::Commands::Update

def execute(tuple)
attributes = input[tuple]
validator.call(attributes)
tuple = attributes.to_h

update(tuple)
end

private

def update(tuple)
original_data = original_dataset.to_a
output = []
Expand Down
31 changes: 15 additions & 16 deletions lib/rom/csv/dataset.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
require 'rom/memory/dataset'
require 'rom/csv/storage'

module ROM
module CSV
# Type definition used to constrain the `connection` option
StorageType = Types.Definition(Storage).constrained(type: Storage)

# Dataset for CSV
#
# @api public
class Dataset < ROM::Memory::Dataset
option :path, reader: true
option :file_options, reader: true

# Connection to the file
#
# @return [Storage]
#
# @api private
option :connection,
optional: true,
type: StorageType

# Convert each CSV::Row to a hash
#
Expand All @@ -17,23 +28,11 @@ def self.row_proc
end

def reload!
@data = load_data
@data = connection.load
end

def sync!
write_data && reload!
end

def write_data
::CSV.open(path, 'wb', file_options) do |csv|
data.to_a.each do |tuple|
csv << tuple
end
end
end

def load_data
::CSV.table(path, file_options).by_row!
connection.dump(data) && reload!
end

def count
Expand Down
36 changes: 21 additions & 15 deletions lib/rom/csv/gateway.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'rom/gateway'
require 'rom/initializer'
require 'rom/csv/dataset'
require 'rom/csv/commands'
require 'rom/csv/storage'

# Ruby Object Mapper
#
Expand Down Expand Up @@ -40,6 +42,20 @@ module CSV
#
# @api public
class Gateway < ROM::Gateway
extend Initializer

param :path,
reader: :private,
type: Types::Strict::String
param :csv_options,
default: proc { {} },
reader: :private,
type: Types::Strict::Hash

# @api private
attr_reader :datasets
private :datasets

# Expect a path to a single csv file which will be registered by rom to
# the given name or :default as the gateway.
#
Expand All @@ -49,16 +65,15 @@ class Gateway < ROM::Gateway
# * header_converters: :symbol
#
# @param path [String] path to csv
# @param options [Hash] options passed to CSV.table
# @param csv_options [Hash] options passed to CSV.table
#
# @api private
#
# @see CSV.table
def initialize(path, options = {})
def initialize(*)
super
@datasets = {}
@path = path
@options = options
@connection = ::CSV.table(path, options).by_row!
@connection = Storage.new(path, csv_options)
end

# Return dataset with the given name
Expand All @@ -80,7 +95,7 @@ def [](name)
#
# @api public
def dataset(name)
datasets[name] = Dataset.new(connection, dataset_options)
datasets[name] = Dataset.new(connection.load, connection: connection)
end

# Check if dataset exists
Expand All @@ -91,15 +106,6 @@ def dataset(name)
def dataset?(name)
datasets.key?(name)
end

private

def dataset_options
{ path: path, file_options: options }
end

# @api private
attr_reader :datasets, :path, :options
end
end
end
8 changes: 4 additions & 4 deletions lib/rom/csv/relation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'rom/relation'
require 'rom/memory'
require 'rom/plugins/relation/key_inference'

module ROM
module CSV
Expand All @@ -9,10 +10,9 @@ module CSV
# end
#
# @api public
class Relation < ROM::Relation
class Relation < ROM::Memory::Relation
adapter :csv

forward :join, :project, :restrict, :order
use :key_inference

def count
dataset.count
Expand Down
52 changes: 52 additions & 0 deletions lib/rom/csv/storage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'csv'
require 'rom/initializer'

module ROM
module CSV
# CSV file storage for datasets
#
# @api private
class Storage
extend Initializer

# Path to the file
#
# @return [String]
#
# @api private
param :path,
type: Types::Strict::String

# Options for file passed to `CSV`
#
# @return [Hash]
#
# @api private
param :csv_options,
default: proc { {} },
type: Types::Strict::Hash

# Dump the data to the file at `path`
#
# @return [undefined]
#
# @api public
def dump(data)
::CSV.open(path, 'wb', csv_options) do |csv|
data.to_a.each do |tuple|
csv << tuple
end
end
end

# Load the data from the file at `path`
#
# @return [CSV::Table]
#
# @api public
def load
::CSV.table(path, csv_options).by_row!
end
end
end
end
13 changes: 13 additions & 0 deletions lib/rom/csv/types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'rom/types'

module ROM
module CSV
module Types
include ROM::Types

def self.Definition(primitive)
Dry::Types::Definition.new(primitive)
end
end
end
end
5 changes: 3 additions & 2 deletions rom-csv.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']

spec.add_runtime_dependency 'rom', '~> 1.0'
spec.add_runtime_dependency 'rom', '~> 3.0'

spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rake', '10.3'
spec.add_development_dependency 'rspec', '~> 3.5'
spec.add_development_dependency 'rubocop', '~> 0.28.0'
end
16 changes: 8 additions & 8 deletions spec/integration/commands/create_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'spec_helper'
require 'virtus'
require 'dry-struct'

describe 'Commands / Create' do
include_context 'database setup'
Expand All @@ -9,17 +9,17 @@
before do
configuration.relation(:users)

class User
include Virtus.model

attribute :id, Integer
attribute :name, String
attribute :email, String
module Test
class User < Dry::Struct
attribute :user_id, Types::Strict::Int
attribute :name, Types::Strict::String
attribute :email, Types::Strict::String
end
end

configuration.mappers do
define(:users) do
model User
model Test::User
register_as :entity
end
end
Expand Down
7 changes: 0 additions & 7 deletions spec/integration/commands/delete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ def by_id(id)
end
end

it 'raises error when tuple count does not match expectation' do
result = users.try { users.delete.call }

expect(result.value).to be(nil)
expect(result.error).to be_instance_of(ROM::TupleCountMismatchError)
end

it 'deletes all tuples in a restricted relation' do
result = users.try { users.delete.by_id(1).call }

Expand Down
Loading