From 4e4ce3bbc60cab22767776b91f6d5f30f653c1ac Mon Sep 17 00:00:00 2001 From: Weston Ganger Date: Tue, 3 May 2016 06:30:45 -0700 Subject: [PATCH] first --- .gitignore | 8 +++ .travis.yml | 10 ++++ CHANGELOG.md | 5 ++ Gemfile | 8 +++ LICENSE | 7 +++ README.md | 9 ++++ Rakefile | 13 +++++ lib/rearmed.rb | 59 ++++++++++++++++++++++ lib/rearmed/version.rb | 3 ++ rearmed_rb.gemspec | 27 ++++++++++ test.rb | 8 +++ test/database.yml | 3 ++ test/helper.rb | 52 ++++++++++++++++++++ test/tc_spreadsheet_architect.rb | 84 ++++++++++++++++++++++++++++++++ 14 files changed, 296 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 CHANGELOG.md create mode 100644 Gemfile create mode 100644 LICENSE create mode 100644 README.md create mode 100644 Rakefile create mode 100644 lib/rearmed.rb create mode 100644 lib/rearmed/version.rb create mode 100644 rearmed_rb.gemspec create mode 100644 test.rb create mode 100644 test/database.yml create mode 100644 test/helper.rb create mode 100644 test/tc_spreadsheet_architect.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..314d1aa --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.bundle/ +Gemfile.lock +coverage/ +doc/ +pkg/ +spec/reports/ +tmp/ +test/*.sqlite3.db diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f76fa3a --- /dev/null +++ b/.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" diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..35b4341 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +CHANGELOG +--------- + +- **April.30.2016**: 0.1.0 + - Gem Initial Release diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..bb9a938 --- /dev/null +++ b/Gemfile @@ -0,0 +1,8 @@ +source 'https://rubygems.org' +gemspec + +gem 'method_source' + +group :rails do + gem 'rails', '>= 3.2.0' +end diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c22417c --- /dev/null +++ b/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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..21d1dd3 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# Spreadsheet Architect +Buy Me A Coffee + + + +# Credits +Created by Weston Ganger - @westonganger + +Buy Me A Coffee diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..aec1bb0 --- /dev/null +++ b/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 diff --git a/lib/rearmed.rb b/lib/rearmed.rb new file mode 100644 index 0000000..f47f68b --- /dev/null +++ b/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 diff --git a/lib/rearmed/version.rb b/lib/rearmed/version.rb new file mode 100644 index 0000000..3be7d0f --- /dev/null +++ b/lib/rearmed/version.rb @@ -0,0 +1,3 @@ +module Rearmed + VERSION = "0.1.0" +end diff --git a/rearmed_rb.gemspec b/rearmed_rb.gemspec new file mode 100644 index 0000000..a6e9104 --- /dev/null +++ b/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 diff --git a/test.rb b/test.rb new file mode 100644 index 0000000..6210c95 --- /dev/null +++ b/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)}}" diff --git a/test/database.yml b/test/database.yml new file mode 100644 index 0000000..c3eec4b --- /dev/null +++ b/test/database.yml @@ -0,0 +1,3 @@ +sqlite3: + adapter: sqlite3 + database: test/spreadsheet_architect.sqlite3.db diff --git a/test/helper.rb b/test/helper.rb new file mode 100644 index 0000000..3486233 --- /dev/null +++ b/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! } diff --git a/test/tc_spreadsheet_architect.rb b/test/tc_spreadsheet_architect.rb new file mode 100644 index 0000000..99ad138 --- /dev/null +++ b/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