dmorrill10-utils 0.0.2

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.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dmorrill10-utils.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Dustin Morrill
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.
@@ -0,0 +1,25 @@
1
+ # Dmorrill10::Utils
2
+
3
+ Group of random Ruby mixin utility functions.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'dmorrill10-utils'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install dmorrill10-utils
18
+
19
+ ## Contributing
20
+
21
+ 1. Fork it
22
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
23
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
24
+ 4. Push to the branch (`git push origin my-new-feature`)
25
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/dmorrill10-utils/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Dustin Morrill"]
6
+ gem.email = ["morrill@ualberta.ca"]
7
+ gem.description = %q{Group of random Ruby mixin utility functions.}
8
+ gem.summary = %q{Group of random Ruby mixin utility functions.}
9
+ gem.homepage = 'https://github.com/dmorrill10/dmorrill10-utils'
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "dmorrill10-utils"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Dmorrill10::Utils::VERSION
17
+ end
@@ -0,0 +1,11 @@
1
+ require "dmorrill10-utils/version"
2
+
3
+ require 'dmorrill10-utils/class'
4
+ require 'dmorrill10-utils/enumerable'
5
+ require 'dmorrill10-utils/integer'
6
+ require 'dmorrill10-utils/string_form_manipulation'
7
+
8
+ module Dmorrill10
9
+ module Utils
10
+ end
11
+ end
@@ -0,0 +1,32 @@
1
+
2
+ require File.expand_path('../string_form_manipulation', __FILE__)
3
+
4
+ class Class
5
+ private
6
+
7
+ # @param [Array] names A list of exception names that the calling class would
8
+ # like to define.
9
+ def exceptions(*names)
10
+ names.each do |name|
11
+ error_name = name.to_class_name
12
+ const_set(error_name, Class.new(RuntimeError))
13
+ end
14
+ end
15
+
16
+ def alias_new(alias_of_new)
17
+ alias_class_method alias_of_new, :new
18
+ end
19
+
20
+ # @param [#to_sym] alias_method A class method to alias the class method, +method_to_alias+.
21
+ # @param [#to_sym] method_to_alias A class method to be aliased by +alias_method+.
22
+ def alias_class_method(alias_method, method_to_alias)
23
+ singleton_class.alias_method_in_singleton_context alias_method.to_sym, method_to_alias.to_sym
24
+ end
25
+
26
+ # Must do this operation in singleton context
27
+ class << self
28
+ def alias_method_in_singleton_context(alias_method, method_to_alias)
29
+ alias_method alias_method, method_to_alias
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,14 @@
1
+ module Enumerable
2
+ # @return [Integer] Sum of the elements in this instance. All elements must
3
+ # have a +to_i+ method, which converts the element into a form that may be
4
+ # summed with an +Integer+.
5
+ def sum
6
+ flatten.inject(0){ |sum, element_amount| sum += element_amount.to_i }
7
+ end
8
+
9
+ # @return [Array] The array resulting from summing all elements in
10
+ # this instance. All elements must have a +sum+ or +to_i+ method.
11
+ def mapped_sum
12
+ map { |element| if element.respond_to? :sum then element.sum else element.to_i end }
13
+ end
14
+ end
@@ -0,0 +1,69 @@
1
+
2
+ require File.expand_path('../class', __FILE__)
3
+
4
+ ########### Table game specific ##########
5
+ class Integer
6
+
7
+ exceptions :seat_out_of_bounds, :relative_position_out_of_bounds
8
+
9
+ # @param [Integer] seat The seat to which the relative position is desired.
10
+ # @param [Integer] number_of_players The number of players at the table.
11
+ # @return [Integer] The relative position of +self+ to +seat+, given the
12
+ # number of players at the table, +number_of_players+, indexed such that
13
+ # the seat immediately to the left of +seat+ has a +position_relative_to+ of
14
+ # zero.
15
+ # @example <code>1.position_relative_to 0, 3</code> == 0
16
+ # @example <code>1.position_relative_to 1, 3</code> == 2
17
+ def position_relative_to(seat, number_of_players)
18
+ raise SeatOutOfBounds unless seat.seat_in_bounds?(number_of_players) &&
19
+ seat_in_bounds?(number_of_players)
20
+
21
+ adjusted_seat = if self > seat
22
+ self
23
+ else
24
+ self + number_of_players
25
+ end
26
+ adjusted_seat - seat - 1
27
+ end
28
+
29
+ # Inverse operation of +position_relative_to+.
30
+ # Given
31
+ # <code>relative_position = seat.position_relative_to to_seat, number_of_players</code>
32
+ # then
33
+ # <code>to_seat = seat.seat_from_relative_position relative_position, number_of_players</code>
34
+ #
35
+ # @param [Integer] relative_position_of_self_to_result The relative position
36
+ # of seat +self+ to the seat that is returned by this function.
37
+ # @param [Integer] number_of_players The number of players at the table.
38
+ # @return [Integer] The seat to which the relative position,
39
+ # +relative_position_of_self_to_result+, of +self+ was derived, given the
40
+ # number of players at the table, +number_of_players+, indexed such that
41
+ # the seat immediately to the left of +from_seat+ has a
42
+ # +position_relative_to+ of zero.
43
+ # @example <code>1.seat_from_relative_position 0, 3</code> == 0
44
+ # @example <code>1.seat_from_relative_position 2, 3</code> == 1
45
+ def seat_from_relative_position(relative_position_of_self_to_result,
46
+ number_of_players)
47
+ raise SeatOutOfBounds unless seat_in_bounds?(number_of_players)
48
+
49
+ unless relative_position_of_self_to_result.seat_in_bounds?(number_of_players)
50
+ raise RelativePositionOutOfBounds
51
+ end
52
+
53
+ position_adjustment = relative_position_of_self_to_result + 1
54
+
55
+ to_seat = self + number_of_players - position_adjustment
56
+ if self > to_seat || !to_seat.seat_in_bounds?(number_of_players)
57
+ self - position_adjustment
58
+ else
59
+ to_seat
60
+ end
61
+ end
62
+
63
+ # @param [Integer] number_of_players The number of players at the table.
64
+ # @return [Bool] Reports whether or not +self+ represents an out of bounds
65
+ # seat.
66
+ def seat_in_bounds?(number_of_players)
67
+ self < number_of_players && self >= 0
68
+ end
69
+ end
@@ -0,0 +1,17 @@
1
+ module StringFormManipulation
2
+ # @return This string in camel-case class name form.
3
+ def to_camel_case
4
+ class_name = to_s.capitalize
5
+ class_name.gsub(/[_\s]+./) { |match| match = match[1,].capitalize }
6
+ end
7
+
8
+ alias_method :to_class_name, :to_camel_case
9
+ end
10
+
11
+ class String
12
+ include StringFormManipulation
13
+ end
14
+
15
+ class Symbol
16
+ include StringFormManipulation
17
+ end
@@ -0,0 +1,5 @@
1
+ module Dmorrill10
2
+ module Utils
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dmorrill10-utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dustin Morrill
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-17 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Group of random Ruby mixin utility functions.
15
+ email:
16
+ - morrill@ualberta.ca
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - Gemfile
22
+ - LICENSE
23
+ - README.md
24
+ - Rakefile
25
+ - dmorrill10-utils.gemspec
26
+ - lib/dmorrill10-utils.rb
27
+ - lib/dmorrill10-utils/class.rb
28
+ - lib/dmorrill10-utils/enumerable.rb
29
+ - lib/dmorrill10-utils/integer.rb
30
+ - lib/dmorrill10-utils/string_form_manipulation.rb
31
+ - lib/dmorrill10-utils/version.rb
32
+ homepage: https://github.com/dmorrill10/dmorrill10-utils
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.24
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Group of random Ruby mixin utility functions.
56
+ test_files: []
57
+ has_rdoc: