summoner 0.0.1 → 0.1.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/README CHANGED
@@ -1,7 +1,12 @@
1
1
  Summoner
2
2
  ========
3
3
 
4
- Summoner is a really simple fixture replacement for Ruby.
4
+ Summoner is a really simple fixture replacemoent for Ruby.
5
+
6
+ How to install
7
+ --------------
8
+ gem install summoner
9
+
5
10
 
6
11
  You can _prepare_ your _monsters_ like:
7
12
 
@@ -22,3 +27,11 @@ end
22
27
  Also, you can override options with a hash:
23
28
 
24
29
  Summoner.summon(:monster, :type => "Avatar")
30
+
31
+ Rails
32
+ -----
33
+
34
+ 1. Add config.gem "summoner" to the config/environments/test.rb or gem "summoner" to your Gemfile if you're using bundler
35
+ 2. Define your _monsters_ on a file, e.g: spec/monsters.rb (you can call it as you please)
36
+ 3. Require that file on your (test|spec)_helper.rb
37
+ 4. Summon your _monsters_ on your tests/specs
@@ -0,0 +1,8 @@
1
+ class Hash
2
+ def symbolize_keys
3
+ inject({}) do |options, (key, value)|
4
+ options[(key.to_sym rescue key) || key] = value
5
+ options
6
+ end
7
+ end
8
+ end
@@ -1,3 +1,3 @@
1
1
  module Summoner
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/summoner.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'summoner/beast'
2
2
  require 'summoner/definition_duplicated_error'
3
3
  require 'summoner/unprepared_beast_error'
4
+ require 'summoner/hash'
4
5
 
5
6
  module Summoner
6
7
  @@beasts = {}
@@ -8,7 +9,7 @@ module Summoner
8
9
  def self.summon(name, attrs = {})
9
10
  raise UnpreparedBeastError unless @@beasts.has_key? name
10
11
 
11
- @@beasts[name].attributes = @@beasts[name].attributes.merge(attrs)
12
+ @@beasts[name].attributes = @@beasts[name].attributes.merge(attrs.symbolize_keys)
12
13
 
13
14
  klass = @@beasts[name].options.has_key?(:class) ? @@beasts[name].options[:class] : eval(name.to_s.capitalize)
14
15
 
@@ -18,6 +19,8 @@ module Summoner
18
19
  yield @@beasts[name]
19
20
  end
20
21
  monster.update_attributes(@@beasts[name].attributes)
22
+ monster.save(false)
23
+ monster.reload
21
24
  monster
22
25
  end
23
26
 
@@ -32,4 +35,19 @@ module Summoner
32
35
  @@beasts.clear
33
36
  end
34
37
 
38
+
39
+ module ClassMethods
40
+ def summon(options = {}, &block)
41
+ begin
42
+ object = self.create(options)
43
+ yield object if block_given?
44
+ object.save(false)
45
+ object
46
+ rescue ArgumentError
47
+ puts "You're probably summoning something by itself. Try defining attributes as attr = value"
48
+ end
49
+ end
50
+ end
35
51
  end
52
+
53
+ ActiveRecord::Base.extend(Summoner::ClassMethods) if defined? ActiveRecord
Binary file
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,10 @@
1
+ require 'active_record'
2
+
3
+ ActiveRecord::Base.establish_connection(
4
+ :adapter => "sqlite3",
5
+ :database => File.dirname(File.expand_path(__FILE__)) + "/db/summoner.sqlite3"
6
+ )
7
+
1
8
  class Monster
2
9
  attr_accessor :kind
3
10
  attr_accessor :power
@@ -16,7 +23,21 @@ class Monster
16
23
  end
17
24
  true
18
25
  end
26
+ def save(validate=true)
27
+ return true
28
+ end
29
+ def reload
30
+ return true
31
+ end
19
32
  end
20
33
 
21
34
  class Esper < Monster
22
35
  end
36
+
37
+ module Monsters
38
+ class Avatar < Monster
39
+ end
40
+ end
41
+
42
+ class Power < ActiveRecord::Base;end
43
+ class Aeon < ActiveRecord::Base;end
@@ -1,5 +1,5 @@
1
- require File.dirname(File.expand_path(__FILE__)) + '/../lib/summoner'
2
1
  require File.dirname(File.expand_path(__FILE__)) + '/spec_helper'
2
+ require File.dirname(File.expand_path(__FILE__)) + '/../lib/summoner'
3
3
 
4
4
  describe Summoner do
5
5
 
@@ -17,9 +17,9 @@ describe Summoner do
17
17
  it "should prepare a monster" do
18
18
  beast = Summoner.prepare :esper do |s|
19
19
  s.power 6
20
- end
20
+ end
21
21
  beast.should be_a Summoner::Beast
22
- end
22
+ end
23
23
 
24
24
  it "should summon a previously prepared monster" do
25
25
 
@@ -58,4 +58,41 @@ describe Summoner do
58
58
  monster.kind.should == "Aeon"
59
59
  end
60
60
 
61
+ it "should pepare a monster with a custom class" do
62
+ beast = Summoner.prepare :avatar, :class => Monster do |a|
63
+ a.kind "Avatar"
64
+ end
65
+
66
+ beast.should be_a Summoner::Beast
67
+
68
+ avatar = Summoner.summon(:avatar)
69
+
70
+ avatar.should be_a Monster
71
+ avatar.kind.should == "Avatar"
72
+ end
73
+
74
+ it "should prepare a monster with a custom namespaced class" do
75
+ beast = Summoner.prepare :avatar, :class => Monsters::Avatar do |a|
76
+ a.kind "Monster avatar"
77
+ end
78
+
79
+ beast.should be_a Summoner::Beast
80
+
81
+ avatar = Summoner.summon(:avatar)
82
+ avatar.should be_a Monsters::Avatar
83
+ avatar.kind.should == "Monster avatar"
84
+ end
85
+
86
+ it "should add summon ability to AR" do
87
+ creature = Aeon.summon do |a|
88
+ a.name = "Flux"
89
+ a.power = 10
90
+ end
91
+
92
+ creature.should_not be_nil
93
+ creature.should be_a Aeon
94
+ creature.name.should == "Flux"
95
+ creature.power.should == 10
96
+ end
97
+
61
98
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: summoner
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
- - 0
9
7
  - 1
10
- version: 0.0.1
8
+ - 2
9
+ version: 0.1.2
11
10
  platform: ruby
12
11
  authors:
13
12
  - Leonardo Mateo
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-11-29 00:00:00 -03:00
17
+ date: 2010-12-18 00:00:00 -03:00
19
18
  default_executable:
20
19
  dependencies: []
21
20
 
@@ -37,14 +36,12 @@ files:
37
36
  - lib/summoner/attributes.rb
38
37
  - lib/summoner/beast.rb
39
38
  - lib/summoner/definition_duplicated_error.rb
39
+ - lib/summoner/hash.rb
40
40
  - lib/summoner/unprepared_beast_error.rb
41
41
  - lib/summoner/version.rb
42
- - lib/wizard.rb
43
- - lib/wizard/attributes.rb
44
- - lib/wizard/version.rb
42
+ - spec/db/summoner.sqlite3
45
43
  - spec/spec_helper.rb
46
44
  - spec/summoner_spec.rb
47
- - spec/wizard_spec.rb
48
45
  - summoner.gemspec
49
46
  has_rdoc: true
50
47
  homepage: ""
@@ -60,7 +57,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
60
57
  requirements:
61
58
  - - ">="
62
59
  - !ruby/object:Gem::Version
63
- hash: 3
64
60
  segments:
65
61
  - 0
66
62
  version: "0"
@@ -69,7 +65,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
65
  requirements:
70
66
  - - ">="
71
67
  - !ruby/object:Gem::Version
72
- hash: 3
73
68
  segments:
74
69
  - 0
75
70
  version: "0"
@@ -81,6 +76,6 @@ signing_key:
81
76
  specification_version: 3
82
77
  summary: Simple Ruby fixtures replacement.
83
78
  test_files:
79
+ - spec/db/summoner.sqlite3
84
80
  - spec/spec_helper.rb
85
81
  - spec/summoner_spec.rb
86
- - spec/wizard_spec.rb
@@ -1,13 +0,0 @@
1
- module Wizard
2
- class Attributes < Hash
3
- def method_missing(name, *args, &block)
4
- if name == :has_one
5
- self[args.first] = Wizard.invoke args.first
6
- elsif name == :has_many
7
- self[args.first] = [Wizard.invoke(args.first)]
8
- else
9
- self[name.to_sym] = args
10
- end
11
- end
12
- end
13
- end
@@ -1,3 +0,0 @@
1
- module Wizard
2
- VERSION = "0.0.1"
3
- end
data/lib/wizard.rb DELETED
@@ -1,46 +0,0 @@
1
- require 'wizard/attributes'
2
-
3
- module Wizard
4
- class DefinitionDuplicatedError < RuntimeError
5
- end
6
-
7
- class UnpreparedSpellError < RuntimeError
8
- end
9
-
10
- @@attributes = Attributes.new
11
- @@spells = {}
12
-
13
- def self.attributes
14
- @@attributes
15
- end
16
-
17
- def self.invoke(name, attrs = {})
18
- raise UnpreparedSpellError unless self.attributes.has_key? name
19
-
20
- if @@spells.has_key? :name
21
- spell = @spells[:name]
22
- else
23
- new_attrs = self.attributes[name].clone.merge(attrs)
24
- prepared_options = new_attrs.delete(:options)
25
-
26
- klass = prepared_options.has_key?(:class) ? prepared_options[:class] : eval(name.to_s.capitalize)#.constantize
27
-
28
- spell = klass.create(new_attrs)
29
- end
30
- if block_given?
31
- yield new_attrs
32
- spell.update_attributes(new_attrs)
33
- end
34
- self.attributes[name].merge(new_attrs)
35
- @@spells[name] = spell
36
- end
37
-
38
- def self.prepare(name, options = {}, &block)
39
- key = name.to_sym
40
- raise DefinitionDuplicatedError if self.attributes.has_key? key
41
- self.attributes[key] = Attributes.new
42
- self.attributes[key][:options] = options
43
- yield self.attributes[key]
44
- end
45
-
46
- end
data/spec/wizard_spec.rb DELETED
@@ -1,55 +0,0 @@
1
- require File.dirname(File.expand_path(__FILE__)) + '/../lib/wizard'
2
- require File.dirname(File.expand_path(__FILE__)) + '/spec_helper'
3
-
4
- describe Wizard do
5
-
6
- before :all do
7
- Wizard.prepare :conjure do |c|
8
- c.name "Turn into a toad"
9
- end
10
- end
11
-
12
- it "should prepare a spell" do
13
- attributes = Wizard.prepare :spell do |s|
14
- s.name "Small Fireball"
15
- s.level 1
16
- end
17
- attributes.should be_a Array
18
- end
19
-
20
- it "should invoke a previously prepared spell" do
21
-
22
- conjure = Wizard.invoke :conjure
23
-
24
- conjure.should_not be_nil
25
- conjure.should be_a Conjure
26
- conjure.name.should == "Turn into a toad"
27
- end
28
-
29
- it "should invoke a prepared spell with custom name" do
30
- conjure = Wizard.invoke :conjure do |c|
31
- c.name "Turn back to knight"
32
- end
33
-
34
- conjure.should_not be_nil
35
- conjure.should be_a Conjure
36
- conjure.name.should == "Turn back to knight"
37
- end
38
-
39
- it "should override attributes with a hash" do
40
- conjure = Wizard.invoke(:conjure, :name => "Turn into a Camel")
41
- conjure.should_not be nil
42
- conjure.name.should == "Turn into a Camel"
43
- end
44
-
45
- it "should change attributes on the second invoke" do
46
- conjure = Wizard.invoke(:conjure)
47
- conjure.should_not be_nil
48
- conjure.name.should == "Turn into a toad"
49
-
50
- conjure = Wizard.invoke(:conjure, :name => "Turn into a snake")
51
- conjure.should_not be_nil
52
- conjure.name.should == "Turn into a snake"
53
- end
54
-
55
- end