separated_values 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bf22f4626ef0b7c1a99f0e1342ea118deca30cf3
4
+ data.tar.gz: 00e0bc67b4dddcd8498ab799f6f4cea54860e610
5
+ SHA512:
6
+ metadata.gz: f6704f9b8a63421586c40c9f8bb8252dc182e57e8078b03589f5217e9c364872388ba1b57c1ad4ff2fc8ac3ce086ca98486003cb80c0497cbf24bb60395094ae
7
+ data.tar.gz: 111fdcbded392511da6ad88f459a5fd5347191ee05a2de360d53c694347e84a3938608ee792e5f77e5d1ecc4b78c48ba13a50b189b4c258561ce3a25c313c912
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in separated_values.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ separated_values (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.7.0)
12
+ rspec-core (~> 3.7.0)
13
+ rspec-expectations (~> 3.7.0)
14
+ rspec-mocks (~> 3.7.0)
15
+ rspec-core (3.7.1)
16
+ rspec-support (~> 3.7.0)
17
+ rspec-expectations (3.7.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.7.0)
20
+ rspec-mocks (3.7.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.7.0)
23
+ rspec-support (3.7.1)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.16)
30
+ rake (~> 10.0)
31
+ rspec (~> 3.0)
32
+ separated_values!
33
+
34
+ BUNDLED WITH
35
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Emric
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # SeparatedValues
2
+
3
+ This is a gem for easily parsing primitive data sheet files.
4
+ It supports file types like CSV and TSV to name a few.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'separated_values'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install separated_values
21
+
22
+ ## Usage
23
+
24
+ Parsing a file:
25
+ ```ruby
26
+ document = SeparatedValues.parse('my_file.csv')
27
+ ```
28
+
29
+ As you might guess the `document` file contains the contents
30
+ of the file. You can access the cell values as a two dimensional array.
31
+ ```ruby
32
+ document[0][0] = 'foo'
33
+ ```
34
+
35
+ The parser will default to parseCSV files.
36
+ If you want to parse a custom format you can use the `:separator` option.
37
+ ```ruby
38
+ document = SeparatedValues.parse('my_file.tsv', separator: '\t')
39
+ ```
40
+
41
+ The parser also comes with the handy `:schema` option.
42
+ By providing this option you can convert the document to an array of hashes:
43
+ ```ruby
44
+ document = SeparatedValues.parse('my_file.csv', schema: [:first_name, :last_name])
45
+ document.to_hash_array = [
46
+ { first_name: 'Luke', last_name: 'Skywalker' },
47
+ { first_name: 'Han', last_name: 'Solo' }
48
+ ]
49
+ ```
50
+
51
+ ## Development
52
+
53
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
54
+
55
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
56
+
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/istanful/separated_values.
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "separated_values"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,20 @@
1
+ module SeparatedValues
2
+ class Document
3
+ attr_reader :rows, :options
4
+
5
+ def initialize(rows, options = DEFAULT_OPTIONS)
6
+ @rows = rows
7
+ @options = options
8
+ end
9
+
10
+ def [](index)
11
+ rows[index]
12
+ end
13
+
14
+ def to_hash_array
15
+ rows.map do |row|
16
+ row.to_h(options[:schema])
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,25 @@
1
+ require 'separated_values/document'
2
+ require 'separated_values/row_parser'
3
+
4
+ module SeparatedValues
5
+ class Parser
6
+ attr_reader :file_path, :options
7
+
8
+ def initialize(file_path, options = DEFAULT_OPTIONS)
9
+ @file_path = file_path
10
+ @options = options
11
+ end
12
+
13
+ def parse
14
+ Document.new(rows, options)
15
+ end
16
+
17
+ private
18
+
19
+ def rows
20
+ File.readlines(file_path).map do |line|
21
+ RowParser.new(line, options).parse
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ module SeparatedValues
2
+ class Row
3
+ attr_reader :options
4
+
5
+ def initialize(values, options = DEFAULT_OPTIONS)
6
+ @values = values
7
+ @options = options
8
+ end
9
+
10
+ def [](index)
11
+ @values[index]
12
+ end
13
+
14
+ def to_h(schema = options[:schema])
15
+ schema.each_with_object({}).with_index do |(key, hash), i|
16
+ hash[key] = @values[i]
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,22 @@
1
+ require 'separated_values/row'
2
+
3
+ module SeparatedValues
4
+ class RowParser
5
+ attr_reader :source_data, :options
6
+
7
+ def initialize(source_data, options = DEFAULT_OPTIONS)
8
+ @source_data = source_data
9
+ @options = options
10
+ end
11
+
12
+ def parse
13
+ Row.new(source_data_to_array)
14
+ end
15
+
16
+ private
17
+
18
+ def source_data_to_array
19
+ source_data.split(options[:separator])
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module SeparatedValues
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,10 @@
1
+ require 'separated_values/version'
2
+ require 'separated_values/parser'
3
+
4
+ module SeparatedValues
5
+ DEFAULT_OPTIONS = { separator: ';' }
6
+
7
+ def self.parse(file_path, options = DEFAULT_OPTIONS)
8
+ Parser.new(file_path, options).parse
9
+ end
10
+ end
@@ -0,0 +1,32 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "separated_values/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "separated_values"
8
+ spec.version = SeparatedValues::VERSION
9
+ spec.authors = ["Emric"]
10
+ spec.email = ["w.e.w@live.se"]
11
+
12
+ spec.summary = 'Gem for parsing separated values documents'
13
+ spec.homepage = 'https://www.github.com/istanful/separated-values'
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = 'https://www.rubygems.org'
20
+ end
21
+
22
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
23
+ f.match(%r{^(test|spec|features)/})
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.16"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rspec", "~> 3.0"
32
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: separated_values
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Emric
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - w.e.w@live.se
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/separated_values.rb
73
+ - lib/separated_values/document.rb
74
+ - lib/separated_values/parser.rb
75
+ - lib/separated_values/row.rb
76
+ - lib/separated_values/row_parser.rb
77
+ - lib/separated_values/version.rb
78
+ - separated_values.gemspec
79
+ homepage: https://www.github.com/istanful/separated-values
80
+ licenses:
81
+ - MIT
82
+ metadata:
83
+ allowed_push_host: https://www.rubygems.org
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.6.8
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Gem for parsing separated values documents
104
+ test_files: []