rb-gust 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NDI4MTRmNTY1NDgxN2M5MTBkYzRlMThkMjIwMDE5OTg3YmM0ZDVkNw==
5
+ data.tar.gz: !binary |-
6
+ ZGEzMjhkNWNhMWRjYzE5NWIxOTVkMTQ3MTlhMmY5Yzc1NzcyODRkYw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ OWFhMTQwZjY4OGE4NTQ1Zjk0N2MwZDA0MTVlMjllZjkxNzU1MjRmZTBhMjk4
10
+ MTc2OWU3ZjIyZDU2N2VmYTI3NWVlM2M4YjZkNzk3MTg5Y2Y3MWI5ZjMyY2Ew
11
+ MDc3Y2NmMTE3NTcyZTllZWVhMDU2ZDUyNmE1MGVjMDI1MTY3MmE=
12
+ data.tar.gz: !binary |-
13
+ NzMxMzI3ZjU3YjQ2M2U1ODg3M2Y1YmI0NGVlNWMwODg0YTU3NDljZTAxYzUx
14
+ YzMyY2E0NDM1MmU2OGM0NzM0MzVlMDc4NWJhMTQ2MDY4MWY5OWNkMDllNzFi
15
+ ZmQ5MjQwN2MxODY2NGNiMThmZTVkNjYyMzBmMTViZjY1ZGRlNjI=
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/FORECAST.md ADDED
@@ -0,0 +1,44 @@
1
+ Gust Forecast
2
+ =============
3
+
4
+ The Gust Forecast is a list of feature ideas that would be nice to have. Ideally, this list is prioritized. (Maybe this list should be moved to Pivotal Tracker)
5
+
6
+ ### Allow for more than one Spreadsheet
7
+ Currently the script overwrites all but the last spreadsheet
8
+
9
+ ### Refactor the Script class to be OO
10
+
11
+ ### Object groups accessible by title name
12
+
13
+ For Example:
14
+
15
+ + basic_objects.vehicles
16
+ + basic_objects.fruits
17
+ + basic_objects.cities
18
+ + basic_object.vehicles.first.color
19
+
20
+ ### Title Row, Header Row, and Object Row are configurable
21
+
22
+ ```ruby
23
+ Gust.load(spreadsheet_filepath, config_hash)
24
+ Gust.config do |gust|
25
+ gust.title_row = 5
26
+ gust.object_row = 6
27
+ end
28
+ ```
29
+
30
+ ### Objects are associable via sql-like foreign key attributes
31
+
32
+ ### Rails Plugin
33
+
34
+ ### Builds/Saves objects into Rails Models
35
+
36
+ ### Compatibility with more filetypes (using roo)
37
+
38
+ ### Formula Handling
39
+
40
+ + Get true/false values
41
+ + Formula specific methods/accessors
42
+
43
+ ### Multiple Tables Vertically stacked in One Spreadsheet
44
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rb-gust.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,40 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rb-gust (0.0.1)
5
+ active_support
6
+ spreadsheet
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ active_support (3.0.0)
12
+ activesupport (= 3.0.0)
13
+ activesupport (3.0.0)
14
+ coderay (1.0.9)
15
+ diff-lcs (1.2.4)
16
+ method_source (0.8.1)
17
+ pry (0.9.12.2)
18
+ coderay (~> 1.0.5)
19
+ method_source (~> 0.8)
20
+ slop (~> 3.4)
21
+ rspec (2.13.0)
22
+ rspec-core (~> 2.13.0)
23
+ rspec-expectations (~> 2.13.0)
24
+ rspec-mocks (~> 2.13.0)
25
+ rspec-core (2.13.1)
26
+ rspec-expectations (2.13.0)
27
+ diff-lcs (>= 1.1.3, < 2.0)
28
+ rspec-mocks (2.13.1)
29
+ ruby-ole (1.2.11.6)
30
+ slop (3.4.5)
31
+ spreadsheet (0.8.5)
32
+ ruby-ole (>= 1.0)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ pry
39
+ rb-gust!
40
+ rspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 jfeaver
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,54 @@
1
+ # Gust
2
+
3
+ Gust allows you to load many objects from an excel spreadsheet (.xls only) into Ruby objects.
4
+
5
+ ```ruby
6
+ objects = Gust.load('/path/to/spreadsheet.xls')
7
+ ```
8
+
9
+ ... and you're off!
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'rb-gust'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install rb-gust
24
+
25
+ ## Usage
26
+
27
+ Format a Spreadsheet (see the example spreadsheet [here](spec/workbooks/basic_objects.xls)):
28
+
29
+ + first row with content as the object titles.
30
+ + second row has column headers
31
+ + third row begins the object data values
32
+
33
+ To load the objects into Ruby, use Gust.load(). An RSpec example:
34
+
35
+ ```ruby
36
+ objects = Gust.load('/gust_gem_path/spec/worksheets/basic_objects.xls')
37
+ objects[:vehicles].should eq(
38
+ [
39
+ {:name=>"sedan", :wheels=>4.0, :color=>"blue"},
40
+ {:name=>"sports car", :wheels=>4.0, :color=>"red"},
41
+ {:name=>"moving truck", :wheels=>6.0, :color=>"white"},
42
+ ]
43
+ )
44
+ ```
45
+
46
+ ## Contributing
47
+
48
+ Check out the [feature forecast](FORECAST.md) for desired features.
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,45 @@
1
+ module Gust
2
+ class Script
3
+ attr_reader :objects, :object_groups
4
+
5
+ require 'active_support/inflector'
6
+ def build_object_groups
7
+ @object_groups = {}
8
+ @titles.each_with_index do |title,i|
9
+ group_name = ActiveSupport::Inflector.tableize(title).to_sym
10
+ @object_groups[group_name] = group_objects(@headers[i], @objects[i])
11
+ end
12
+ end
13
+
14
+ def group_objects headers, object_data
15
+ group = []
16
+ object_data.each do |object_attributes|
17
+ next if object_attributes.compact.empty?
18
+ group << hashify_object(headers, object_attributes)
19
+ end
20
+ group
21
+ end
22
+
23
+ def hashify_object(headers, object_attributes)
24
+ hash = {}
25
+ headers.each_with_index do |header, i|
26
+ hash[header.to_sym] = object_attributes[i]
27
+ end
28
+ hash
29
+ end
30
+
31
+ def script
32
+ _wb = ::Spreadsheet.open(Gust.workbook_filepath)
33
+
34
+ _wb.worksheets.each do |_ws|
35
+ @ws = Gust::Spreadsheet::Worksheet.new(_ws)
36
+ @titles = @ws.titles
37
+ @headers = @ws.headers
38
+ @objects = @ws.objects
39
+ build_object_groups
40
+ end
41
+
42
+ @object_groups
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,50 @@
1
+ module Gust::Spreadsheet
2
+ class Structure
3
+ attr_reader :header_row, :object_regions, :inside
4
+
5
+ def initialize header_row
6
+ @header_row = header_row
7
+ @object_regions = []
8
+ determine_object_structure
9
+ end
10
+
11
+ private
12
+
13
+ def determine_object_structure
14
+ @inside = false
15
+ start_index = 0
16
+ header_row.each_with_index do |cell, i|
17
+ if single_column_object?(i)
18
+ @object_regions << [i,i]
19
+ elsif first_column?(cell)
20
+ start_index = i
21
+ @inside = true
22
+ elsif last_column?(i)
23
+ @object_regions << [start_index, i]
24
+ @inside = false
25
+ end
26
+ end
27
+ end
28
+
29
+ def single_column_object? i
30
+ not(inside) && last_object_heading?(i)
31
+ end
32
+
33
+ def first_column?(cell)
34
+ not(inside) && present?(cell)
35
+ end
36
+
37
+ def last_column?(i)
38
+ inside && last_object_heading?(i)
39
+ end
40
+
41
+ def present? cell
42
+ cell && cell.length > 0
43
+ end
44
+
45
+ def last_object_heading? i
46
+ present?(header_row[i]) && not(present?(header_row[i+1]))
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,62 @@
1
+ module Gust::Spreadsheet
2
+ class Worksheet
3
+ attr_reader :worksheet
4
+ attr_reader :title_row_index, :header_row_index, :object_row_index
5
+ attr_reader :object_regions
6
+ attr_reader :titles, :headers, :objects
7
+
8
+ def initialize worksheet
9
+ @worksheet = worksheet
10
+ @title_row_index = @worksheet.dimensions.first
11
+ @header_row_index = @title_row_index + 1
12
+ @object_row_index = @title_row_index + 2
13
+ @object_regions = Gust::Spreadsheet::Structure.new(header_row).object_regions
14
+ @titles = []
15
+ @headers = []
16
+ define_object_titles(title_row)
17
+ define_headers(header_row)
18
+ define_objects(object_rows)
19
+ end
20
+
21
+ def title_row
22
+ @worksheet.row(title_row_index)
23
+ end
24
+
25
+ def header_row
26
+ @worksheet.row(header_row_index)
27
+ end
28
+
29
+ def object_rows
30
+ rows = []
31
+ @worksheet.each(object_row_index) do |row|
32
+ rows << row
33
+ end
34
+ rows
35
+ end
36
+
37
+ def define_object_titles title_row
38
+ object_regions.each do |r|
39
+ @titles << title_row[r.first]
40
+ end
41
+ end
42
+
43
+ def define_headers header_row
44
+ object_regions.each do |r|
45
+ object_headers = header_row[r[0]..r[1]]
46
+ @headers << object_headers
47
+ end
48
+ end
49
+
50
+ def define_objects object_rows
51
+ @objects = []
52
+ object_rows.each do |object_row|
53
+ object_regions.each_with_index do |r,i|
54
+ @objects << []
55
+ object = object_row[r[0]..r[1]]
56
+ @objects[i] << object
57
+ end
58
+ end
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,6 @@
1
+ require 'rb-gust/spreadsheet/worksheet'
2
+ require 'rb-gust/spreadsheet/structure'
3
+
4
+ module Gust::Spreadsheet
5
+ end
6
+
@@ -0,0 +1,3 @@
1
+ module Gust
2
+ VERSION = "0.0.1"
3
+ end
data/lib/rb-gust.rb ADDED
@@ -0,0 +1,19 @@
1
+
2
+ require 'spreadsheet'
3
+
4
+ require 'rb-gust/version'
5
+ require 'rb-gust/script'
6
+ require 'rb-gust/spreadsheet'
7
+
8
+ module Gust
9
+
10
+ def self.workbook_filepath
11
+ @@workbook_filepath
12
+ end
13
+
14
+ def self.load workbook_filepath
15
+ @@workbook_filepath = workbook_filepath
16
+ Script.new.script
17
+ end
18
+
19
+ end
data/rb-gust.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rb-gust/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rb-gust"
8
+ gem.description = 'A tool for loading spreadsheet data into ruby objects'
9
+ gem.summary = 'Load data from a spreadsheets directly into Ruby objects for further manipulation. Useful while bulk uploading data for applications, analytics, or scripting.'
10
+ gem.homepage = 'http://www.github.com/jfeaver/rb-gust/'
11
+
12
+ gem.version = Gust::VERSION
13
+ gem.license = 'MIT'
14
+
15
+ gem.authors = 'Nathan Feaver'
16
+ gem.email = 'nathan@nathanfeaver.com'
17
+
18
+ gem.files = `git ls-files`.split($/)
19
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
20
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
21
+ gem.require_paths = ["lib"]
22
+
23
+ ### DEPENDENCIES
24
+ gem.add_dependency('spreadsheet')
25
+ gem.add_dependency('active_support')
26
+
27
+ # Development Dependencies
28
+ gem.add_development_dependency('rspec')
29
+ gem.add_development_dependency('pry')
30
+ end
data/spec/gust_spec.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gust do
4
+ before do
5
+ @path = File.join(File.dirname(__FILE__), 'workbooks','basic_objects.xls')
6
+ end
7
+
8
+ it "is already loaded" do
9
+ require('rb-gust').should be_false
10
+ end
11
+
12
+ it "should have objects" do
13
+ o = Gust.load(@path)
14
+ o.should be_a(Hash)
15
+ end
16
+
17
+ it "should not contain any empty objects" do
18
+ o = Gust.load(@path)
19
+ o.each do |object_title, object_group|
20
+ object_group.none? do |object|
21
+ object.values.compact.empty?
22
+ end.should be_true
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,28 @@
1
+
2
+ ## Development Dependencies
3
+ require 'pry'
4
+
5
+ ## Load Gust
6
+ require 'rb-gust'
7
+
8
+ ## Sample Application
9
+ #APP_ROOT = File.join(File.dirname(__FILE__), 'app')
10
+
11
+ ## Configure RSpec
12
+ RSpec.configure do |config|
13
+ config.treat_symbols_as_metadata_keys_with_true_values = true
14
+ config.run_all_when_everything_filtered = true
15
+ config.filter_run :focus
16
+
17
+ # Run specs in random order to surface order dependencies. If you find an
18
+ # order dependency and want to debug it, you can fix the order by providing
19
+ # the seed, which is printed after each run.
20
+ # --seed 1234
21
+ config.order = 'random'
22
+ end
23
+
24
+ ## Spec Helpers
25
+ #Dir[File.join(File.dirname(__FILE__), 'helpers', '**', '*_helper.rb')].each do |f|
26
+ # require f
27
+ #end
28
+
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gust::Spreadsheet::Structure do
4
+
5
+ it "knows the object regions" do
6
+ # Gust assumes that you have spacers between separate object headers
7
+ header_row = [nil, 'headerA1', 'headerA2', nil, nil, 'headerB1', 'headerB2', 'headerB3', nil, 'headerC1']
8
+ @structure = Gust::Spreadsheet::Structure.new(header_row)
9
+ @structure.object_regions.should == [[1,2],[5,7],[9,9]]
10
+ end
11
+
12
+ end
13
+
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gust::Spreadsheet::Worksheet do
4
+ before :each do
5
+ path = File.join(File.expand_path('..', File.dirname(__FILE__)), 'workbooks','basic_objects.xls')
6
+ _s = Spreadsheet.open(path)
7
+ _ws = _s.worksheet('basic_objects')
8
+ @ws = Gust::Spreadsheet::Worksheet.new(_ws)
9
+ end
10
+
11
+ it "handles all interactions with the Spreadsheet gem Worksheets" do
12
+ @ws.worksheet.should be_a(::Spreadsheet::Excel::Worksheet)
13
+ end
14
+
15
+ it "knows about the title row" do
16
+ @ws.title_row.should include('Vehicles')
17
+ end
18
+
19
+ it "knows about the header row" do
20
+ @ws.header_row.should include('juicable?')
21
+ end
22
+
23
+ it "knows about the object rows" do
24
+ @ws.object_rows.should have(4).object_rows
25
+ end
26
+
27
+ it "knows its own titles" do
28
+ titles = %w(Vehicles Fruits Cities)
29
+ @ws.titles.should eq(titles)
30
+ end
31
+
32
+ it "knows the object headers" do
33
+ headers = [%w(name wheels color),%w(id type juicable?),%w(name state).push('favorite fruit id')]
34
+ @ws.headers.should eq(headers)
35
+ end
36
+
37
+ end
Binary file
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rb-gust
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Feaver
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: spreadsheet
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: active_support
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A tool for loading spreadsheet data into ruby objects
70
+ email: nathan@nathanfeaver.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - .gitignore
76
+ - .rspec
77
+ - FORECAST.md
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/rb-gust.rb
84
+ - lib/rb-gust/script.rb
85
+ - lib/rb-gust/spreadsheet.rb
86
+ - lib/rb-gust/spreadsheet/structure.rb
87
+ - lib/rb-gust/spreadsheet/worksheet.rb
88
+ - lib/rb-gust/version.rb
89
+ - rb-gust.gemspec
90
+ - spec/gust_spec.rb
91
+ - spec/spec_helper.rb
92
+ - spec/spreadsheet/structure_spec.rb
93
+ - spec/spreadsheet/worksheet_spec.rb
94
+ - spec/workbooks/basic_objects.xls
95
+ homepage: http://www.github.com/jfeaver/rb-gust/
96
+ licenses:
97
+ - MIT
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.0.3
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Load data from a spreadsheets directly into Ruby objects for further manipulation.
119
+ Useful while bulk uploading data for applications, analytics, or scripting.
120
+ test_files:
121
+ - spec/gust_spec.rb
122
+ - spec/spec_helper.rb
123
+ - spec/spreadsheet/structure_spec.rb
124
+ - spec/spreadsheet/worksheet_spec.rb
125
+ - spec/workbooks/basic_objects.xls