orm_from_csv 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gemtest ADDED
File without changes
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .*.sw*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm ruby-1.9.2-p0@orm_from_csv
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in orm_from_csv.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,32 @@
1
+ = ORM From CSV
2
+
3
+ A super simple way (137 characters of code) of instantiating ORM objects from a CSV file.
4
+ Supports any Ruby class that accepts a hash of attributes like ActiveRecord:
5
+
6
+ class ActiveRecord::Base
7
+ def initialize(attributes = {})
8
+ # ...
9
+
10
+ For ActiveRecord-specific usage, see the active_record_csv gem link below.
11
+
12
+ == Usage
13
+
14
+ The following would read in a CSV file and return instantiated objects with attributes from the file:
15
+
16
+ SomeModel.from_csv(csv_file)
17
+
18
+ Note that it does not save the objects since that is dependent on what ORM you use.
19
+
20
+ === Rails/ActiveRecord:
21
+
22
+ ActiveRecord::Base.extend ORMFromCSV
23
+
24
+ === Other ORMs
25
+
26
+ Haven't tested with any others.
27
+ It shouldn't be difficult to integrate with others. The only assumption made is that YourModel.new() should accept a hash of attributes and assign them. See spec/support/models/movie.rb for an example.
28
+
29
+ == Related gems
30
+
31
+ * https://github.com/ordinaryzelig/active_record_csv
32
+ * https://github.com/ordinaryzelig/active_record_to_csv
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ task :default => :test
5
+
6
+ desc 'Run rspec tests'
7
+ task :test do
8
+ sh 'rspec spec/'
9
+ end
@@ -0,0 +1,11 @@
1
+ require 'csv'
2
+
3
+ module ORMFromCSV
4
+
5
+ def from_csv(file)
6
+ CSV.table(file).map do |csv_row|
7
+ new csv_row.to_hash
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,3 @@
1
+ module ORMFromCSV
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "orm_from_csv/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "orm_from_csv"
7
+ s.version = ORMFromCSV::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jared Ning"]
10
+ s.email = ["jared@redningja.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Create instances of ORM objects from CSV}
13
+ s.description = %q{Create instances of ORM (like ActiveRecord or Mongoid, anything that supports Model.new(attributes_as_hash)) objects from CSV}
14
+
15
+ s.rubyforge_project = "orm_from_csv"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency 'rspec', '2.5.0'
23
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe ORMFromCSV do
4
+
5
+ it 'should instantiate new objects by importing a CSV file' do
6
+ Movie.from_csv(csv_file).map(&:attributes).should =~ expected_attributes_from_movies_csv
7
+ end
8
+
9
+ end
@@ -0,0 +1,10 @@
1
+ require 'pathname'
2
+
3
+ require File.join(Pathname(__FILE__).dirname.expand_path, '../lib/orm_from_csv')
4
+
5
+ # require support .rb files.
6
+ Dir[File.expand_path("../support/**/*.rb", __FILE__)].each {|f| require f}
7
+
8
+ RSpec.configure do |config|
9
+ config.include Macros
10
+ end
@@ -0,0 +1,3 @@
1
+ title,director_id
2
+ Black Swan,0
3
+ Inception,1
@@ -0,0 +1,22 @@
1
+ module Macros
2
+
3
+ def content_of(file)
4
+ file.read
5
+ end
6
+
7
+ def fixtures_file(file_name)
8
+ File.open(File.join(Pathname(__FILE__).dirname.expand_path, 'fixtures', file_name))
9
+ end
10
+
11
+ def csv_file
12
+ fixtures_file('movies.csv')
13
+ end
14
+
15
+ def expected_attributes_from_movies_csv
16
+ expected_attributes = [
17
+ {'title' => 'Black Swan', 'director_id' => 0},
18
+ {'title' => 'Inception', 'director_id' => 1},
19
+ ]
20
+ end
21
+
22
+ end
@@ -0,0 +1,18 @@
1
+ class Movie < Struct.new(:title, :director_id)
2
+
3
+ extend ORMFromCSV
4
+
5
+ def initialize(attributes = {})
6
+ attributes.each do |field, value|
7
+ send(:"#{field}=", value)
8
+ end
9
+ end
10
+
11
+ def attributes
12
+ ['title', 'director_id'].inject({}) do |atts, field|
13
+ atts[field] = send(field)
14
+ atts
15
+ end
16
+ end
17
+
18
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: orm_from_csv
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Jared Ning
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-01 00:00:00 -06:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - "="
23
+ - !ruby/object:Gem::Version
24
+ version: 2.5.0
25
+ type: :development
26
+ version_requirements: *id001
27
+ description: Create instances of ORM (like ActiveRecord or Mongoid, anything that supports Model.new(attributes_as_hash)) objects from CSV
28
+ email:
29
+ - jared@redningja.com
30
+ executables: []
31
+
32
+ extensions: []
33
+
34
+ extra_rdoc_files: []
35
+
36
+ files:
37
+ - .gemtest
38
+ - .gitignore
39
+ - .rvmrc
40
+ - Gemfile
41
+ - README.rdoc
42
+ - Rakefile
43
+ - lib/orm_from_csv.rb
44
+ - lib/orm_from_csv/version.rb
45
+ - orm_from_csv.gemspec
46
+ - spec/orm_from_csv_spec.rb
47
+ - spec/spec_helper.rb
48
+ - spec/support/fixtures/movies.csv
49
+ - spec/support/macros.rb
50
+ - spec/support/models/.movie.rb.swp
51
+ - spec/support/models/movie.rb
52
+ has_rdoc: true
53
+ homepage: ""
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project: orm_from_csv
76
+ rubygems_version: 1.5.2
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Create instances of ORM objects from CSV
80
+ test_files:
81
+ - spec/orm_from_csv_spec.rb
82
+ - spec/spec_helper.rb
83
+ - spec/support/fixtures/movies.csv
84
+ - spec/support/macros.rb
85
+ - spec/support/models/.movie.rb.swp
86
+ - spec/support/models/movie.rb