Skip to content
This repository has been archived by the owner on Oct 17, 2023. It is now read-only.

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed May 3, 2016
0 parents commit 4e4ce3b
Show file tree
Hide file tree
Showing 14 changed files with 296 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
@@ -0,0 +1,8 @@
.bundle/
Gemfile.lock
coverage/
doc/
pkg/
spec/reports/
tmp/
test/*.sqlite3.db
10 changes: 10 additions & 0 deletions .travis.yml
@@ -0,0 +1,10 @@
bundler_args: --binstubs
rvm:
- 1.9.0
- 1.9.3
- 2.0.0
- 2.1.0
- 2.2.0
- 2.3.0
notifications:
irc: "irc.freenode.org#axlsx"
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,5 @@
CHANGELOG
---------

- **April.30.2016**: 0.1.0
- Gem Initial Release
8 changes: 8 additions & 0 deletions Gemfile
@@ -0,0 +1,8 @@
source 'https://rubygems.org'
gemspec

gem 'method_source'

group :rails do
gem 'rails', '>= 3.2.0'
end
7 changes: 7 additions & 0 deletions LICENSE
@@ -0,0 +1,7 @@
Copyright (c) 2016 Weston Ganger

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 changes: 9 additions & 0 deletions README.md
@@ -0,0 +1,9 @@
# Spreadsheet Architect
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=VKY8YAWAS5XRQ&lc=CA&item_name=Weston%20Ganger&item_number=rearmed_rb&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHostedGuest" target="_blank" title="Buy Me A Coffee"><img src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif" alt="Buy Me A Coffee"/></a>



# Credits
Created by Weston Ganger - @westonganger

<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=VKY8YAWAS5XRQ&lc=CA&item_name=Weston%20Ganger&item_number=rearmed_rb&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHostedGuest" target="_blank" title="Buy Me A Coffee"><img src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif" alt="Buy Me A Coffee"/></a>
13 changes: 13 additions & 0 deletions Rakefile
@@ -0,0 +1,13 @@
require File.expand_path(File.dirname(__FILE__) + '/lib/spreadsheet_architect/version.rb')
require 'bundler/gem_tasks'

task :test do
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << 'test'
t.test_files = FileList['test/**/tc_*.rb']
t.verbose = true
end
end

task default: :test
59 changes: 59 additions & 0 deletions lib/rearmed.rb
@@ -0,0 +1,59 @@
module Rearmed
def self.recursive_require(path, file_name="**/*.rb")
file_name = File.join(File.dirname(__FILE__), path, file_name)
Dir[file_name].each{|file| require file}
end

def self.require_folder(path, file_name="*.rb")
file_name = File.join(File.dirname(__FILE__), path, file_name)
Dir[file_name].each{|file| require file}
end

def self.naturalize_str(str)
#TODO: BENCHMARK
#str.split(/(\d+)/).map{|a| a =~ /\d+/ ? a.to_i : a}
str.scan(/[^\d\.]+|[\d\.]+/).collect{|f| f.match(/\d+(\.\d+)?/) ? f.to_f : f}
end
end

if defined?(Rails)
# Rails 3
if Rails.version =~ /^3\./
Hash.class_eval do
def compact
self.select{|_, value| !value.nil?}
end

def compact!
self.reject!{|_, value| value.nil?}
end
end

if defined?(ActiveRecord)
ActiveRecord::Persistence::ClassMethods.module_eval do
def update_columns(attributes)
raise ActiveRecordError, "cannot update a new record" if new_record?
raise ActiveRecordError, "cannot update a destroyed record" if destroyed?

attributes.each_key do |key|
raise ActiveRecordError, "#{key.to_s} is marked as readonly" if self.class.readonly_attributes.include?(key.to_s)
end

updated_count = self.class.unscoped.where(self.class.primary_key => id).update_all(attributes)

attributes.each do |k, v|
raw_write_attribute(k, v)
end

updated_count == 1
end
end
end
end
end

Array.module_eval do
def natural_sort_by(&block)
sort_by{|x| Rearmed.naturalize_str(eval(block.source))}
end
end
3 changes: 3 additions & 0 deletions lib/rearmed/version.rb
@@ -0,0 +1,3 @@
module Rearmed
VERSION = "0.1.0"
end
27 changes: 27 additions & 0 deletions rearmed_rb.gemspec
@@ -0,0 +1,27 @@
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'rearmed/version.rb'

Gem::Specification.new do |s|
s.name = 'rearmed_rb'
s.version = Rearmed::VERSION
s.author = "Weston Ganger"
s.email = 'westonganger@gmail.com'
s.homepage = 'https://github.com/westonganger/rearmed_rb'

s.summary = "Ruby method toolbox with new methods for both Ruby and Rails"
s.description = "Ruby method toolbox with new methods for both Ruby and Rails"
s.files = Dir.glob("{lib/**/*}") + %w{ LICENSE README.md Rakefile CHANGELOG.md }
s.test_files = Dir.glob("{test/**/*}")

s.add_runtime_dependency 'method_source'

s.add_development_dependency 'rake'
s.add_development_dependency 'minitest'
s.add_development_dependency 'bundler'
s.add_development_dependency 'sqlite3'
s.add_development_dependency 'activerecord'

s.required_ruby_version = '>= 1.9.3'
s.require_path = 'lib'
end
8 changes: 8 additions & 0 deletions test.rb
@@ -0,0 +1,8 @@
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'rearmed.rb'

require 'method_source'

puts "sort_by: #{["1.1","1.11","1.2","1.22"].sort_by{|x| x}}"
puts "natural_sort_by: #{["1.1","1.11","1.2","1.22"].natural_sort_by{|x| Rearmed.naturalize_str(x)}}"
3 changes: 3 additions & 0 deletions test/database.yml
@@ -0,0 +1,3 @@
sqlite3:
adapter: sqlite3
database: test/spreadsheet_architect.sqlite3.db
52 changes: 52 additions & 0 deletions test/helper.rb
@@ -0,0 +1,52 @@
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
ActiveRecord::Base.establish_connection(config['sqlite3'])
ActiveRecord::Schema.define(version: 0) do
begin
drop_table :posts, :force => true
drop_table :other_posts, :force => true
rescue
#dont really care if the tables are not dropped
end

create_table(:posts, :force => true) do |t|
t.string :name
t.string :title
t.text :content
t.integer :votes
t.timestamps null: false
end

create_table(:other_posts, :force => true) do |t|
t.string :name
t.string :title
t.text :content
t.integer :votes
t.timestamps null: false
end
end

class Post < ActiveRecord::Base
include SpreadsheetArchitect

def spreadsheet_columns
[:name, :title, :content, :votes, :ranking]
end

def ranking
1
end
end

class OtherPost < ActiveRecord::Base
include SpreadsheetArchitect
end

posts = []
posts << Post.new(name: "first post", title: "This is the first post", content: "I am a very good first post!", votes: 1)
posts << Post.new(name: "second post", title: "This is the second post", content: "I am the best post!", votes: 7)
posts.each { |p| p.save! }

posts = []
posts << OtherPost.new(name: "my other first", title: "first other post", content: "the first other post!", votes: 1)
posts << OtherPost.new(name: "my other second", title: "second other post", content: "last other post!", votes: 7)
posts.each { |p| p.save! }
84 changes: 84 additions & 0 deletions test/tc_spreadsheet_architect.rb
@@ -0,0 +1,84 @@
#!/usr/bin/env ruby -w
require "spreadsheet_architect"
require 'yaml'
require 'active_record'
require 'minitest'

require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))

class TestSpreadsheetArchitect < MiniTest::Test
class Post < ActiveRecord::Base
include SpreadsheetArchitect

def self.spreadsheet_columns
[:name, :title, :content, :votes, :ranking]
end

def ranking
1
end
end

class OtherPost < ActiveRecord::Base
include SpreadsheetArchitect
end

class PlainPost
include SpreadsheetArchitect

def self.spreadsheet_columns
[:name, :title, :content]
end

def name
"the name"
end

def title
"the title"
end

def content
"the content"
end
end

test "test_spreadsheet_options" do
assert_equal([:name, :title, :content, :votes, :ranking], Post.spreadsheet_columns)
assert_equal([:name, :title, :content, :votes, :created_at, :updated_at], OtherPost.column_names)
assert_equal([:name, :title, :content], PlainPost.spreadsheet_columns)
end
end

class TestToCsv < MiniTest::Test
test "test_class_method" do
p = Post.to_csv(spreadsheet_columns: [:name, :votes, :content, :ranking])
assert_equal(true, p.is_a?(String))
end
test 'test_chained_method' do
p = Post.order("name asc").to_csv(spreadsheet_columns: [:name, :votes, :content, :ranking])
assert_equal(true, p.is_a?(String))
end
end

class TestToOds < MiniTest::Test
test 'test_class_method' do
p = Post.to_ods(spreadsheet_columns: [:name, :votes, :content, :ranking])
assert_equal(true, p.is_a?(String))
end
test 'test_chained_method' do
p = Post.order("name asc").to_ods(spreadsheet_columns: [:name, :votes, :content, :ranking])
assert_equal(true, p.is_a?(String))
end
end

class TestToXlsx < MiniTest::Test
test 'test_class_method' do
p = Post.to_xlsx(spreadsheet_columns: [:name, :votes, :content, :ranking])
assert_equal(true, p.is_a?(String))
end
test 'test_chained_method' do
p = Post.order("name asc").to_xlsx(spreadsheet_columns: [:name, :votes, :content, :ranking])
assert_equal(true, p.is_a?(String))
end
end

0 comments on commit 4e4ce3b

Please sign in to comment.