oocsv 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/oocsv.rb +75 -0
  3. metadata +50 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 87d29477d4f42f5f18b829c017f5d74b7f30f368
4
+ data.tar.gz: 169982cc8d0b3dce9918f661ecd189298ffd6e30
5
+ SHA512:
6
+ metadata.gz: f25fde4fb410d046c1d43bf881719ed3898fa48a783de6273f2e3dfc4c96f5cd3c196120b12f37cb6882b608ef70411546754ca45d7dafd237664bf395d3728d
7
+ data.tar.gz: d259518cc682ff64968ef19e9be16283361f00eea09e3889ec6b88f025872f21dc2cc7e2cf9db8ac0c0e2accdb67d9871fc42bd9a0e3134ebc52fb0b0048a7c6
data/lib/oocsv.rb ADDED
@@ -0,0 +1,75 @@
1
+ module OOCSV
2
+ module_function
3
+
4
+ # A struct representing a single line in a CSV file.
5
+ CSVEntry = Struct.new('CSVEntry') do
6
+ # Creates a new CSVEntry from a hash, rather than specific symbol => value sets.
7
+ # @param opts [Hash<Symbol, Any>] The options hash. Key is the instance variable name (header in CSV), value is
8
+ # this entry's value.
9
+ def initialize(opts = {})
10
+ opts.each do |k, v|
11
+ self.class.send(:attr_accessor, k)
12
+ instance_variable_set("@#{k}", v)
13
+ end
14
+
15
+ # noinspection RubyInstanceVariableNamingConvention
16
+ @i_expect_that_nobody_will_use_this_name = opts
17
+ end
18
+
19
+ # Returns a string representation of the CSV. This is not the same as {OOCSV#write}.
20
+ def to_s
21
+ values = {}
22
+ @i_expect_that_nobody_will_use_this_name.keys.each do |key|
23
+ val = instance_variable_get("@#{key}")
24
+ values[key] = val
25
+ end
26
+ str = '#<struct Struct::CSVEntry'
27
+ values.each do |k, v|
28
+ str << " @#{k}=#{v}"
29
+ end
30
+ str << '>'
31
+ end
32
+ end
33
+
34
+ # Read a CSV string into an array of CSVEntries.
35
+ # @param string [String] The CSV.
36
+ # @return [Array<Struct::CSVEntry>] An array of CSVEntries representing the CSV provided.
37
+ def read(string)
38
+ lines = string.split("\n")
39
+ header = lines[0]
40
+ attributes = header.split(',').map! { |v| v.to_sym }
41
+ # Struct.new('CSVEntry', *attributes)
42
+ ret = []
43
+ lines.drop(1).each do |line|
44
+ values = line.split(',')
45
+ opts = {}
46
+ values.each_with_index do |val, idx|
47
+ opts[attributes[idx]] = val
48
+ end
49
+ ret << Struct::CSVEntry.new(opts)
50
+ end
51
+
52
+ ret
53
+ end
54
+
55
+ # Turns an array of CSVEntries (see #read) into a String.
56
+ # @param objects [Array<Struct::CSVEntry>] The array of structs.
57
+ # @return [String] The CSV representing the array given.
58
+ def write(objects = [])
59
+ return '' if objects.empty?
60
+ str = ''
61
+ vars = objects[0].instance_variables.select! { |v| v != :@i_expect_that_nobody_will_use_this_name }.map { |v| v }
62
+ str << vars.map { |i| i[1..-1] }.join(',')
63
+ str << "\n"
64
+ objects.each_with_index do |obj, obj_indx|
65
+ vars.each_with_index do |var, var_indx|
66
+ val = obj.instance_variable_get(var)
67
+ str << val
68
+ str << ',' if var_indx != vars.size - 1
69
+ end
70
+ str << "\n" if obj_indx != objects.size - 1
71
+ end
72
+
73
+ str
74
+ end
75
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oocsv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eli Foster
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'A very dynamic object-oriented approach to CSV reading and writing.
14
+ Unlike the stdlib csv library, this does not handle files. Use the various I/O libraries
15
+ for that. This gem only handles reading and writing CSV strings. It adds a new struct,
16
+ CSVEntry, that dynamically has its instance attribute accessor methods created and
17
+ initialized. '
18
+ email: elifosterwy@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - lib/oocsv.rb
24
+ homepage: https://github.com/elifoster/oocsv-rb
25
+ licenses:
26
+ - MIT
27
+ metadata:
28
+ issue_tracker: https://github.com/elifoster/oocsv-rb/issues
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.6.4
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: A very dynamic object-oriented approach to CSV reading and writing.
49
+ test_files: []
50
+ has_rdoc: