summoner 0.1.2 → 0.1.3

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
@@ -28,6 +28,17 @@ Also, you can override options with a hash:
28
28
 
29
29
  Summoner.summon(:monster, :type => "Avatar")
30
30
 
31
+ v0.1.2
32
+ ======
33
+ From v0.1.2 on, you can also summon ActiveRecord instances directly from their classes without prepare them before, for example:
34
+
35
+ Monster.summon do |m|
36
+ m.type = "Esper"
37
+ end
38
+
39
+ Note the difference between with the _prepare_ method, here you should use the *=* sign to assign values.
40
+
41
+
31
42
  Rails
32
43
  -----
33
44
 
@@ -1,3 +1,3 @@
1
1
  module Summoner
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/summoner.rb CHANGED
@@ -9,17 +9,17 @@ module Summoner
9
9
  def self.summon(name, attrs = {})
10
10
  raise UnpreparedBeastError unless @@beasts.has_key? name
11
11
 
12
- @@beasts[name].attributes = @@beasts[name].attributes.merge(attrs.symbolize_keys)
13
-
14
12
  klass = @@beasts[name].options.has_key?(:class) ? @@beasts[name].options[:class] : eval(name.to_s.capitalize)
15
13
 
16
- monster = klass.create(@@beasts[name].attributes)
14
+ monster = klass.create(@@beasts[name].attributes.merge(attrs.symbolize_keys))
17
15
 
18
16
  if block_given?
19
- yield @@beasts[name]
17
+ bicho = Beast.new
18
+ yield bicho
19
+ monster.update_attributes(monster.attributes.merge(bicho.attributes))
20
20
  end
21
- monster.update_attributes(@@beasts[name].attributes)
22
- monster.save(false)
21
+
22
+ monster.save(:validate => false)
23
23
  monster.reload
24
24
  monster
25
25
  end
data/spec/spec_helper.rb CHANGED
@@ -29,6 +29,9 @@ class Monster
29
29
  def reload
30
30
  return true
31
31
  end
32
+ def attributes
33
+ {:kind => kind, :power => power}
34
+ end
32
35
  end
33
36
 
34
37
  class Esper < Monster
@@ -0,0 +1,31 @@
1
+ require File.dirname(File.expand_path(__FILE__)) + '/spec_helper'
2
+ require File.dirname(File.expand_path(__FILE__)) + '/../lib/summoner'
3
+
4
+ class Vampire < Monster
5
+ attr_accessor :name
6
+ end
7
+
8
+
9
+ describe Vampire do
10
+ before :all do
11
+ Summoner.prepare :vampire do |m|
12
+ m.name "Dracula"
13
+ end
14
+ end
15
+
16
+ it "should override attributes only once" do
17
+ edward = Summoner.summon :vampire, :name => "Edward"
18
+ edward.name.should == "Edward"
19
+ vlad = Summoner.summon :vampire
20
+ vlad.name.should == "Dracula"
21
+ end
22
+
23
+ it "should override attributes only once when a block is given" do
24
+ edward = Summoner.summon :vampire do |v|
25
+ v.name "Edward"
26
+ end
27
+ edward.name.should == "Edward"
28
+ vlad = Summoner.summon :vampire
29
+ vlad.name.should == "Dracula"
30
+ end
31
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 2
9
- version: 0.1.2
8
+ - 3
9
+ version: 0.1.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Leonardo Mateo
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-18 00:00:00 -03:00
17
+ date: 2011-01-15 00:00:00 -03:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -42,6 +42,7 @@ files:
42
42
  - spec/db/summoner.sqlite3
43
43
  - spec/spec_helper.rb
44
44
  - spec/summoner_spec.rb
45
+ - spec/vampires_spec.rb
45
46
  - summoner.gemspec
46
47
  has_rdoc: true
47
48
  homepage: ""
@@ -79,3 +80,4 @@ test_files:
79
80
  - spec/db/summoner.sqlite3
80
81
  - spec/spec_helper.rb
81
82
  - spec/summoner_spec.rb
83
+ - spec/vampires_spec.rb