flex_data 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.idea/flex_data.iml +50 -0
- data/.idea/inspectionProfiles/Project_Default.xml +8 -0
- data/.idea/inspectionProfiles/profiles_settings.xml +7 -0
- data/.idea/misc.xml +14 -0
- data/.idea/modules.xml +8 -0
- data/.idea/vcs.xml +6 -0
- data/.idea/workspace.xml +790 -0
- data/Gemfile +7 -0
- data/README.md +36 -0
- data/Rakefile +2 -0
- data/bin/console +12 -0
- data/bin/setup +8 -0
- data/flex_data.gemspec +36 -0
- data/lib/flex_data.rb +12 -0
- data/lib/flex_data/lines.rb +21 -0
- data/lib/flex_data/pdf.rb +22 -0
- data/lib/flex_data/spreadsheet.rb +130 -0
- data/lib/flex_data/spreadsheet/open_struct.rb +34 -0
- data/lib/flex_data/version.rb +3 -0
- metadata +119 -0
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# FlexData
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/flex_data`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'flex_data'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install flex_data
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/flex_data.
|
36
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "flex_data"
|
5
|
+
|
6
|
+
path = '/Users/YCL/Dropbox/Jero/Invoice/8219995-topexec.pdf'
|
7
|
+
test_invoice = PDF::Reader.new(path)
|
8
|
+
text = test_invoice.pages[0].text
|
9
|
+
lines = text.to_lines.reject! { |line| line == '' }
|
10
|
+
|
11
|
+
require "irb"
|
12
|
+
IRB.start
|
data/bin/setup
ADDED
data/flex_data.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'flex_data/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "flex_data"
|
8
|
+
spec.version = FlexData::VERSION
|
9
|
+
spec.authors = ["StochasticSpr"]
|
10
|
+
spec.email = ["ejt.lai@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = 'summary: placeholder'
|
13
|
+
spec.description = 'description: placeholder'
|
14
|
+
spec.homepage = "http://www.hellogoodbye.com"
|
15
|
+
|
16
|
+
=begin
|
17
|
+
Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
=end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
27
|
+
spec.bindir = "exe"
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
32
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
33
|
+
|
34
|
+
spec.add_runtime_dependency 'pdf-reader'
|
35
|
+
spec.add_runtime_dependency 'simple_xlsx_reader', '~> 1.0'
|
36
|
+
end
|
data/lib/flex_data.rb
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
require_relative '../flex_data'
|
2
|
+
|
3
|
+
require_relative 'spreadsheet/open_struct'
|
4
|
+
|
5
|
+
module FlexData
|
6
|
+
|
7
|
+
class Spreadsheet
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
def open (path, sheet = 0)
|
12
|
+
Spreadsheet.new(:open, path: path, sheet: sheet)
|
13
|
+
end
|
14
|
+
|
15
|
+
def load_rows (rows)
|
16
|
+
Spreadsheet.new(:load, rows: rows)
|
17
|
+
end
|
18
|
+
|
19
|
+
def write (rows, path)
|
20
|
+
new_source = self.load_rows(rows)
|
21
|
+
new_source.write(path)
|
22
|
+
new_source
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
attr_accessor :blank, :path
|
28
|
+
|
29
|
+
def initialize (mode, args)
|
30
|
+
@path = args[:path]
|
31
|
+
|
32
|
+
if mode == :open
|
33
|
+
sheet = args[:sheet]
|
34
|
+
ext = @path.split('.').last
|
35
|
+
|
36
|
+
case ext
|
37
|
+
when 'xlsx'
|
38
|
+
open_xlsx(@path, sheet)
|
39
|
+
when 'csv'
|
40
|
+
open_csv(@path)
|
41
|
+
else
|
42
|
+
raise ArgumentError 'file extension not recognized'
|
43
|
+
end
|
44
|
+
|
45
|
+
elsif mode == :load
|
46
|
+
|
47
|
+
rows = args[:rows]
|
48
|
+
load_rows(rows)
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
@blank = []
|
53
|
+
end
|
54
|
+
|
55
|
+
def method_missing (method, *args, &block)
|
56
|
+
@sheet.send(method, *args, &block)
|
57
|
+
end
|
58
|
+
|
59
|
+
def open_xlsx (path = @path, sheet = 0)
|
60
|
+
doc = SimpleXlsxReader.open(path)
|
61
|
+
@sheet = doc.sheets[sheet].rows
|
62
|
+
end
|
63
|
+
|
64
|
+
def open_csv (path = @path)
|
65
|
+
@sheet = CSV.read(path)
|
66
|
+
end
|
67
|
+
|
68
|
+
def load_rows (rows)
|
69
|
+
@sheet = rows
|
70
|
+
end
|
71
|
+
|
72
|
+
def column (num)
|
73
|
+
@sheet.data.inject([]) { |acc, row|
|
74
|
+
acc << row[num]
|
75
|
+
}
|
76
|
+
end
|
77
|
+
|
78
|
+
def aggregate (col1, col2, operator)
|
79
|
+
@sheet.data.inject([]) { |acc, row|
|
80
|
+
if row[col1].nil? || row[col2].nil?
|
81
|
+
acc
|
82
|
+
else
|
83
|
+
acc << eval("row[col1] #{operator.to_s} row[col2]")
|
84
|
+
end
|
85
|
+
}
|
86
|
+
end
|
87
|
+
|
88
|
+
def map_column (col1, &block)
|
89
|
+
original = column(col)
|
90
|
+
original.collect { |cell| block.call(cell) }
|
91
|
+
end
|
92
|
+
|
93
|
+
def map_column! (col1, col2, &block)
|
94
|
+
new = map_column(col1, &block)
|
95
|
+
new.each_with_index { |cell, index|
|
96
|
+
@blank[index] = [] if @blank[index].nil?
|
97
|
+
@blank[index][col2] = cell
|
98
|
+
}
|
99
|
+
@blank
|
100
|
+
end
|
101
|
+
|
102
|
+
def compact! (verbose = true)
|
103
|
+
init_count = @sheet.size
|
104
|
+
@sheet.reject! { |row|
|
105
|
+
row.compact.empty?
|
106
|
+
}
|
107
|
+
puts "#{init_count - @sheet.size} empty rows rejected" if verbose
|
108
|
+
self
|
109
|
+
end
|
110
|
+
|
111
|
+
def to_s
|
112
|
+
@sheet.inject('') { |acc, row|
|
113
|
+
acc << "#{row.inspect}\n"
|
114
|
+
}
|
115
|
+
end
|
116
|
+
|
117
|
+
def write (path = @path, mode = :sheet)
|
118
|
+
CSV.open(path, 'w') do |csv|
|
119
|
+
case mode
|
120
|
+
when :sheet
|
121
|
+
@sheet.each { |row| csv << row }
|
122
|
+
when :new
|
123
|
+
@blank.each { |row| csv << row }
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative 'flex_data'
|
2
|
+
|
3
|
+
module FlexData
|
4
|
+
|
5
|
+
class Spreadsheet
|
6
|
+
|
7
|
+
def map_to_h (first = 1, last = -1)
|
8
|
+
(first..last).inject([]) { |acc, index|
|
9
|
+
acc << row_to_h(index)
|
10
|
+
}
|
11
|
+
end
|
12
|
+
|
13
|
+
def map_to_ostruct (first = 1, last = -1)
|
14
|
+
(first..last).inject([]) { |acc, index|
|
15
|
+
acc << row_to_ostruct(index)
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
def row_to_h (index)
|
20
|
+
keys = @sheet[0]
|
21
|
+
output = {}
|
22
|
+
@sheet[index].each_with_index { |value, i|
|
23
|
+
output.store(keys[i], value)
|
24
|
+
}
|
25
|
+
output
|
26
|
+
end
|
27
|
+
|
28
|
+
def row_to_ostruct (index)
|
29
|
+
OpenStruct.new(row_to_h(index))
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: flex_data
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- StochasticSpr
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-13 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.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
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: pdf-reader
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simple_xlsx_reader
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.0'
|
69
|
+
description: 'description: placeholder'
|
70
|
+
email:
|
71
|
+
- ejt.lai@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".idea/flex_data.iml"
|
78
|
+
- ".idea/inspectionProfiles/Project_Default.xml"
|
79
|
+
- ".idea/inspectionProfiles/profiles_settings.xml"
|
80
|
+
- ".idea/misc.xml"
|
81
|
+
- ".idea/modules.xml"
|
82
|
+
- ".idea/vcs.xml"
|
83
|
+
- ".idea/workspace.xml"
|
84
|
+
- Gemfile
|
85
|
+
- README.md
|
86
|
+
- Rakefile
|
87
|
+
- bin/console
|
88
|
+
- bin/setup
|
89
|
+
- flex_data.gemspec
|
90
|
+
- lib/flex_data.rb
|
91
|
+
- lib/flex_data/lines.rb
|
92
|
+
- lib/flex_data/pdf.rb
|
93
|
+
- lib/flex_data/spreadsheet.rb
|
94
|
+
- lib/flex_data/spreadsheet/open_struct.rb
|
95
|
+
- lib/flex_data/version.rb
|
96
|
+
homepage: http://www.hellogoodbye.com
|
97
|
+
licenses: []
|
98
|
+
metadata: {}
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 2.6.2
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: 'summary: placeholder'
|
119
|
+
test_files: []
|