csv-machine 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ module CSVMachine
2
+
3
+ class Field
4
+
5
+ attr_reader :name
6
+ attr_reader :options
7
+
8
+ VALID_OPTIONS = [ :column, :type ] # TODO: :convert
9
+
10
+ def initialize(name, options={})
11
+ @name, @options = name.to_sym, options
12
+ end
13
+
14
+ def column
15
+ @options[:column]
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,3 @@
1
+ module CSVMachine
2
+ Version = VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,65 @@
1
+ require 'csv'
2
+ require 'csv-machine/field'
3
+
4
+ module CSVMachine
5
+
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ # TODO: def self.map(klass, &block)
11
+ # end
12
+
13
+ module ClassMethods
14
+ attr_reader :csv_fields
15
+ attr_reader :csv_options
16
+
17
+ def csv_fields
18
+ @csv_fields ||= {}
19
+ end
20
+
21
+ def csv_options
22
+ @csv_options ||= CSV::DEFAULT_OPTIONS
23
+ end
24
+
25
+ # @param [Symbol] name
26
+ # @param [] value
27
+ def set_csv_option(name, value)
28
+ raise "Unknown csv option `#{name}'" unless csv_options.key?(name)
29
+ end
30
+
31
+ # @param [Symbol, #to_sym] name
32
+ # @param [Hash] options
33
+ def field(name, options={})
34
+ #options.clone do |hash|
35
+ #hash[(key.to_sym rescue key) || key] = options.delete(key)
36
+ #end
37
+
38
+ options[:column] ||= begin
39
+ csv_fields.empty? ? 0 : ( csv_fields.values.last.options[:column] + 1 )
40
+ end
41
+
42
+ _field = Field.new(name, options)
43
+ create_attribute(_field.name)
44
+ csv_fields[_field.name] = _field
45
+ end
46
+
47
+ # @param [Array] row Parsed csv row
48
+ def from_csv(row)
49
+ new.tap do |obj|
50
+ csv_fields.each { |n, f| obj.__send__(:"#{n}=", row[f.column]) }
51
+ end
52
+ end
53
+
54
+ def create_attribute(name)
55
+ attr_reader name unless method_defined?(name)
56
+ attr_writer name unless method_defined?(:"#{name}=")
57
+ end
58
+
59
+ def parse(data)
60
+ CSV.new(data, csv_options).collect { |row| from_csv(row) }
61
+ end
62
+
63
+ end
64
+
65
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csv-machine
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Daniel Johnston
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-10 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A machine for csv parsing
15
+ email: dan@dj-agiledev.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/csv-machine/field.rb
21
+ - lib/csv-machine/version.rb
22
+ - lib/csv-machine.rb
23
+ homepage: https://github.com/drfeelngood/csv-machine
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.25
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: Small library used to transform csv data to Ruby objects
47
+ test_files: []