clearbooks 0.16.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/AUTHORS.md +31 -0
- data/CHANGELOG.md +0 -0
- data/COPYING.md +12 -0
- data/FAQ.md +8 -0
- data/Gemfile +105 -0
- data/LICENSE.md +14 -0
- data/MAINTAINERS.md +40 -0
- data/README.md +549 -0
- data/Rakefile +94 -0
- data/Thorfile +80 -0
- data/bin/clearbooks +28 -0
- data/clearbooks.gemspec +119 -0
- data/examples/demo.rb +8 -0
- data/lib/clearbooks.rb +92 -0
- data/lib/clearbooks/core_ext.rb +8 -0
- data/lib/clearbooks/core_ext/array.rb +51 -0
- data/lib/clearbooks/core_ext/hash.rb +47 -0
- data/lib/clearbooks/core_ext/io_binary_read.rb +20 -0
- data/lib/clearbooks/core_ext/string.rb +21 -0
- data/lib/clearbooks/error.rb +8 -0
- data/lib/clearbooks/error/errors.rb +228 -0
- data/lib/clearbooks/interface/rake/cucumber.rb +36 -0
- data/lib/clearbooks/interface/rake/default.rb +28 -0
- data/lib/clearbooks/interface/rake/documentation.rb +45 -0
- data/lib/clearbooks/interface/rake/guard.rb +13 -0
- data/lib/clearbooks/interface/rake/helpers.rb +27 -0
- data/lib/clearbooks/interface/rake/library.rb +126 -0
- data/lib/clearbooks/interface/rake/metric.rb +15 -0
- data/lib/clearbooks/interface/rake/rspec.rb +31 -0
- data/lib/clearbooks/interface/thor/info.thor +292 -0
- data/lib/clearbooks/interface/thor/mixin/config_choice.rb +27 -0
- data/lib/clearbooks/interface/thor/mixin/configuration.rb +28 -0
- data/lib/clearbooks/interface/thor/mixin/default.rb +30 -0
- data/lib/clearbooks/interface/thor/mixin/default_config.rb +31 -0
- data/lib/clearbooks/interface/thor/mixin/guess.rb +57 -0
- data/lib/clearbooks/interface/thor/mixin/shell.rb +225 -0
- data/lib/clearbooks/interface/thor/version.thor +34 -0
- data/lib/clearbooks/library/client.rb +257 -0
- data/lib/clearbooks/library/configuration.rb +34 -0
- data/lib/clearbooks/model/account_code.rb +65 -0
- data/lib/clearbooks/model/base.rb +67 -0
- data/lib/clearbooks/model/entity.rb +225 -0
- data/lib/clearbooks/model/invoice.rb +163 -0
- data/lib/clearbooks/model/item.rb +78 -0
- data/lib/clearbooks/model/journal.rb +74 -0
- data/lib/clearbooks/model/ledger.rb +52 -0
- data/lib/clearbooks/model/payment.rb +113 -0
- data/lib/clearbooks/model/project.rb +58 -0
- data/lib/clearbooks/version.rb +12 -0
- data/spec/clearbooks/clearbooks_spec.rb +27 -0
- data/spec/clearbooks/model/account_code_spec.rb +52 -0
- data/spec/clearbooks/model/entity_spec.rb +107 -0
- data/spec/clearbooks/model/invoice_spec.rb +109 -0
- data/spec/clearbooks/model/journal_spec.rb +77 -0
- data/spec/clearbooks/model/payment_spec.rb +103 -0
- data/spec/clearbooks/model/project_spec.rb +72 -0
- data/spec/fixtures/response/allocate_payment.xml +12 -0
- data/spec/fixtures/response/create_entity.xml +12 -0
- data/spec/fixtures/response/create_invoice.xml +11 -0
- data/spec/fixtures/response/create_journal.xml +12 -0
- data/spec/fixtures/response/create_payment.xml +12 -0
- data/spec/fixtures/response/create_project.xml +12 -0
- data/spec/fixtures/response/delete_entity.xml +12 -0
- data/spec/fixtures/response/delete_journal.xml +12 -0
- data/spec/fixtures/response/list_account_codes.xml +168 -0
- data/spec/fixtures/response/list_entities.xml +45 -0
- data/spec/fixtures/response/list_invoices.xml +56 -0
- data/spec/fixtures/response/list_projects.xml +16 -0
- data/spec/fixtures/response/no_api_key_fault.xml +8 -0
- data/spec/fixtures/response/well_formed_request.xml +10 -0
- data/spec/fixtures/response/wrong_api_key_fault.xml +8 -0
- data/spec/spec_helper.rb +26 -0
- metadata +212 -0
data/Rakefile
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# System
|
4
|
+
require 'bundler'
|
5
|
+
require 'bundler/gem_tasks'
|
6
|
+
|
7
|
+
require 'shellwords'
|
8
|
+
require 'fileutils'
|
9
|
+
require 'yaml'
|
10
|
+
|
11
|
+
require 'date'
|
12
|
+
require 'ostruct'
|
13
|
+
|
14
|
+
require 'ronn'
|
15
|
+
require 'rdiscount'
|
16
|
+
|
17
|
+
require 'benchmark'
|
18
|
+
require 'wicked_pdf'
|
19
|
+
|
20
|
+
require 'clearbooks/version'
|
21
|
+
|
22
|
+
|
23
|
+
### Project Customization for Thor and Rake
|
24
|
+
|
25
|
+
project = YAML.load_file( '.project.yaml' )
|
26
|
+
project.each_pair { |name, value| self.instance_variable_set( "@#{name.to_s}", value ) }
|
27
|
+
|
28
|
+
|
29
|
+
### General
|
30
|
+
|
31
|
+
desc "Generate proper README file from templates" # {{{
|
32
|
+
task :readme do |t|
|
33
|
+
source = "README.md.template"
|
34
|
+
target = "README.md"
|
35
|
+
|
36
|
+
content = File.readlines( source ).collect!{ |line| line.rstrip }
|
37
|
+
version = Clearbooks::VERSION
|
38
|
+
|
39
|
+
content[ content.index( "$Version$" ) ] = "Version " + version if( content.include?( "$Version$" ) )
|
40
|
+
File.write( target, content.join("\n") )
|
41
|
+
puts "(II) #{target.to_s} generated from #{source.to_s}"
|
42
|
+
end # }}}
|
43
|
+
|
44
|
+
desc "Generate Yardoc documentation for this project" # {{{
|
45
|
+
task :yardoc do |t|
|
46
|
+
|
47
|
+
# Define new tags for yardoc to detect
|
48
|
+
tags = {}
|
49
|
+
tags[ "module" ] = "Module"
|
50
|
+
tags[ "class" ] = "Class"
|
51
|
+
tags[ "fn" ] = "Function"
|
52
|
+
tags[ "brief" ] = "Description"
|
53
|
+
|
54
|
+
# Hide tags we don't want in yardoc output
|
55
|
+
hidden = %w[module class fn]
|
56
|
+
|
57
|
+
# Construct tag string for CLI command
|
58
|
+
tags_line = ""
|
59
|
+
tags.each_pair { |n,v| tags_line += " --tag #{n.to_s}:\"#{v.to_s}\"" }
|
60
|
+
hidden.each { |h| tags_line += " --hide-tag #{h.to_s}" }
|
61
|
+
|
62
|
+
puts "(II) Generating multi-file yardoc output written to doc/yardoc"
|
63
|
+
system "yard --private --protected --markup-provider=redcarpet --markup=markdown #{tags_line.to_s} -o doc/yardoc lib/**/**/*.rb - LICENSE MAINTAINERS"
|
64
|
+
|
65
|
+
puts "(II) Generating one-file yardoc output written to doc/yardoc_pdf"
|
66
|
+
system "yard --private --protected --markup-provider=redcarpet --markup=markdown --one-file #{tags_line.to_s} -o doc/yardoc_pdf lib/**/**/*.rb - LICENSE MAINTAINERS"
|
67
|
+
|
68
|
+
puts "(II) HTML to PDF written to doc/yardoc_pdf"
|
69
|
+
pdf = WickedPdf.new.pdf_from_string( File.read( "doc/yardoc_pdf/index.html" ) )
|
70
|
+
File.open 'doc/yardoc_pdf/index.pdf', 'wb' do |file|
|
71
|
+
file << pdf
|
72
|
+
end
|
73
|
+
|
74
|
+
end # }}}
|
75
|
+
|
76
|
+
desc "Generate Yard Graphs for this project" # {{{
|
77
|
+
task :yardgraph do |t|
|
78
|
+
basedir = "doc/yard-graph"
|
79
|
+
FileUtils.mkdir_p( basedir )
|
80
|
+
system "yard graph --dependencies --empty-mixins --full > #{basedir.to_s}/graph.dot"
|
81
|
+
system "dot -Tpng #{basedir.to_s}/graph.dot > #{basedir.to_s}/graph.png"
|
82
|
+
end # }}}
|
83
|
+
|
84
|
+
### Actions
|
85
|
+
|
86
|
+
|
87
|
+
### Helper Functions
|
88
|
+
|
89
|
+
|
90
|
+
### Load all Rake file tasks
|
91
|
+
Dir.glob( "{,lib/}#{@gem_name}/interface/rake/**/*.{rake,rb}" ) { |name| load name }
|
92
|
+
|
93
|
+
|
94
|
+
# vim:ts=2:tw=100:wm=100:syntax=ruby
|
data/Thorfile
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Make sure load path contains local + global
|
4
|
+
$LOAD_PATH << '.'
|
5
|
+
$LOAD_PATH << 'lib'
|
6
|
+
|
7
|
+
|
8
|
+
# System includes
|
9
|
+
require 'bundler'
|
10
|
+
|
11
|
+
require 'fileutils'
|
12
|
+
|
13
|
+
|
14
|
+
# Make Thor scripts debug-able, e.g. ruby -r debug -- Thorfile
|
15
|
+
require 'thor' unless defined? Thor::Runner
|
16
|
+
require 'thor/rake_compat'
|
17
|
+
|
18
|
+
|
19
|
+
require 'clearbooks/version'
|
20
|
+
|
21
|
+
# @class class Default < Thor
|
22
|
+
# @brief Default Tasks for Thor, with thight Rake integration
|
23
|
+
class Default < Thor
|
24
|
+
|
25
|
+
include Thor::RakeCompat
|
26
|
+
|
27
|
+
Bundler::GemHelper.install_tasks
|
28
|
+
|
29
|
+
## Rake Task bindings
|
30
|
+
|
31
|
+
# @fn def build # {{{
|
32
|
+
# @brief Build clearbooks gem
|
33
|
+
#
|
34
|
+
desc "build", "Build clearbooks-#{Clearbooks::VERSION}.gem into the pkg directory"
|
35
|
+
def build
|
36
|
+
Rake::Task["build"].execute
|
37
|
+
end # }}}
|
38
|
+
|
39
|
+
# @fn def clean # {{{
|
40
|
+
# @brief Clean clearbooks gem generated ressources
|
41
|
+
#
|
42
|
+
desc "clean", "Clean clearbooks-#{Clearbooks::VERSION}.gem generated files from current directories"
|
43
|
+
def clean
|
44
|
+
Rake::Task["clean"].execute
|
45
|
+
end # }}}
|
46
|
+
|
47
|
+
# @fn def install {{{
|
48
|
+
# @brief Build and install clearbooks gem into system gems
|
49
|
+
desc "install", "Build and install clearbooks-#{Clearbooks::VERSION}.gem into system gems"
|
50
|
+
def install
|
51
|
+
Rake::Task["install"].execute
|
52
|
+
end # }}}
|
53
|
+
|
54
|
+
# @fn def release {{{
|
55
|
+
# @brief Build, Tag and push built gem into internal repository
|
56
|
+
desc "release", "Create tag v#{Clearbooks::VERSION} and build and push clearbooks-#{Clearbooks::VERSION}.gem to internal gems repository"
|
57
|
+
def release
|
58
|
+
not ImplementedError # we can push to rubygems since its closed!
|
59
|
+
# Rake::Task["release"].execute
|
60
|
+
end # }}}
|
61
|
+
|
62
|
+
# @fn def spec {{{
|
63
|
+
# @brief Run RSpec unit tests
|
64
|
+
desc "spec", "Run RSpec code examples"
|
65
|
+
def spec
|
66
|
+
exec "rspec spec"
|
67
|
+
end # }}}
|
68
|
+
|
69
|
+
end # of class Default
|
70
|
+
|
71
|
+
# Project Customization for Thor and Rake
|
72
|
+
project = YAML.load_file( '.project.yaml' )
|
73
|
+
project.each_pair { |name, value| self.instance_variable_set( "@#{name.to_s}", value ) }
|
74
|
+
|
75
|
+
# Load all Thor/Rake file tasks
|
76
|
+
Dir.glob( "{,lib/}#{@gem_name}/interface/thor/**/*.{thor,rb}" ) { |name| Thor::Util.load_thorfile name }
|
77
|
+
Dir.glob( "{,lib/}#{@gem_name}/interface/rake/**/*.{rake,rb}" ) { |name| load name }
|
78
|
+
|
79
|
+
|
80
|
+
# vim:ts=2:tw=100:wm=100:syntax=ruby
|
data/bin/clearbooks
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$:.unshift File.expand_path('../../lib/', __FILE__)
|
4
|
+
|
5
|
+
# System includes
|
6
|
+
require 'bundler'
|
7
|
+
require 'pry'
|
8
|
+
require 'thor' unless defined? Thor::Runner
|
9
|
+
|
10
|
+
# Custom includes
|
11
|
+
require_relative '../lib/clearbooks'
|
12
|
+
|
13
|
+
|
14
|
+
# @class Clearbooks executable
|
15
|
+
# @brief Main entry point for commandline execution
|
16
|
+
class Default < Thor
|
17
|
+
|
18
|
+
default_task :console
|
19
|
+
desc 'console', 'Launch Clearbooks console'
|
20
|
+
def console
|
21
|
+
Clearbooks.pry
|
22
|
+
end
|
23
|
+
|
24
|
+
end # of class Default
|
25
|
+
|
26
|
+
Default.start( ARGV )
|
27
|
+
|
28
|
+
# vim:ts=2:tw=100:wm=100:syntax=ruby
|
data/clearbooks.gemspec
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
# File: clearbooks.gemspec
|
2
|
+
# coding: utf-8
|
3
|
+
|
4
|
+
|
5
|
+
# Make sure lib is in Load path
|
6
|
+
lib = File.expand_path( '../lib/', __FILE__ )
|
7
|
+
$LOAD_PATH.unshift lib unless $LOAD_PATH.include?( lib )
|
8
|
+
|
9
|
+
# System includes
|
10
|
+
require 'date'
|
11
|
+
|
12
|
+
# Custom includes
|
13
|
+
require 'clearbooks/version'
|
14
|
+
|
15
|
+
Gem::Specification.new do |spec|
|
16
|
+
|
17
|
+
spec.name = 'clearbooks'
|
18
|
+
|
19
|
+
spec.description = %q(Unofficial Clear Books PLC (https://www.clearbooks.co.uk) gem handling all interactions of their SOAP API via native Ruby interface)
|
20
|
+
spec.summary = spec.description
|
21
|
+
|
22
|
+
spec.authors = [ 'Bjoern Rennhak', 'Oleg Kukareka' ]
|
23
|
+
spec.email = [ 'bjoern@greylon.com', 'oleg@kukareka.com' ]
|
24
|
+
|
25
|
+
spec.homepage = 'http://github.com/greylon/clearbooks'
|
26
|
+
|
27
|
+
spec.licenses = %w[MIT]
|
28
|
+
|
29
|
+
spec.date = DateTime.now.to_s.split( 'T' ).first
|
30
|
+
spec.version = Clearbooks::VERSION
|
31
|
+
spec.platform = Gem::Platform::RUBY
|
32
|
+
|
33
|
+
spec.metadata = {
|
34
|
+
'issue_tracker' => 'http://github.com/greylon/clearbooks/issues'
|
35
|
+
}
|
36
|
+
|
37
|
+
spec.bindir = 'bin'
|
38
|
+
spec.executables = %w[clearbooks]
|
39
|
+
|
40
|
+
spec.require_paths = %w[lib]
|
41
|
+
|
42
|
+
spec.files = %w[
|
43
|
+
AUTHORS.md
|
44
|
+
CHANGELOG.md
|
45
|
+
COPYING.md
|
46
|
+
FAQ.md
|
47
|
+
LICENSE.md
|
48
|
+
MAINTAINERS.md
|
49
|
+
Gemfile
|
50
|
+
README.md
|
51
|
+
Rakefile
|
52
|
+
Thorfile
|
53
|
+
clearbooks.gemspec
|
54
|
+
]
|
55
|
+
|
56
|
+
spec.files += Dir.glob( 'bin/**/*' )
|
57
|
+
|
58
|
+
spec.files += Dir.glob( 'lib/**/*.rb' )
|
59
|
+
spec.files += Dir.glob( 'lib/**/*.thor' )
|
60
|
+
|
61
|
+
spec.files += Dir.glob( 'spec/**/*' )
|
62
|
+
|
63
|
+
spec.files += Dir.glob( 'data/**/*' )
|
64
|
+
|
65
|
+
spec.files += Dir.glob( 'documentation/**/*' )
|
66
|
+
|
67
|
+
spec.files += Dir.glob( 'examples/**/*' )
|
68
|
+
|
69
|
+
spec.files += Dir.glob( 'base/**/*' )
|
70
|
+
|
71
|
+
spec.test_files += Dir.glob( 'test/**/*' )
|
72
|
+
spec.test_files += Dir.glob( 'spec/**/*' )
|
73
|
+
spec.test_files += Dir.glob( 'features/**/*' )
|
74
|
+
|
75
|
+
## Dependencies
|
76
|
+
|
77
|
+
# Ruby VM
|
78
|
+
spec.required_ruby_version = '>= 1.9'
|
79
|
+
|
80
|
+
# General
|
81
|
+
spec.add_runtime_dependency 'thor', '~> 0.19'
|
82
|
+
|
83
|
+
# Middlewares
|
84
|
+
spec.add_runtime_dependency 'savon', '~> 2.11'
|
85
|
+
|
86
|
+
spec.add_runtime_dependency 'andand', '~> 1.3'
|
87
|
+
|
88
|
+
spec.add_runtime_dependency 'pry', '~> 0.10'
|
89
|
+
|
90
|
+
|
91
|
+
# Post Install
|
92
|
+
spec.post_install_message = <<-EOS
|
93
|
+
|
94
|
+
____ _ _____ _ ____ ____ ___ ___ _ ______
|
95
|
+
/ ___| | | ____| / \ | _ \| __ ) / _ \ / _ \| |/ / ___|
|
96
|
+
| | | | | _| / _ \ | |_) | _ \| | | | | | | ' /\___ \
|
97
|
+
| |___| |___| |___ / ___ \| _ <| |_) | |_| | |_| | . \ ___) |
|
98
|
+
\____|_____|_____/_/ \_\_| \_\____/ \___/ \___/|_|\_\____/
|
99
|
+
|
100
|
+
|
101
|
+
(c) #{spec.date.to_s}, All rights reserved
|
102
|
+
Bjoern Rennhak, Greylon Ltd.
|
103
|
+
|
104
|
+
Don't forget to get Clearbooks API key from http://clearbooks.co.uk
|
105
|
+
and save it in ~/.clearbooks/config.yml:
|
106
|
+
|
107
|
+
$ echo "api_key: {your_api_key}" >> ~/.clearbooks/config.yml
|
108
|
+
|
109
|
+
You can also provide the API key in ENV['CLEARBOOKS_API_KEY']:
|
110
|
+
|
111
|
+
$ CLEARBOOKS_API_KEY=your_api_key clearbooks
|
112
|
+
|
113
|
+
Thanks for installing unofficial Clearbooks Gem !
|
114
|
+
EOS
|
115
|
+
|
116
|
+
end # of Gem::Specification.new do |s|
|
117
|
+
|
118
|
+
|
119
|
+
# vim:ts=2:tw=100:wm=100:syntax=ruby
|
data/examples/demo.rb
ADDED
data/lib/clearbooks.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
|
4
|
+
# Standard library includes
|
5
|
+
require 'bundler'
|
6
|
+
require 'thor'
|
7
|
+
require 'rake'
|
8
|
+
|
9
|
+
# Custom library includes
|
10
|
+
require_relative 'clearbooks/core_ext'
|
11
|
+
require_relative 'clearbooks/error'
|
12
|
+
|
13
|
+
|
14
|
+
# @module module Clearbooks
|
15
|
+
# @brief Clearbooks modules and classes namespace
|
16
|
+
module Clearbooks
|
17
|
+
|
18
|
+
DEFAULT_CONFIG = '.clearbooks/config.yml'.freeze
|
19
|
+
|
20
|
+
autoload :Client, 'clearbooks/library/client'
|
21
|
+
autoload :Configuration, 'clearbooks/library/configuration'
|
22
|
+
|
23
|
+
autoload :Base, 'clearbooks/model/base'
|
24
|
+
autoload :Invoice, 'clearbooks/model/invoice'
|
25
|
+
autoload :Item, 'clearbooks/model/item'
|
26
|
+
autoload :Entity, 'clearbooks/model/entity'
|
27
|
+
autoload :Project, 'clearbooks/model/project'
|
28
|
+
autoload :AccountCode, 'clearbooks/model/account_code'
|
29
|
+
autoload :Journal, 'clearbooks/model/journal'
|
30
|
+
autoload :Ledger, 'clearbooks/model/ledger'
|
31
|
+
autoload :Payment, 'clearbooks/model/payment'
|
32
|
+
|
33
|
+
class << self
|
34
|
+
|
35
|
+
# @fn def client {{{
|
36
|
+
# @brief Clearbooks client instance. You can use static methods in Clearbooks module instead of referring to the client instance.
|
37
|
+
#
|
38
|
+
# @example Clearbooks.list_invoices
|
39
|
+
# # or
|
40
|
+
# Clearbooks.client.list_invoices
|
41
|
+
def client
|
42
|
+
@client ||= Client.new
|
43
|
+
end # }}}
|
44
|
+
|
45
|
+
# @fn def config {{{
|
46
|
+
# @brief Clearbooks configuration
|
47
|
+
#
|
48
|
+
# @return [Configuration] Returns Configuration object
|
49
|
+
def config
|
50
|
+
@config ||= Configuration.new
|
51
|
+
end # }}}
|
52
|
+
|
53
|
+
# @fn def configure {{{
|
54
|
+
# @brief Use a block syntax to configure the gem
|
55
|
+
#
|
56
|
+
# @return [Hash] Returns Configuration object
|
57
|
+
#
|
58
|
+
# @example Clearbooks.configure do |config|
|
59
|
+
# config.api_key = 'api_key'
|
60
|
+
# config.log = true
|
61
|
+
# config.logger = Logger.new(STDOUT)
|
62
|
+
# end
|
63
|
+
def configure
|
64
|
+
yield config
|
65
|
+
end # }}}
|
66
|
+
|
67
|
+
# @fn def method_missing method, *args, &block {{{
|
68
|
+
# @brief Duck typing response for Clearbooks missing methods behavior
|
69
|
+
#
|
70
|
+
# @param [String] method Some unknown method call on Clearbooks class
|
71
|
+
# @param [Array] args Some given function arguments with given method call
|
72
|
+
# @param [Explicit Block] block Some given block argument with the function and arguments
|
73
|
+
#
|
74
|
+
def method_missing method, *args, &block
|
75
|
+
client.send method, *args, &block
|
76
|
+
end # }}}
|
77
|
+
|
78
|
+
# @fn def respond_to? *args {{{
|
79
|
+
# @brief Duck typing response for Clearbooks method queries
|
80
|
+
#
|
81
|
+
# @param [Array] args Function and function parameter argument list
|
82
|
+
#
|
83
|
+
def respond_to? *args
|
84
|
+
super || client.respond_to?(*args)
|
85
|
+
end # }}}
|
86
|
+
|
87
|
+
end # of class << self
|
88
|
+
|
89
|
+
end # of module Clearbooks
|
90
|
+
|
91
|
+
|
92
|
+
# vim:ts=2:tw=100:wm=100:syntax=ruby
|
@@ -0,0 +1,51 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
|
4
|
+
# @class class Array
|
5
|
+
# @brief
|
6
|
+
class Array
|
7
|
+
|
8
|
+
class << self
|
9
|
+
|
10
|
+
# @fn def delete_unless &block {{{
|
11
|
+
# @brief Inverted #delete_if function for convenience
|
12
|
+
#
|
13
|
+
def delete_unless &block
|
14
|
+
delete_if{ |element| not block.call( element ) }
|
15
|
+
end # }}}
|
16
|
+
|
17
|
+
# @fn def %(len) {{{
|
18
|
+
# @brief Chunk array into convienent sub-chunks
|
19
|
+
#
|
20
|
+
# @credit http://drnicwilliams.com/2007/03/22/meta-magic-in-ruby-presentation/
|
21
|
+
# direct original source at http://redhanded.hobix.com/bits/matchingIntoMultipleAssignment.html
|
22
|
+
#
|
23
|
+
# now e.g. this is possible
|
24
|
+
#
|
25
|
+
# ["foo0", "foo1", "foo2", "foo3", "foo4", "foo5", "foo6", "foo7", "foo8", "foo9", "foo10"]
|
26
|
+
# [ ["foo0", "foo1", "foo2"], ["foo3", "foo4", "foo5"], ["foo6", "foo7", "foo8"], ["foo9", "foo10"]]
|
27
|
+
#
|
28
|
+
def %(len)
|
29
|
+
inject([]) do |array, x|
|
30
|
+
array << [] if [*array.last].nitems % len == 0
|
31
|
+
array.last << x
|
32
|
+
array
|
33
|
+
end
|
34
|
+
end # }}}
|
35
|
+
|
36
|
+
# @fn def sum {{{
|
37
|
+
def sum
|
38
|
+
inject( nil ) { |sum,x| sum ? sum+x : x }
|
39
|
+
end # }}}
|
40
|
+
|
41
|
+
# @fn def mean {{{
|
42
|
+
# @brief
|
43
|
+
def mean
|
44
|
+
sum / size
|
45
|
+
end # }}}
|
46
|
+
|
47
|
+
end # of class << self
|
48
|
+
end # of class Array
|
49
|
+
|
50
|
+
|
51
|
+
# vim:ts=2:tw=100:wm=100
|