summoner 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ *.lock
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in wizard.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem "rspec"
8
+ end
data/README ADDED
@@ -0,0 +1,24 @@
1
+ Summoner
2
+ ========
3
+
4
+ Summoner is a really simple fixture replacement for Ruby.
5
+
6
+ You can _prepare_ your _monsters_ like:
7
+
8
+ Summoner.prepare :monster do |c|
9
+ c.type "Esper"
10
+ end
11
+
12
+ Then, you can _summon_ your _monsters_ like:
13
+
14
+ Summoner.summon :monster
15
+
16
+ Or, if you need to override default values:
17
+
18
+ Summoner.summon :monster do |c|
19
+ c.type "Guardian"
20
+ end
21
+
22
+ Also, you can override options with a hash:
23
+
24
+ Summoner.summon(:monster, :type => "Avatar")
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,13 @@
1
+ module Summoner
2
+ class Attributes < Hash
3
+ def method_missing(name, *args, &block)
4
+ if name == :has_one
5
+ self[args.first] = Summoner.invoke args.first
6
+ elsif name == :has_many
7
+ self[args.first] = [Summoner.invoke(args.first)]
8
+ else
9
+ self[name.to_sym] = args
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,22 @@
1
+ module Summoner
2
+ class Beast
3
+ attr_accessor :attributes
4
+ attr_accessor :options
5
+
6
+ def initialize(options = {}, attributes = {})
7
+ self.options = options
8
+ self.attributes = attributes
9
+ end
10
+
11
+ def method_missing(name, *args, &block)
12
+ if name == :has_one
13
+ self.attributes[args.first] = Summoner.invoke args.first
14
+ elsif name == :has_many
15
+ self.attributes[args.first] = [Summoner.invoke(args.first)]
16
+ else
17
+ self.attributes[name.to_sym] = args.first
18
+ end
19
+ self
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ class DefinitionDuplicatedError < RuntimeError
2
+ end
3
+
@@ -0,0 +1,2 @@
1
+ class UnpreparedBeastError < RuntimeError
2
+ end
@@ -0,0 +1,3 @@
1
+ module Summoner
2
+ VERSION = "0.0.1"
3
+ end
data/lib/summoner.rb ADDED
@@ -0,0 +1,35 @@
1
+ require 'summoner/beast'
2
+ require 'summoner/definition_duplicated_error'
3
+ require 'summoner/unprepared_beast_error'
4
+
5
+ module Summoner
6
+ @@beasts = {}
7
+
8
+ def self.summon(name, attrs = {})
9
+ raise UnpreparedBeastError unless @@beasts.has_key? name
10
+
11
+ @@beasts[name].attributes = @@beasts[name].attributes.merge(attrs)
12
+
13
+ klass = @@beasts[name].options.has_key?(:class) ? @@beasts[name].options[:class] : eval(name.to_s.capitalize)
14
+
15
+ monster = klass.create(@@beasts[name].attributes)
16
+
17
+ if block_given?
18
+ yield @@beasts[name]
19
+ end
20
+ monster.update_attributes(@@beasts[name].attributes)
21
+ monster
22
+ end
23
+
24
+ def self.prepare(name, options = {}, &block)
25
+ key = name.to_sym
26
+ raise DefinitionDuplicatedError if @@beasts.has_key? key
27
+ @@beasts[key] = Beast.new(options)
28
+ yield @@beasts[key]
29
+ end
30
+
31
+ def self.reset
32
+ @@beasts.clear
33
+ end
34
+
35
+ end
@@ -0,0 +1,13 @@
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
@@ -0,0 +1,3 @@
1
+ module Wizard
2
+ VERSION = "0.0.1"
3
+ end
data/lib/wizard.rb ADDED
@@ -0,0 +1,46 @@
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
@@ -0,0 +1,22 @@
1
+ class Monster
2
+ attr_accessor :kind
3
+ attr_accessor :power
4
+
5
+ def self.create(*args)
6
+ @object = self.new
7
+ args.first.each do |k, v|
8
+ @object.send("#{k.to_s}=", v)
9
+ end
10
+ @object
11
+ end
12
+
13
+ def update_attributes (*args)
14
+ args.first.each do |k, v|
15
+ self.send("#{k.to_s}=", v)
16
+ end
17
+ true
18
+ end
19
+ end
20
+
21
+ class Esper < Monster
22
+ end
@@ -0,0 +1,61 @@
1
+ require File.dirname(File.expand_path(__FILE__)) + '/../lib/summoner'
2
+ require File.dirname(File.expand_path(__FILE__)) + '/spec_helper'
3
+
4
+ describe Summoner do
5
+
6
+ before :each do
7
+ Summoner.prepare :monster do |m|
8
+ m.kind "Guardian"
9
+ m.power 5
10
+ end
11
+ end
12
+
13
+ after :each do
14
+ Summoner.reset
15
+ end
16
+
17
+ it "should prepare a monster" do
18
+ beast = Summoner.prepare :esper do |s|
19
+ s.power 6
20
+ end
21
+ beast.should be_a Summoner::Beast
22
+ end
23
+
24
+ it "should summon a previously prepared monster" do
25
+
26
+ monster = Summoner.summon :monster
27
+
28
+ monster.should_not be_nil
29
+ monster.should be_a Monster
30
+ monster.kind.should == "Guardian"
31
+ end
32
+
33
+ it "should summon a prepared monster with custom kind" do
34
+ monster = Summoner.summon :monster do |m|
35
+ m.kind "Esper"
36
+ m.power 10
37
+ end
38
+
39
+ monster.should_not be_nil
40
+ monster.should be_a Monster
41
+ monster.kind.should == "Esper"
42
+ monster.power.should == 10
43
+ end
44
+
45
+ it "should override attributes with a hash" do
46
+ monster = Summoner.summon(:monster, :kind => "Avatar")
47
+ monster.should_not be nil
48
+ monster.kind.should == "Avatar"
49
+ end
50
+
51
+ it "should change attributes on the second summon" do
52
+ monster = Summoner.summon(:monster)
53
+ monster.should_not be_nil
54
+ monster.kind.should == "Guardian"
55
+
56
+ monster = Summoner.summon(:monster, :kind => "Aeon")
57
+ monster.should_not be_nil
58
+ monster.kind.should == "Aeon"
59
+ end
60
+
61
+ end
@@ -0,0 +1,55 @@
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
data/summoner.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "summoner/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "summoner"
7
+ s.version = Summoner::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Leonardo Mateo"]
10
+ s.email = ["leonardomateo@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Simple Ruby fixtures replacement.}
13
+ s.description = %q{Simple Ruby Fixtures replacement like Factory Girl, and many others, but based on simplicity.}
14
+
15
+ s.rubyforge_project = "summoner"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: summoner
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Leonardo Mateo
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-29 00:00:00 -03:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Simple Ruby Fixtures replacement like Factory Girl, and many others, but based on simplicity.
23
+ email:
24
+ - leonardomateo@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - README
35
+ - Rakefile
36
+ - lib/summoner.rb
37
+ - lib/summoner/attributes.rb
38
+ - lib/summoner/beast.rb
39
+ - lib/summoner/definition_duplicated_error.rb
40
+ - lib/summoner/unprepared_beast_error.rb
41
+ - lib/summoner/version.rb
42
+ - lib/wizard.rb
43
+ - lib/wizard/attributes.rb
44
+ - lib/wizard/version.rb
45
+ - spec/spec_helper.rb
46
+ - spec/summoner_spec.rb
47
+ - spec/wizard_spec.rb
48
+ - summoner.gemspec
49
+ has_rdoc: true
50
+ homepage: ""
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project: summoner
79
+ rubygems_version: 1.3.7
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Simple Ruby fixtures replacement.
83
+ test_files:
84
+ - spec/spec_helper.rb
85
+ - spec/summoner_spec.rb
86
+ - spec/wizard_spec.rb