mtg 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2010 Austin Schneider
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ the Software, and to permit persons to whom the Software is furnished to do so,
8
+ subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
File without changes
@@ -0,0 +1,5 @@
1
+ require File.expand_path('lib/mtg')
2
+
3
+ a1 = MTG::Card::ManaCost.new("5 r r")
4
+
5
+ p a1.to_s
@@ -0,0 +1,8 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
2
+
3
+ module MTG
4
+ end
5
+
6
+ require 'mtg/card'
7
+ require 'mtg/color'
8
+ require 'mtg/set'
@@ -0,0 +1,8 @@
1
+ module MTG
2
+ module Card
3
+ end
4
+ end
5
+
6
+ require 'mtg/card/base'
7
+ require 'mtg/card/mana_cost'
8
+ require 'mtg/card/rarity'
@@ -0,0 +1,38 @@
1
+ require 'mtg/nameable'
2
+
3
+ module MTG
4
+ module Card
5
+ class Base
6
+
7
+ include Nameable
8
+
9
+ def initialize(name, attrs = {})
10
+ self.name = name
11
+ attrs.each { |k, v| send("#{k}=", v) }
12
+ end
13
+
14
+ attr_reader :set, :rarity, :number, :mana_cost
15
+
16
+ def converted_mana_cost
17
+ mana_cost.converted
18
+ end
19
+
20
+ def set=(set_name)
21
+ @set = Set.new(set_name)
22
+ end
23
+
24
+ def rarity=(rarity_name)
25
+ @rarity = Rarity.new(rarity_name)
26
+ end
27
+
28
+ def number=(number)
29
+ @number = number.to_i
30
+ end
31
+
32
+ def mana_cost=(str)
33
+ @mana_cost = ManaCost.new(str)
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,30 @@
1
+ module MTG
2
+ module Card
3
+ class ManaCost
4
+
5
+ NON_ZERO_PATTERN = /^(X*)([1-9]\d*)?([BGRUW]*)$/
6
+
7
+ def initialize(str)
8
+ str = str.to_s.strip.upcase
9
+ if (str != '') && str != '0' && !(str =~ NON_ZERO_PATTERN)
10
+ raise ArgumentError, "invalid mana cost"
11
+ end
12
+ @str = str
13
+ end
14
+
15
+ def to_s
16
+ @str
17
+ end
18
+
19
+ def converted
20
+ if ['', '0'].include?(@str)
21
+ 0
22
+ else
23
+ @str =~ NON_ZERO_PATTERN
24
+ $2.to_i + $3.length
25
+ end
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,39 @@
1
+ module MTG
2
+ module Card
3
+ class Rarity
4
+
5
+ include Comparable
6
+
7
+ NAMES = %w(common uncommon rare mythic\ rare)
8
+
9
+ def initialize(name)
10
+ unless NAMES.include?(name)
11
+ raise ArgumentError, "#{name.inspect} is not one of: #{NAMES.inspect}"
12
+ end
13
+ @name = name.to_s
14
+ end
15
+
16
+ attr_reader :name
17
+ alias_method :to_s, :name
18
+
19
+ def <=>(rarity)
20
+ NAMES.index(name) <=> NAMES.index(rarity.name)
21
+ end
22
+
23
+ def eql?(rarity)
24
+ self.class == rarity.class && self == rarity
25
+ end
26
+
27
+ def hash
28
+ name.hash
29
+ end
30
+
31
+ NAMES.each do |name|
32
+ define_method(name.gsub(/\s/, '_') + '?') do
33
+ self.name == name
34
+ end
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,42 @@
1
+ module MTG
2
+ class Color
3
+
4
+ # TODO: test
5
+ NAMES = {
6
+ 'B' => 'black',
7
+ 'G' => 'green',
8
+ 'R' => 'red',
9
+ 'U' => 'blue',
10
+ 'W' => 'white'
11
+ }
12
+
13
+ # TODO: test
14
+ def initialize(name)
15
+ self.name = name
16
+ end
17
+
18
+ attr_reader :name
19
+ alias_method :to_s, :name
20
+
21
+ # TODO: test
22
+ def abbr
23
+ NAMES.key(name)
24
+ end
25
+
26
+ private
27
+
28
+ # TODO: test
29
+ def name=(name)
30
+ name = name.to_s.strip
31
+ @name = case name
32
+ when /^(#{NAMES.keys.join('|')})$/i
33
+ NAMES[name.upcase]
34
+ when /^(#{NAMES.values.join('|')})$/i
35
+ name.downcase
36
+ else
37
+ raise ArgumentError, "name is not one of: #{(NAMES.values + NAMES.keys).inspect}"
38
+ end
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,20 @@
1
+ module MTG
2
+ module Nameable
3
+
4
+ def self.included(base)
5
+ base.send(:attr_reader, :name)
6
+ base.send(:alias_method, :to_s, :name)
7
+ end
8
+
9
+ def name=(name)
10
+ check_name!(name)
11
+ @name = name.to_s
12
+ end
13
+
14
+ private
15
+
16
+ def check_name!(name)
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ require 'mtg/nameable'
2
+
3
+ module MTG
4
+ class Set
5
+
6
+ include Nameable
7
+
8
+ # TODO: test
9
+ def initialize(name)
10
+ self.name = name
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'mtg'
3
+ s.version = '0.0.1'
4
+ s.authors = ['Austin Schneider']
5
+ s.email = "soccer022483@gmail.com"
6
+ s.description = 'Objects such as cards, colors, mana costs, etc relating to the Magic, The Gathering trading card game.'
7
+ # s.homepage = ""
8
+ s.summary = 'An MTG gem'
9
+ # s.post_install_message "Thanks for installing!"
10
+ s.files = Dir[
11
+ '[a-zA-Z]*',
12
+ 'lib/**/*',
13
+ 'spec/**/*'
14
+ ].to_a
15
+ s.test_files = []
16
+ s.require_paths = ["lib"]
17
+ s.extra_rdoc_files = ["README"]
18
+ s.has_rdoc = false
19
+
20
+ # s.add_dependency 'msgpack', '0.4.3'
21
+ s.add_development_dependency 'rspec', '2.0.1'
22
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe MTG::Card::Base do
4
+
5
+ before { @b = MTG::Card::Base.new('My Card') }
6
+
7
+ describe "#to_s" do
8
+ it("returns the name") { @b.to_s.should == @b.name }
9
+ end
10
+
11
+ describe "#set=" do
12
+ before do
13
+ @s = MTG::Set.new('my set')
14
+ MTG::Set.stub!(:new => @s)
15
+ end
16
+
17
+ it "sends Set.new with the given name" do
18
+ MTG::Set.should_receive(:new).with('my set')
19
+ @b.set = 'my set'
20
+ end
21
+
22
+ it "sets the set" do
23
+ @b.set = 'my set'
24
+ @b.set.should eq(@s)
25
+ end
26
+ end
27
+
28
+ describe "#rarity=" do
29
+ before do
30
+ @r = MTG::Card::Rarity.new('rare')
31
+ MTG::Card::Rarity.stub!(:new => @r)
32
+ end
33
+
34
+ it "instantiates a rarity with the given name" do
35
+ MTG::Card::Rarity.should_receive(:new).with('rare')
36
+ @b.rarity = 'rare'
37
+ end
38
+
39
+ it "sets the rarity" do
40
+ @b.rarity = 'rare'
41
+ @b.rarity.should eq(@r)
42
+ end
43
+ end
44
+
45
+ describe "#number=" do
46
+ before { @integerable = Struct.new(:to_i).new(87623) }
47
+
48
+ it("sets the number") do
49
+ @b.number = @integerable
50
+ @b.number.should == 87623
51
+ end
52
+ end
53
+
54
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ VALID_MANA_COST_STRINGS = [
4
+ [nil, 0],
5
+ ['', 0],
6
+ ['0', 0],
7
+ ['X', 0],
8
+ ['XX', 0],
9
+ ['X1', 1],
10
+ ['XX1', 1],
11
+ ['X12', 12],
12
+ ['XX12', 12],
13
+ ['XGUB', 3],
14
+ ['XX12UBW', 15]
15
+ ]
16
+
17
+ INVALID_MANA_COST_STRINGS = [
18
+ '01',
19
+ 'X01',
20
+ 'X01GU',
21
+ 'GU1',
22
+ 'UX',
23
+ 'BX',
24
+ '10X'
25
+ ]
26
+
27
+ describe MTG::Card::ManaCost do
28
+
29
+ describe ".new" do
30
+ VALID_MANA_COST_STRINGS.each do |str, x|
31
+ context "with #{str.inspect}" do
32
+ before { @str = str }
33
+ it "does not raise an error" do
34
+ lambda { MTG::Card::ManaCost.new(@str) }.should_not raise_error
35
+ end
36
+ end
37
+ end
38
+
39
+ INVALID_MANA_COST_STRINGS.each do |str|
40
+ context "with #{str.inspect}" do
41
+ before { @str = str }
42
+ it "does raises an argument error" do
43
+ lambda { MTG::Card::ManaCost.new(@str) }.should raise_error(ArgumentError)
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ ##################################################
50
+
51
+ describe "#converted" do
52
+ VALID_MANA_COST_STRINGS.each do |str, conv_cost|
53
+ context "with #{str.inspect}" do
54
+ before { @mc = MTG::Card::ManaCost.new(str) }
55
+
56
+ it("returns #{conv_cost.inspect}") { @mc.converted.should == conv_cost }
57
+ end
58
+ end
59
+ end
60
+
61
+ describe "#to_s" do
62
+ str = VALID_MANA_COST_STRINGS[0][0]
63
+ context "with #{str.inspect}" do
64
+ before { @mc = MTG::Card::ManaCost.new(str) }
65
+ it("returns #{str.to_s.inspect}") { @mc.to_s.should == str.to_s }
66
+ end
67
+ end
68
+
69
+ end
@@ -0,0 +1,106 @@
1
+ require 'spec_helper'
2
+
3
+ describe MTG::Card::Rarity do
4
+
5
+ before { @r = MTG::Card::Rarity.new('rare') }
6
+
7
+ it "should have NAMES" do
8
+ MTG::Card::Rarity::NAMES.should == %w(common uncommon rare mythic\ rare)
9
+ end
10
+
11
+ describe "#new" do
12
+ MTG::Card::Rarity::NAMES.each do |name|
13
+ context "when the name is #{name.inspect}" do
14
+ before { @r2 = MTG::Card::Rarity.new(name) }
15
+
16
+ it("returns an instance") { @r2.should be_an_instance_of(MTG::Card::Rarity) }
17
+ it("sets the name") { @r2.name.should == name }
18
+ end
19
+ end
20
+
21
+ context "when the name is something like 'foo'" do
22
+ it "raises an error" do
23
+ lambda { MTG::Card::Rarity.new('foo') }.should raise_error(ArgumentError)
24
+ end
25
+ end
26
+ end
27
+
28
+ describe "#<=>" do
29
+ MTG::Card::Rarity::NAMES.each do |name|
30
+ context "when the name is #{name.inspect}" do
31
+ before { @r = MTG::Card::Rarity.new(name) }
32
+
33
+ context "when the given rarity's name is #{name.inspect}" do
34
+ before { @r2 = MTG::Card::Rarity.new(name) }
35
+
36
+ it("returns 0") { (@r <=> @r2).should eq(0) }
37
+ end
38
+
39
+ MTG::Card::Rarity::NAMES.select do |n|
40
+ MTG::Card::Rarity::NAMES.index(n) < MTG::Card::Rarity::NAMES.index(name)
41
+ end.each do |lower_name|
42
+ context "when the given rarity's name is #{lower_name.inspect}" do
43
+ before { @r2 = MTG::Card::Rarity.new(lower_name) }
44
+
45
+ it("returns 1") { (@r <=> @r2).should eq(1) }
46
+ end
47
+ end
48
+
49
+ MTG::Card::Rarity::NAMES.select do |n|
50
+ MTG::Card::Rarity::NAMES.index(n) > MTG::Card::Rarity::NAMES.index(name)
51
+ end.each do |higher_name|
52
+ context "when the given rarity's name is #{higher_name.inspect}" do
53
+ before { @r2 = MTG::Card::Rarity.new(higher_name) }
54
+
55
+ it("returns -1") { (@r <=> @r2).should eq(-1) }
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ describe "#eql?" do
63
+ context "when given a rarity" do
64
+ before { @r2 = MTG::Card::Rarity.new('rare') }
65
+
66
+ context "when the given rarity is ==" do
67
+ it("returns true") { @r.eql?(@r2).should be_true }
68
+ end
69
+
70
+ context "when the given rarity is not ==" do
71
+ before { @r2 = MTG::Card::Rarity.new('common') }
72
+
73
+ it("returns false") { @r.eql?(@r2).should be_false }
74
+ end
75
+ end
76
+
77
+ context "when given a non-rarity object" do
78
+ it("returns false") { @r.eql?("yo!").should be_false }
79
+ end
80
+ end
81
+
82
+ describe "#hash" do
83
+ it "returns the name#hash" do
84
+ @r.hash.should == @r.name.hash
85
+ end
86
+ end
87
+
88
+ MTG::Card::Rarity::NAMES.each do |name|
89
+ question_method_name = name.gsub(/\s/, '_') + '?'
90
+
91
+ describe "##{question_method_name}" do
92
+ context "when the name is #{name.inspect}" do
93
+ before { @r2 = MTG::Card::Rarity.new(name) }
94
+ it("returns true") { @r2.send(question_method_name).should be_true }
95
+ end
96
+
97
+ MTG::Card::Rarity::NAMES.select { |n| n != name }.each do |dif_name|
98
+ context "when the name is #{dif_name.inspect}" do
99
+ before { @r2 = MTG::Card::Rarity.new(dif_name) }
100
+ it("returns false") { @r2.send(question_method_name).should be_false }
101
+ end
102
+ end
103
+ end
104
+ end
105
+
106
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ class NameableObject
4
+ include MTG::Nameable
5
+ end
6
+
7
+ describe NameableObject do
8
+
9
+ before { @no = NameableObject.new }
10
+
11
+ describe "#name=" do
12
+ it "checks the name" do
13
+ @no.should_receive(:check_name!).with("foo")
14
+ @no.name = "foo"
15
+ end
16
+
17
+ context "when the name is #{"whatever".inspect}" do
18
+ it "sets the name" do
19
+ @no.name = "whatever"
20
+ @no.name.should == "whatever"
21
+ end
22
+ end
23
+ end
24
+
25
+ describe "#to_s" do
26
+ context "when the name is \"foo\"" do
27
+ before { @no.name = "foo" }
28
+
29
+ it("returns the \"foo\"") { @no.to_s.should == "foo" }
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe MTG::Set do
4
+
5
+
6
+
7
+ end
@@ -0,0 +1,6 @@
1
+ require 'rspec'
2
+ require 'mtg'
3
+
4
+ Rspec.configure do |c|
5
+ c.mock_with :rspec
6
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mtg
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Austin Schneider
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-02 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - "="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 2
30
+ - 0
31
+ - 1
32
+ version: 2.0.1
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Objects such as cards, colors, mana costs, etc relating to the Magic, The Gathering trading card game.
36
+ email: soccer022483@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README
43
+ files:
44
+ - README
45
+ - mtg.gemspec
46
+ - LICENSE
47
+ - example.rb
48
+ - lib/mtg/card/rarity.rb
49
+ - lib/mtg/card/base.rb
50
+ - lib/mtg/card/mana_cost.rb
51
+ - lib/mtg/color.rb
52
+ - lib/mtg/nameable.rb
53
+ - lib/mtg/card.rb
54
+ - lib/mtg/set.rb
55
+ - lib/mtg.rb
56
+ - spec/spec_helper.rb
57
+ - spec/mtg/card/mana_cost_spec.rb
58
+ - spec/mtg/card/rarity_spec.rb
59
+ - spec/mtg/card/base_spec.rb
60
+ - spec/mtg/set_spec.rb
61
+ - spec/mtg/nameable_spec.rb
62
+ has_rdoc: true
63
+ homepage:
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options: []
68
+
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ requirements: []
88
+
89
+ rubyforge_project:
90
+ rubygems_version: 1.3.7
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: An MTG gem
94
+ test_files: []
95
+