-
Notifications
You must be signed in to change notification settings - Fork 9
Upgrade to ROM v4 #16
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
Open
v-kolesnikov
wants to merge
9
commits into
rom-rb:master
Choose a base branch
from
v-kolesnikov:upgrade/rom-4
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
13a81a1
Add pry-byebug
v-kolesnikov b5f93b5
Update rom to rom-core 4.0
v-kolesnikov fa5cc08
Update spec_helper
v-kolesnikov 241efa9
Remove deprecated `macros` plugin
v-kolesnikov ef06f96
Refactoring Gateway, Dataset and Relation
v-kolesnikov 6865721
Add integration tests for adapter
v-kolesnikov 1d09dc9
Allow to provide options for CSV loading
v-kolesnikov 678580a
Fix examples
v-kolesnikov 230548b
Use same ruby versions as rom on CI
v-kolesnikov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -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) | ||
|
|
@@ -27,7 +26,7 @@ class User < OpenStruct | |
|
|
||
| container = ROM.container(configuration) | ||
|
|
||
| user = container.relation(:users).as(:entity).by_name('Jane').one | ||
| user = container.relations[:users].as(:entity).by_name('Jane').one | ||
| # => #<User id=2, name="Jane", email="[email protected]"> | ||
|
|
||
| user or abort "user not found" | ||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,64 +1,90 @@ | ||
| require 'csv' | ||
|
|
||
| require 'rom/gateway' | ||
| require 'rom/csv/dataset' | ||
| require 'rom/csv/commands' | ||
|
|
||
| # Ruby Object Mapper | ||
| # | ||
| # @see http://rom-rb.org/ | ||
| module ROM | ||
| # CSV support for ROM | ||
| # | ||
| # @example | ||
| # require 'rom/csv' | ||
| # require 'ostruct' | ||
| # | ||
| # setup = ROM.setup(:csv, "./spec/fixtures/users.csv") | ||
| # setup.relation(:users) do | ||
| # def by_name(name) | ||
| # dataset.find_all { |row| row[:name] == name } | ||
| # end | ||
| # end | ||
| # | ||
| # class User < OpenStruct | ||
| # end | ||
| # | ||
| # setup.mappers do | ||
| # define(:users) do | ||
| # model User | ||
| # end | ||
| # end | ||
| # | ||
| # rom = setup.finalize | ||
| # p rom.read(:users).by_name('Jane').one | ||
| # # => #<User id=2, name="Jane", email="[email protected]"> | ||
| # | ||
| # **Note: rom-csv is read only at the moment.** | ||
| # | ||
| # @api public | ||
| module CSV | ||
| # CSV gateway interface | ||
| # CSV gateway | ||
| # | ||
| # Connects to a CSV file and uses it as a data-source | ||
| # | ||
| # @example | ||
| # rom = ROM.container(:csv, '/path/to/products.scv') | ||
| # gateway = rom.gateways[:default] | ||
| # gateway[:products] # => data from the csv file | ||
| # | ||
| # @api public | ||
| class Gateway < ROM::Gateway | ||
| # Expect a path to a single csv file which will be registered by rom to | ||
| # the given name or :default as the gateway. | ||
| adapter :csv | ||
|
|
||
| # @attr_reader [Hash] sources Data loaded from files | ||
| # | ||
| # @api private | ||
| attr_reader :sources | ||
|
|
||
| # @attr_reader [Hash] datasets CSV datasets | ||
| # | ||
| # @api private | ||
| attr_reader :datasets | ||
|
|
||
| # Create a new CSV gateway from a path to file(s) | ||
| # | ||
| # @example | ||
| # gateway = ROM::CSV::Gateway.new('/path/to/files') | ||
| # | ||
| # Uses CSV.table which passes the following csv options: | ||
| # * headers: true | ||
| # * converters: numeric | ||
| # * header_converters: :symbol | ||
| # | ||
| # @param path [String] path to csv | ||
| # @see CSV.table | ||
| # | ||
| # @param [String, Pathname] path The path to your CSV file(s) | ||
| # @param options [Hash] options passed to CSV.table | ||
| # | ||
| # @return [Gateway] | ||
| # | ||
| # @api public | ||
| def self.new(path, options = {}) | ||
| super(load_from(path, options)) | ||
| end | ||
|
|
||
| # Load data from CSV file(s) | ||
| # | ||
| # @api private | ||
| def self.load_from(path, options = {}) | ||
| if File.directory?(path) | ||
| load_files(path, options) | ||
| else | ||
| { source_name(path) => load_file(path, options) } | ||
| end | ||
| end | ||
|
|
||
| # Load CSV files from a given directory and return a name => data map | ||
| # | ||
| # @see CSV.table | ||
| def initialize(path, options = {}) | ||
| # @api private | ||
| def self.load_files(path, options = {}) | ||
| Dir["#{path}/*.csv"].each_with_object({}) do |file, h| | ||
| h[source_name(file)] = load_file(file, options) | ||
| end | ||
| end | ||
|
|
||
| def self.source_name(filename) | ||
| File.basename(filename, '.*') | ||
| end | ||
|
|
||
| # Load CSV file | ||
| # | ||
| # @api private | ||
| def self.load_file(path, options = {}) | ||
| ::CSV.table(path, options).map(&:to_h) | ||
| end | ||
|
|
||
| # @api private | ||
| def initialize(sources) | ||
| @sources = sources | ||
| @datasets = {} | ||
| @path = path | ||
| @options = options | ||
| @connection = ::CSV.table(path, options).by_row! | ||
| end | ||
|
|
||
| # Return dataset with the given name | ||
|
|
@@ -80,7 +106,7 @@ def [](name) | |
| # | ||
| # @api public | ||
| def dataset(name) | ||
| datasets[name] = Dataset.new(connection, dataset_options) | ||
| datasets[name] = Dataset.new(sources.fetch(name.to_s)) | ||
| end | ||
|
|
||
| # Check if dataset exists | ||
|
|
@@ -91,15 +117,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 | ||
This file contains hidden or 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 hidden or 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 hidden or 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,3 @@ | ||
| id,title | ||
| 1,T-Shirt | ||
| 2,Mug |
This file contains hidden or 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,7 @@ | ||
| id,product_id,sku,quantity | ||
| 1,1,TSHIRT1010PINK,3 | ||
| 2,1,TSHIRT1020BLUE,5 | ||
| 3,1,TSHIRT1030GREY,2 | ||
| 4,2,MUG2010WHITE,15 | ||
| 5,2,MUG2010GREEN,10 | ||
| 6,3,MUG2030BROWN,12 |
This file contains hidden or 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,3 @@ | ||
| user_id;name;email | ||
| 1;Jane;[email protected] | ||
| 2;John;[email protected] |
This file contains hidden or 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,58 @@ | ||
| require 'spec_helper' | ||
|
|
||
| require 'rom-repository' | ||
|
|
||
| RSpec.describe ROM::CSV do | ||
| let(:configuration) do | ||
| ROM::Configuration.new(:csv, uri) | ||
| end | ||
|
|
||
| let(:uri) { File.expand_path('./spec/fixtures/users.csv') } | ||
| let(:rom) { ROM.container(configuration) } | ||
|
|
||
| context 'single file configuration' do | ||
| let(:users_relation) do | ||
| Class.new(ROM::CSV::Relation) do | ||
| schema(:users) do | ||
| attribute :user_id, ROM::Types::Int | ||
| attribute :name, ROM::Types::String | ||
| attribute :email, ROM::Types::String | ||
| end | ||
|
|
||
| def by_name(name) | ||
| restrict(name: name) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| before do | ||
| configuration.register_relation(users_relation) | ||
| end | ||
|
|
||
| describe ROM::CSV::Relation do | ||
| describe '#by_name' do | ||
| subject(:jane) { rom.relations[:users].by_name('Jane').one } | ||
|
|
||
| it 'returns object by name' do | ||
| expect(jane[:name]).to eql 'Jane' | ||
| expect(jane[:email]).to eql '[email protected]' | ||
| expect(jane[:user_id]).to eql 3 | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe 'with a repository' do | ||
| let(:repo) do | ||
| Class.new(ROM::Repository[:users]).new(rom) | ||
| end | ||
|
|
||
| it 'auto-maps to structs' do | ||
| user = repo.users.by_name('Jane').one | ||
|
|
||
| expect(user.name).to eql 'Jane' | ||
| expect(user.email).to eql '[email protected]' | ||
| expect(user.user_id).to eql 3 | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need it? I think it will be good to have almost the same gems set in all adapters