factis 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -49,8 +49,25 @@ That being the case, I'm going to use terminology from that side of the
49
49
  experience. Here's everything that World gets when you use Factis.
50
50
 
51
51
  ```ruby
52
- # Let's memorize that my brother, Joe, just loves pie.
52
+ # Let's memorize that my brother, Joe, just loves cake.
53
+ memorize_fact("What does Joe love?", "Joe loves cake.")
54
+ => "Joe loves cake."
55
+
56
+ # Wait, I had that wrong. Joe loves pie.
57
+
53
58
  memorize_fact("What does Joe love?", "Joe loves pie.")
59
+ => Cannot memorize a fact more than once: 'What does Joe love?' (RuntimeError)
60
+
61
+ # Why does that happen?
62
+ # Er, because we're trying to track state safely, and overwriting states
63
+ # all willy-nilly is kinda unsafe.
64
+ # So, how do I overwrite a known fact?
65
+ # If you want to unsafely track state, you should probably just use global
66
+ # variables. Shooting from the hip never did anybody wrong, after all. Or, you
67
+ # can use indifferently_memorize_fact, which is a bad thing to do.
68
+
69
+ indifferently_memorize_fact("What does Joe love?", "Joe loves pie.")
70
+ => "Joe loves pie."
54
71
 
55
72
  # Hey Factis, what does Joe love?
56
73
  recall_fact("What does Joe love?")
@@ -58,6 +75,7 @@ recall_fact("What does Joe love?")
58
75
 
59
76
  # That's not very interesting. Let's forget about it.
60
77
  forget_fact("What does Joe love?")
78
+ => "Joe loves pie."
61
79
 
62
80
  # Wait, what does Joe love again?
63
81
  recall_fact("What does Joe love?")
@@ -79,6 +97,7 @@ end
79
97
 
80
98
  # The content can be pretty much anything.
81
99
  memorize_fact(:some_foo, Foo.new)
100
+ => #<Foo:0x007ffd84394728>
82
101
 
83
102
  # What's a foo?
84
103
  recall_fact(:some_foo)
@@ -14,6 +14,10 @@ module Factis
14
14
  Factis::Memory.memorize(fact, content)
15
15
  end
16
16
 
17
+ def indifferently_memorize_fact(fact, content)
18
+ Factis::Memory.memorize(fact, content, :overwrite => true)
19
+ end
20
+
17
21
  def recall_fact(fact)
18
22
  Factis::Memory.recall(fact)
19
23
  end
@@ -10,8 +10,13 @@ module Factis
10
10
  @facts
11
11
  end
12
12
 
13
- def self.memorize(fact, content)
13
+ def self.memorize(fact, content, options = {})
14
14
  init_memory! if @facts.nil?
15
+ if known_fact?(fact)
16
+ unless options[:overwrite] == true
17
+ raise %{Cannot memorize a fact more than once: '#{fact}'}
18
+ end
19
+ end
15
20
  @facts[fact] = content
16
21
  end
17
22
 
@@ -1,3 +1,3 @@
1
1
  module Factis
2
- VERSION = '0.0.6'
2
+ VERSION = '0.0.7'
3
3
  end
@@ -7,6 +7,10 @@ describe Factis::Memory do
7
7
  let(:fact) {"Joe"}
8
8
  let(:content) {"Likes pie"}
9
9
 
10
+ before(:each) do
11
+ Factis::Memory.reset!
12
+ end
13
+
10
14
  describe '.all_facts' do
11
15
  it 'is a Hash' do
12
16
  memory.all_facts.should be_a(Hash)
@@ -14,11 +18,28 @@ describe Factis::Memory do
14
18
  end
15
19
 
16
20
  describe %{.memorize} do
21
+ let(:new_content) {'Something else'}
17
22
  it %{records a fact when given a name and some content} do
18
23
  memory.memorize(fact, content)
19
24
  memory.all_facts.keys.include?(fact).should be_true
20
25
  memory.all_facts[fact].should == content
21
26
  end
27
+
28
+ it %{returns the content of the fact} do
29
+ memory.memorize(fact, content).should == content
30
+ end
31
+
32
+ it %{raises an error when given a known fact} do
33
+ memory.memorize(fact, content)
34
+ lambda {memory.memorize(fact, new_content)}.should raise_error
35
+ end
36
+
37
+ it %{allows a fact to be explicitly over-written} do
38
+ memory.memorize(fact, content)
39
+ lambda {memory.memorize(fact, new_content, overwrite: true)}.should_not raise_error
40
+ memory.recall(fact).should == new_content
41
+
42
+ end
22
43
  end
23
44
 
24
45
  describe %{.known_fact?} do
@@ -40,13 +61,15 @@ describe Factis::Memory do
40
61
  memory.memorize(fact, content)
41
62
  end
42
63
 
43
-
44
-
45
64
  it %{removes a known fact from memory} do
46
65
  memory.forget(fact)
47
66
  memory.known_fact?(fact).should be_false
48
67
  end
49
68
 
69
+ it %{returns the content of the fact} do
70
+ memory.forget(fact).should == content
71
+ end
72
+
50
73
  it %{raises an error when given an unknown fact} do
51
74
  lambda {memory.forget("unknown fact")}.should raise_error
52
75
  end
@@ -7,6 +7,10 @@ describe Factis do
7
7
  let(:fact) {"Joe"}
8
8
  let(:content) {"likes pie"}
9
9
 
10
+ before(:each) do
11
+ Factis::Memory.reset!
12
+ end
13
+
10
14
  describe %{#clear_all_facts!} do
11
15
  it %{resets the Factis memory core} do
12
16
  Factis::Memory.should_receive(:reset!)
@@ -15,10 +19,13 @@ describe Factis do
15
19
  end
16
20
 
17
21
  describe %{#all_facts} do
18
- it %{returns the contents of the Factis memory core} do
22
+ it %{returns the entire fact hash} do
19
23
  facts = {fact => content}
20
24
  Factis::Memory.should_receive(:all_facts).and_return(facts)
21
- factis.all_facts.should == facts
25
+ factis.all_facts.tap do |all_facts|
26
+ all_facts.should be_a(Hash)
27
+ all_facts.should == facts
28
+ end
22
29
  end
23
30
  end
24
31
 
@@ -27,12 +34,30 @@ describe Factis do
27
34
  Factis::Memory.should_receive(:memorize).with(fact, content).and_call_original
28
35
  factis.memorize_fact(fact, content)
29
36
  end
37
+
38
+ it %{returns the content of the provided fact} do
39
+ factis.memorize_fact(fact, content).should == content
40
+ end
41
+
42
+ it %{raises an error if the fact is already known} do
43
+ factis.memorize_fact(fact, content)
44
+ lambda {factis.memorize_fact(fact, 'Something else')}.should raise_error
45
+ end
46
+ end
47
+
48
+ describe %{#indifferently_memorize_fact} do
49
+ it %{stores the provided fact, even if it's already known} do
50
+ new_content = 'Something else'
51
+ factis.memorize_fact(fact, content)
52
+ lambda {factis.indifferently_memorize_fact(fact, new_content)}.should_not raise_error
53
+ factis.recall_fact(fact).should == new_content
54
+ end
30
55
  end
31
56
 
32
57
  describe %{#recall_fact} do
33
58
  before(:each) {factis.memorize_fact(fact, content)}
34
59
 
35
- it %{recalls the provided fact if known} do
60
+ it %{returns the fact content if known} do
36
61
  Factis::Memory.should_receive(:recall).with(fact).and_call_original
37
62
  factis.recall_fact(fact).should == content
38
63
  end
@@ -51,6 +76,10 @@ describe Factis do
51
76
  factis.all_facts.keys.include?(fact).should_not be_true
52
77
  end
53
78
 
79
+ it %{returns the content of the fact} do
80
+ factis.forget_fact(fact).should == content
81
+ end
82
+
54
83
  it %{raises an error if the fact is not known} do
55
84
  lambda {factis.forget_fact(:unknown)}.should raise_error
56
85
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-04 00:00:00.000000000 Z
12
+ date: 2013-10-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec