rom-csv 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0b4fa0eb73ae4be19bf5c40473f3015d008127f0
4
+ data.tar.gz: 7258ee79e604d5fd2698ca7987ff2e617ec87875
5
+ SHA512:
6
+ metadata.gz: 55d0a0a264ff0f404e1fd55d122aba626b740dbe4e42b8638055123e7b0d3d4ed7fcf397e3717e93442a25f2715494c3b6d1776144b3a22dd4e9f29aa1d75031
7
+ data.tar.gz: 7a5c75411dc268e9d6eb7fc7578f292b1d1a0757525bc2d0d2923f284fa3b64a33207eb6e3e09bc95db42d4d89c519a975e12055194e26238b14ac96176e4a45
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --order random
@@ -0,0 +1,67 @@
1
+ # Generated by `rubocop --auto-gen-config`
2
+ inherit_from: .rubocop_todo.yml
3
+
4
+ # It’s quite readable when we know what we are doing
5
+ Lint/AssignmentInCondition:
6
+ Enabled: false
7
+
8
+ # No need to handle LoadError in rake
9
+ Lint/HandleExceptions:
10
+ Exclude:
11
+ - rakelib/*.rake
12
+
13
+ # The enforced style doesn’t match Vim’s defaults
14
+ Style/AlignParameters:
15
+ Enabled: false
16
+
17
+ # UTF-8 is perfectly fine in comments
18
+ Style/AsciiComments:
19
+ Enabled: false
20
+
21
+ # Allow using braces for value-returning blocks
22
+ Style/Blocks:
23
+ Enabled: false
24
+
25
+ # Documentation checked by Inch CI
26
+ Style/Documentation:
27
+ Enabled: false
28
+
29
+ # Early returns have their vices
30
+ Style/GuardClause:
31
+ Enabled: false
32
+
33
+ # Need to be skipped for >-> usage
34
+ Style/Lambda:
35
+ Enabled: false
36
+
37
+ # Multiline block chains are ok
38
+ Style/MultilineBlockChain:
39
+ Enabled: false
40
+
41
+ # Even a single escaped slash can be confusing
42
+ Style/RegexpLiteral:
43
+ MaxSlashes: 0
44
+
45
+ # Don’t introduce semantic fail/raise distinction
46
+ Style/SignalException:
47
+ EnforcedStyle: only_raise
48
+
49
+ # Need to be skipped for >-> usage
50
+ Style/SpaceAroundOperators:
51
+ Enabled: false
52
+
53
+ # Accept both single and double quotes
54
+ Style/StringLiterals:
55
+ Enabled: false
56
+
57
+ # Allow def self.foo; @foo; end
58
+ Style/TrivialAccessors:
59
+ Enabled: false
60
+
61
+ # Allow rom-csv
62
+ Style/FileName:
63
+ Enabled: false
64
+
65
+ # Allow logical `or`/`and` in conditionals
66
+ Style/AndOr:
67
+ EnforcedStyle: conditionals
@@ -0,0 +1,6 @@
1
+ # This configuration was generated by `rubocop --auto-gen-config`
2
+ # on 2015-01-17 15:00:06 +0100 using RuboCop version 0.28.0.
3
+ # The point is for the user to remove these configuration records
4
+ # one by one as the offenses are removed from the code base.
5
+ # Note that changes in the inspected code, or installation of new
6
+ # versions of RuboCop, may require this file to be generated again.
@@ -0,0 +1,22 @@
1
+ language: ruby
2
+ bundler_args: --without tools
3
+ script: "bundle exec rake ci"
4
+ env:
5
+ - CODECLIMATE_REPO_TOKEN=a49f28520e49a35d73c9b6ed3d78ae110e4a5d05b9f154b999578a5175add09d
6
+ rvm:
7
+ - 2.0
8
+ - 2.1
9
+ - 2.2
10
+ - rbx-2
11
+ - jruby
12
+ - ruby-head
13
+ matrix:
14
+ allow_failures:
15
+ - rvm: ruby-head
16
+ notifications:
17
+ webhooks:
18
+ urls:
19
+ - https://webhooks.gitter.im/e/39e1225f489f38b0bd09
20
+ on_success: change
21
+ on_failure: always
22
+ on_start: false
@@ -0,0 +1,3 @@
1
+ ## v0.0.1 2015-03-22
2
+
3
+ First public release
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'rom', git: 'https://github.com/rom-rb/rom.git', branch: 'master'
7
+ gem 'rspec', '~> 3.1'
8
+ gem 'codeclimate-test-reporter', require: false
9
+ gem 'inflecto'
10
+ end
11
+
12
+ group :tools do
13
+ gem 'rubocop'
14
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Don Morrison
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ [gem]: https://rubygems.org/gems/rom-csv
2
+ [travis]: https://travis-ci.org/rom-rb/rom-csv
3
+ [gemnasium]: https://gemnasium.com/rom-rb/rom-csv
4
+ [codeclimate]: https://codeclimate.com/github/rom-rb/rom-csv
5
+ [inchpages]: http://inch-ci.org/github/rom-rb/rom-csv
6
+
7
+ # ROM::CSV
8
+
9
+ [![Gem Version](https://badge.fury.io/rb/rom-csv.svg)][gem]
10
+ [![Build Status](https://travis-ci.org/rom-rb/rom-csv.svg?branch=master)][travis]
11
+ [![Dependency Status](https://gemnasium.com/rom-rb/rom-csv.png)][gemnasium]
12
+ [![Code Climate](https://codeclimate.com/github/rom-rb/rom-csv/badges/gpa.svg)][codeclimate]
13
+ [![Test Coverage](https://codeclimate.com/github/rom-rb/rom-csv/badges/coverage.svg)][codeclimate]
14
+ [![Inline docs](http://inch-ci.org/github/rom-rb/rom-csv.svg?branch=master)][inchpages]
15
+
16
+ CSV support for [Ruby Object Mapper](https://github.com/rom-rb/rom)
17
+
18
+ **Note: rom-csv is read only at the moment.**
19
+
20
+ ## Installation
21
+
22
+ Add this line to your application's Gemfile:
23
+
24
+ ```ruby
25
+ gem "rom-csv"
26
+ ```
27
+
28
+ And then execute:
29
+
30
+ $ bundle
31
+
32
+ Or install it yourself as:
33
+
34
+ $ gem install rom-csv
35
+
36
+ ## Usage
37
+
38
+ See `spec/integration/repository_spec.rb` or [examples/](https://github.com/rom-rb/rom-csv/tree/master/examples) folder for a sample usage.
39
+
40
+ ## License
41
+
42
+ See `LICENSE` file.
@@ -0,0 +1,7 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ task default: :spec
5
+
6
+ desc "Run CI tasks"
7
+ task ci: [:spec, :examples]
@@ -0,0 +1,3 @@
1
+ # Examples for ROM::CSV
2
+
3
+ This folder contains sample usage of ROM::CSV.
@@ -0,0 +1,30 @@
1
+ require 'bundler'
2
+ Bundler.setup
3
+
4
+ require 'rom/csv'
5
+ require 'ostruct'
6
+
7
+ csv_file = ARGV[0] || File.expand_path("./users.csv", File.dirname(__FILE__))
8
+
9
+ setup = ROM.setup(:csv, csv_file)
10
+ setup.relation(:users) do
11
+ def by_name(name)
12
+ restrict(name: name)
13
+ end
14
+ end
15
+
16
+ class User < OpenStruct
17
+ end
18
+
19
+ setup.mappers do
20
+ define(:users) do
21
+ register_as :entity
22
+ model User
23
+ end
24
+ end
25
+
26
+ rom = setup.finalize
27
+ user = rom.relation(:users).as(:entity).by_name('Jane').one
28
+ # => #<User id=2, name="Jane", email="jane@doe.org">
29
+
30
+ user or abort "user not found"
@@ -0,0 +1,3 @@
1
+ id,name,email
2
+ 1,Julie,julie.andrews@example.com
3
+ 2,Jane,jane@doe.org
@@ -0,0 +1 @@
1
+ require 'rom/csv'
@@ -0,0 +1,8 @@
1
+ require 'csv'
2
+
3
+ require 'rom'
4
+ require 'rom/csv/version'
5
+ require 'rom/csv/repository'
6
+ require 'rom/csv/relation'
7
+
8
+ ROM.register_adapter(:csv, ROM::CSV)
@@ -0,0 +1,17 @@
1
+ require 'rom/memory/dataset'
2
+
3
+ module ROM
4
+ module CSV
5
+ # Dataset for CSV
6
+ #
7
+ # @api public
8
+ class Dataset < ROM::Memory::Dataset
9
+ # Convert each CSV::Row to a hash
10
+ #
11
+ # @api public
12
+ def self.row_proc
13
+ -> row { row.to_hash }
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ require 'rom/relation'
2
+
3
+ module ROM
4
+ module CSV
5
+ # Relation subclass of CSV adapter
6
+ #
7
+ # @example
8
+ # class Users < ROM::Relation[:csv]
9
+ # end
10
+ #
11
+ # @api public
12
+ class Relation < ROM::Relation
13
+ forward :join, :project, :restrict, :order
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,97 @@
1
+ require 'rom/repository'
2
+ require 'rom/csv/dataset'
3
+
4
+ # Ruby Object Mapper
5
+ #
6
+ # @see http://rom-rb.org/
7
+ module ROM
8
+ # CSV support for ROM
9
+ #
10
+ # @example
11
+ # require 'rom/csv'
12
+ # require 'ostruct'
13
+ #
14
+ # setup = ROM.setup(:csv, "./spec/fixtures/users.csv")
15
+ # setup.relation(:users) do
16
+ # def by_name(name)
17
+ # dataset.find_all { |row| row[:name] == name }
18
+ # end
19
+ # end
20
+ #
21
+ # class User < OpenStruct
22
+ # end
23
+ #
24
+ # setup.mappers do
25
+ # define(:users) do
26
+ # model User
27
+ # end
28
+ # end
29
+ #
30
+ # rom = setup.finalize
31
+ # p rom.read(:users).by_name('Jane').one
32
+ # # => #<User id=2, name="Jane", email="jane@doe.org">
33
+ #
34
+ # **Note: rom-csv is read only at the moment.**
35
+ #
36
+ # @api public
37
+ module CSV
38
+ # CSV repository interface
39
+ #
40
+ # @api public
41
+ class Repository < ROM::Repository
42
+ # Expect a path to a single csv file which will be registered by rom to
43
+ # the given name or :default as the repository.
44
+ #
45
+ # Uses CSV.table which passes the following csv options:
46
+ # * headers: true
47
+ # * converters: numeric
48
+ # * header_converters: :symbol
49
+ #
50
+ # @param path [String] path to csv
51
+ #
52
+ # @api private
53
+ #
54
+ # @see CSV.table
55
+ def initialize(path)
56
+ @datasets = {}
57
+ @connection = ::CSV.table(path).by_row!
58
+ end
59
+
60
+ # Return dataset with the given name
61
+ #
62
+ # @param name [String] dataset name
63
+ # @return [Dataset]
64
+ #
65
+ # @api public
66
+ def [](name)
67
+ datasets[name]
68
+ end
69
+
70
+ # Register a dataset in the repository
71
+ #
72
+ # If dataset already exists it will be returned
73
+ #
74
+ # @param name [String] dataset name
75
+ # @return [Dataset]
76
+ #
77
+ # @api public
78
+ def dataset(name)
79
+ datasets[name] = Dataset.new(connection)
80
+ end
81
+
82
+ # Check if dataset exists
83
+ #
84
+ # @param name [String] dataset name
85
+ #
86
+ # @api public
87
+ def dataset?(name)
88
+ datasets.key?(name)
89
+ end
90
+
91
+ private
92
+
93
+ # @api private
94
+ attr_reader :datasets
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,5 @@
1
+ module ROM
2
+ module CSV
3
+ VERSION = '0.0.1'.freeze
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ desc "Run all samples from examples/ folder"
2
+ task :examples do
3
+ FileList["examples/*.rb"].each do |example|
4
+ sh "ruby #{example}"
5
+ end
6
+ end
@@ -0,0 +1,18 @@
1
+ begin
2
+ require "rubocop/rake_task"
3
+
4
+ Rake::Task[:default].enhance [:rubocop]
5
+
6
+ RuboCop::RakeTask.new do |task|
7
+ task.options << "--display-cop-names"
8
+ end
9
+
10
+ namespace :rubocop do
11
+ desc 'Generate a configuration file acting as a TODO list.'
12
+ task :auto_gen_config do
13
+ exec "bundle exec rubocop --auto-gen-config"
14
+ end
15
+ end
16
+
17
+ rescue LoadError
18
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rom/csv/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'rom-csv'
8
+ spec.version = ROM::CSV::VERSION.dup
9
+ spec.authors = ['Don Morrison']
10
+ spec.email = ['don@elskwid.net']
11
+ spec.summary = 'CSV support for Ruby Object Mapper'
12
+ spec.description = spec.summary
13
+ spec.homepage = 'http://rom-rb.org'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_runtime_dependency 'rom', '~> 0.6.0'
22
+
23
+ spec.add_development_dependency 'anima'
24
+ spec.add_development_dependency 'bundler'
25
+ spec.add_development_dependency 'rake'
26
+ spec.add_development_dependency 'rubocop', '~> 0.28.0'
27
+ end
@@ -0,0 +1,3 @@
1
+ id,name,email
2
+ 1,Julie,julie.andrews@example.com
3
+ 2,Jane,jane@doe.org
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+ require 'anima'
3
+
4
+ describe 'CSV repository' do
5
+ subject(:rom) { setup.finalize }
6
+
7
+ let(:path) { File.expand_path('./spec/fixtures/users.csv') }
8
+
9
+ # If :csv is not passed in the repository is named `:default`
10
+ let(:setup) { ROM.setup(:csv, path) }
11
+
12
+ before do
13
+ setup.relation(:users) do
14
+ def by_name(name)
15
+ restrict(name: name)
16
+ end
17
+ end
18
+
19
+ class User
20
+ # Anima allows creation of an instance with an attribute hash
21
+ include Anima.new(:id, :name, :email)
22
+ end
23
+
24
+ setup.mappers do
25
+ define(:users) do
26
+ model User
27
+ register_as :entity
28
+ end
29
+ end
30
+ end
31
+
32
+ describe 'env#relation' do
33
+ it 'returns mapped object' do
34
+ jane = rom.relation(:users).as(:entity).by_name('Jane').to_a.first
35
+
36
+ expect(jane.id).to eql(2)
37
+ expect(jane.name).to eql('Jane')
38
+ expect(jane.email).to eql('jane@doe.org')
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+
3
+ require 'bundler'
4
+ Bundler.require
5
+
6
+ if RUBY_ENGINE == 'rbx'
7
+ require 'codeclimate-test-reporter'
8
+ CodeClimate::TestReporter.start
9
+ end
10
+
11
+ require 'rom'
12
+ require 'rom-csv'
13
+
14
+ root = Pathname(__FILE__).dirname
15
+
16
+ Dir[root.join('shared/*.rb').to_s].each { |f| require f }
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ require 'rom/lint/spec'
4
+
5
+ describe ROM::CSV::Dataset do
6
+ let(:data) { [{ id: 1 }, { id: 2 }] }
7
+ let(:dataset) { ROM::CSV::Dataset.new(data) }
8
+
9
+ it_behaves_like "a rom enumerable dataset"
10
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ require 'rom/lint/spec'
4
+
5
+ describe ROM::CSV::Repository do
6
+ let(:repository) { ROM::CSV::Repository }
7
+ let(:uri) { File.expand_path('./spec/fixtures/users.csv') }
8
+
9
+ it_behaves_like "a rom repository" do
10
+ let(:identifier) { :csv }
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rom-csv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Don Morrison
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rom
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.6.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: anima
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.28.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.28.0
83
+ description: CSV support for Ruby Object Mapper
84
+ email:
85
+ - don@elskwid.net
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".rubocop.yml"
93
+ - ".rubocop_todo.yml"
94
+ - ".travis.yml"
95
+ - CHANGELOG.md
96
+ - Gemfile
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - examples/README.md
101
+ - examples/find_user.rb
102
+ - examples/users.csv
103
+ - lib/rom-csv.rb
104
+ - lib/rom/csv.rb
105
+ - lib/rom/csv/dataset.rb
106
+ - lib/rom/csv/relation.rb
107
+ - lib/rom/csv/repository.rb
108
+ - lib/rom/csv/version.rb
109
+ - rakelib/examples.rake
110
+ - rakelib/rubocop.rake
111
+ - rom-csv.gemspec
112
+ - spec/fixtures/users.csv
113
+ - spec/integration/repository_spec.rb
114
+ - spec/spec_helper.rb
115
+ - spec/unit/dataset_spec.rb
116
+ - spec/unit/repository_spec.rb
117
+ homepage: http://rom-rb.org
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.4.5
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: CSV support for Ruby Object Mapper
141
+ test_files:
142
+ - spec/fixtures/users.csv
143
+ - spec/integration/repository_spec.rb
144
+ - spec/spec_helper.rb
145
+ - spec/unit/dataset_spec.rb
146
+ - spec/unit/repository_spec.rb