csv_reader 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in csv_reader.gemspec
4
+ gem 'rspec'
5
+
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Deepali Kherudkar
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.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # CsvReader
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'csv_reader'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install csv_reader
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/csv_reader/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Deepali Kherudkar"]
6
+ gem.email = ["deepali@bangthetable.com"]
7
+ gem.description = %q{CSV Reader}
8
+ gem.summary = %q{CSV Reader}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "csv_reader"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = CsvReader::VERSION
17
+ end
data/lib/csv_reader.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "csv_reader/version"
2
+ require "csv_reader/reader"
3
+ require "csv_reader/parser"
4
+ require "csv_reader/finder"
5
+ require "csv_reader/base"
6
+
7
+ module CsvReader
8
+ end
@@ -0,0 +1,28 @@
1
+ module CsvReader
2
+ class Base
3
+ def initialize(csv_file)
4
+ csv_data = CsvReader::Reader.new(csv_file).read
5
+ @parser = CsvReader::Parser.new(csv_data)
6
+ end
7
+
8
+ def method_missing(*args)
9
+ method_name = args[0]
10
+ value_to_find = args[1]
11
+
12
+ arr = method_name.to_s.split('find_by_')
13
+ if arr.first == ""
14
+ field_name = arr[1]
15
+
16
+ if @parser.headers.include?(field_name)
17
+ CsvReader::Finder.new(@parser, field_name, value_to_find).find
18
+ else
19
+ raise NoMethodError
20
+ end
21
+
22
+ else
23
+ raise NoMethodError
24
+ end
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,17 @@
1
+ module CsvReader
2
+ class Finder
3
+ def initialize(parser, field_name, value)
4
+ @parser = parser
5
+ @value_to_find = value
6
+ @field_name = field_name
7
+ end
8
+
9
+ def find
10
+ index = @parser.headers.index(@field_name)
11
+
12
+ @parser.data_rows.each do |row|
13
+ return row if row[index] == @value_to_find
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ module CsvReader
2
+ class Parser
3
+ def initialize(csv_data)
4
+ @csv_data = csv_data
5
+ parse
6
+ end
7
+
8
+ def parse
9
+ @rows = @csv_data.split("\n").reject{|x| x.split == []}
10
+ end
11
+
12
+ def rows
13
+ @rows
14
+ end
15
+
16
+ def headers
17
+ @rows[0].split(",").map{|x| x.split.first}
18
+ end
19
+
20
+ def data_rows
21
+ rows = []
22
+ 1.upto(@rows.length-1).each do |i|
23
+ rows << @rows[i].split(",").map{|x| x.split.first}
24
+ end
25
+ rows
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,11 @@
1
+ module CsvReader
2
+ class Reader
3
+ def initialize(file)
4
+ @file = File.open(file)
5
+ end
6
+
7
+ def read
8
+ @file.read
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module CsvReader
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,37 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe CsvReader::Base do
4
+ before :all do
5
+ @csv_file = File.join(Dir.pwd, "spec/data/sample.csv")
6
+ @dipu_row = ["Deepali","dipu","deepali@bangthetable.com","30","F"]
7
+ end
8
+
9
+ context "Respond to methods" do
10
+
11
+ it "should not respond to invalid methods" do
12
+ lambda{
13
+ CsvReader::Base.new(@csv_file).abc
14
+ }.should raise_exception NoMethodError
15
+ end
16
+
17
+ it "should respond to find_by_<valid field name> methods" do
18
+ lambda{
19
+ CsvReader::Base.new(@csv_file).find_by_name
20
+ }.should_not raise_exception NoMethodError
21
+ end
22
+
23
+ it "should not respond to find_by_<invalid field name> methods" do
24
+ lambda{
25
+ CsvReader::Base.new(@csv_file).find_by_abc
26
+ }.should raise_exception NoMethodError
27
+ end
28
+
29
+ end
30
+
31
+ context "find" do
32
+ it "should find the correct row" do
33
+ CsvReader::Base.new(@csv_file).find_by_login("dipu").should eq @dipu_row
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,19 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe CsvReader::Finder do
4
+ before :all do
5
+ @csv_data = %{
6
+ name, login, email, age, sex
7
+ Unni, unnitallman, unni@bangthetable.com, 28, M
8
+ Deepali, dipu, deepali@bangthetable.com, 30, F
9
+ }
10
+
11
+ @dipu_row = ["Deepali","dipu","deepali@bangthetable.com","30","F"]
12
+
13
+ @parser = CsvReader::Parser.new(@csv_data)
14
+ end
15
+
16
+ it "should find correct rows" do
17
+ CsvReader::Finder.new(@parser, "login", "dipu").find.should eq @dipu_row
18
+ end
19
+ end
@@ -0,0 +1,33 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe CsvReader::Parser do
4
+ before :all do
5
+ @csv_data = %{
6
+ name, login, email, age, sex
7
+ Unni, unnitallman, unni@bangthetable.com, 28, M
8
+ Deepali, dipu, deepali@bangthetable.com, 30, F
9
+ }
10
+ end
11
+
12
+ context "rows" do
13
+ it "should return each row as an array" do
14
+ CsvReader::Parser.new(@csv_data).rows.size.should eq 3
15
+ end
16
+ end
17
+
18
+ context "headers" do
19
+ it "should return the correct header row" do
20
+ CsvReader::Parser.new(@csv_data).headers.should eq ["name","login","email","age","sex"]
21
+ end
22
+ end
23
+
24
+ context "data rows" do
25
+ it "should return correct number of data rows" do
26
+ CsvReader::Parser.new(@csv_data).data_rows.size.should eq 2
27
+ end
28
+
29
+ it "should return correct data" do
30
+ CsvReader::Parser.new(@csv_data).data_rows[1].should eq ["Deepali","dipu","deepali@bangthetable.com","30","F"]
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,8 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe CsvReader::Reader do
4
+ it "should read the csv file" do
5
+ csv_file = File.join(Dir.pwd, "spec/data/sample.csv")
6
+ CsvReader::Reader.new(csv_file).read.should include("deepali@bangthetable.com")
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ name, login, email, age, sex
2
+ Unni, unnitallman, unni@bangthetable.com, 28, M
3
+ Deepali, dipu, deepali@bangthetable.com, 30, F
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require_relative "../lib/csv_reader"
5
+
6
+
7
+ RSpec.configure do |config|
8
+ # some (optional) config here
9
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csv_reader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Deepali Kherudkar
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: CSV Reader
15
+ email:
16
+ - deepali@bangthetable.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .rspec
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - csv_reader.gemspec
28
+ - lib/csv_reader.rb
29
+ - lib/csv_reader/base.rb
30
+ - lib/csv_reader/finder.rb
31
+ - lib/csv_reader/parser.rb
32
+ - lib/csv_reader/reader.rb
33
+ - lib/csv_reader/version.rb
34
+ - spec/csv_reader/base_spec.rb
35
+ - spec/csv_reader/finder_spec.rb
36
+ - spec/csv_reader/parser_spec.rb
37
+ - spec/csv_reader/reader_spec.rb
38
+ - spec/data/sample.csv
39
+ - spec/spec_helper.rb
40
+ homepage: ''
41
+ licenses: []
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 1.8.24
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: CSV Reader
64
+ test_files:
65
+ - spec/csv_reader/base_spec.rb
66
+ - spec/csv_reader/finder_spec.rb
67
+ - spec/csv_reader/parser_spec.rb
68
+ - spec/csv_reader/reader_spec.rb
69
+ - spec/data/sample.csv
70
+ - spec/spec_helper.rb