factree 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c1acd242c9352248bdd755e245c55e026d16cdc0
4
- data.tar.gz: b888a69e8cd08b85e1d3177e2b38b5c3f8cda78c
3
+ metadata.gz: ec4b10d80f3e3fda0f49c30fed2b96130b706d34
4
+ data.tar.gz: 7f3679094a62023d8f43af067177d43b38f5b76e
5
5
  SHA512:
6
- metadata.gz: cf47060ea361430911941b26da4cef20bb527847c9f0efd522f3bac3b32d7fe94dc56d750dc91f1947598d6e6aa07bcb8573d6a462a350af2c9c17f42782795a
7
- data.tar.gz: 42455c60d1d5b9ddb10a0d50cd6a7c2d4847e855453a60c9f1058bce56327dc1469684e5358c243d36db5513779ef33d4a0476ab852cec54c021038f8a4d96f2
6
+ metadata.gz: f9465dfadd4267524ca50b0467bd15d6c4e4df965197ad6357f0f30274421df924e7f6ae2e1e2b07eb3486c015802b1b5847319f18997773fb9cba836f5d07b9
7
+ data.tar.gz: b5310e3f4992629a79b3b781d602cfb47dfe218cc81200867aa3c3d23936f6d81b489f6514303a0c8290af8f352e4e68e8b54bc588ff697f5684425e54cf6c58
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Factree
2
2
  [![Gem Version](https://badge.fury.io/rb/factree.svg)](https://rubygems.org/gems/factree)
3
- [![Build Status](https://travis-ci.org/jstrater/factree.svg?branch=master)](https://travis-ci.org/jstrater/factree)
3
+ [![Build Status](https://travis-ci.org/ConsultingMD/factree.svg?branch=master)](https://travis-ci.org/ConsultingMD/factree)
4
4
 
5
5
  Factree provides tools for making choices based on a set of facts that are not yet known. You write a decision function that takes a set facts and returns a conclusion. Factree will run your function and make sure it has all of the facts it needs to complete. If it doesn't, then Factree will tell you what's needed to continue.
6
6
 
@@ -22,7 +22,7 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- [API documentation](http://www.rubydoc.info/github/jstrater/factree/)
25
+ [API documentation](http://www.rubydoc.info/gems/factree)
26
26
 
27
27
  ## Development
28
28
 
@@ -32,7 +32,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
32
 
33
33
  ## Contributing
34
34
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/jstrater/factree.
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ConsultingMD/factree.
36
36
 
37
37
  ## License
38
38
 
data/factree.gemspec CHANGED
@@ -7,10 +7,10 @@ Gem::Specification.new do |spec|
7
7
  spec.name = "factree"
8
8
  spec.version = Factree::VERSION
9
9
  spec.authors = ["Josh Strater"]
10
- spec.email = ["jstrater@gmail.com"]
10
+ spec.email = ["josh.strater@grandrounds.com"]
11
11
 
12
12
  spec.summary = %q{Decision trees that request facts as needed}
13
- spec.homepage = "https://github.com/jstrater/factree"
13
+ spec.homepage = "https://github.com/ConsultingMD/factree"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
data/lib/factree/dsl.rb CHANGED
@@ -1,7 +1,3 @@
1
- require 'factree/conclusion'
2
- require 'factree/pathfinder'
3
- require 'factree/aggregate'
4
-
5
1
  # Readable shortcuts to common functions.
6
2
  module Factree::DSL
7
3
  # Creates a {Conclusion} to return from a decision proc.
@@ -0,0 +1,105 @@
1
+ module Factree
2
+ # Mixin to help define fact source classes.
3
+ module FactSource
4
+ UndefinedFactError = Class.new(StandardError)
5
+ UnknownFactError = Class.new(StandardError)
6
+
7
+ def self.included(base)
8
+ base.extend(ClassMethods)
9
+ end
10
+
11
+ module ClassMethods
12
+ # Defines a named fact along with a block used to compute the fact's value.
13
+ #
14
+ # @param [Symbol] fact_name The new fact's name
15
+ def def_fact(fact_name, &block)
16
+ raise ArgumentError, "block required" unless block_given?
17
+
18
+ all_fact_procs = fact_procs.merge(fact_name => block).freeze
19
+ class_variable_set(:@@_fact_procs, all_fact_procs)
20
+ fact_name
21
+ end
22
+
23
+ # @return [Array<Symbol>] Names for all of the defined facts, whether their values are known or not.
24
+ def fact_names
25
+ fact_procs.keys.freeze
26
+ end
27
+
28
+ # @api private
29
+ def fact_procs
30
+ class_variable_defined?(:@@_fact_procs) ?
31
+ class_variable_get(:@@_fact_procs) :
32
+ {}.freeze
33
+ end
34
+
35
+ # @return [Boolean] True if the fact has been defined (using {FactSource.def_fact})
36
+ def defined?(fact_name)
37
+ fact_procs.include? fact_name
38
+ end
39
+ end
40
+
41
+ # Checks to see if the value of the fact is known.
42
+ def known?(fact_name)
43
+ fetch(fact_name) { return false }
44
+ true
45
+ end
46
+
47
+ # Returns the value of the fact, or nil if the value is unknown
48
+ def [](fact_name)
49
+ fetch(fact_name) { nil }
50
+ end
51
+
52
+ # Returns the value of the fact.
53
+ #
54
+ # If the value is unknown, then the block will be called with the name of the fact. If no block is supplied, then an UnknownFactError will be raised.
55
+ def fetch(fact_name, &block)
56
+ ensure_defined fact_name
57
+ fact_proc = self.class.fact_procs[fact_name]
58
+
59
+ fact_value = nil
60
+ fact_known = catch(UNKNOWN_FACT) do
61
+ fact_value = instance_eval(&fact_proc)
62
+ true
63
+ end
64
+ return fact_value if fact_known
65
+
66
+ fetch_unknown(fact_name, &block)
67
+ end
68
+
69
+ # @api private
70
+ def ensure_defined(fact_name)
71
+ unless self.class.defined? fact_name
72
+ raise UndefinedFactError, "undefined fact referenced: #{fact_name}"
73
+ end
74
+ end
75
+
76
+ # @api private
77
+ def fetch_unknown(fact_name)
78
+ return yield(fact_name) if block_given?
79
+
80
+ raise UnknownFactError, "unknown fact: #{fact_name}"
81
+ end
82
+
83
+ # A hash mapping all of the known fact names to values.
84
+ def to_h
85
+ self.class.fact_procs.flat_map { |fact_name, _fact_proc|
86
+ fact_known = true
87
+ fact_value = fetch(fact_name) { fact_known = false }
88
+
89
+ fact_known ? [[fact_name, fact_value]] : []
90
+ }.to_h
91
+ end
92
+
93
+ # Takes several FactSources and returns a single hash containing all of their facts mixed together.
94
+ def self.to_combined_h(*sources)
95
+ sources.map(&:to_h).inject({}, &:merge)
96
+ end
97
+
98
+ # Calling this method in a fact proc will signal that the fact's value is unknown.
99
+ def unknown
100
+ throw UNKNOWN_FACT
101
+ end
102
+
103
+ UNKNOWN_FACT = "Attempted to determine the value of a fact that is unknown".freeze
104
+ end
105
+ end
@@ -1,7 +1,3 @@
1
- require 'factree/path'
2
- require 'factree/facts'
3
- require 'factree/facts_spy'
4
-
5
1
  module Factree
6
2
  # Raised when a decision proc fails to return a Conclusion as expected
7
3
  InvalidConclusionError = Class.new(StandardError)
@@ -1,3 +1,3 @@
1
1
  module Factree
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/factree.rb CHANGED
@@ -1,9 +1,13 @@
1
- require "factree/version"
2
- require "factree/conclusion"
3
- require "factree/path"
4
- require "factree/aggregate"
5
- require "factree/dsl"
6
-
7
1
  module Factree
2
+ autoload :Aggregate, "factree/aggregate"
3
+ autoload :Conclusion, "factree/conclusion"
4
+ autoload :DSL, "factree/dsl"
5
+ autoload :FactSource, "factree/fact_source"
6
+ autoload :Facts, "factree/facts"
7
+ autoload :FactsSpy, "factree/facts_spy"
8
+ autoload :Path, "factree/path"
9
+ autoload :Pathfinder, "factree/pathfinder"
10
+ autoload :VERSION, "factree/version"
11
+
8
12
  extend DSL
9
13
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Strater
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-26 00:00:00.000000000 Z
11
+ date: 2017-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -82,7 +82,7 @@ dependencies:
82
82
  version: '0.9'
83
83
  description:
84
84
  email:
85
- - jstrater@gmail.com
85
+ - josh.strater@grandrounds.com
86
86
  executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
@@ -102,12 +102,13 @@ files:
102
102
  - lib/factree/aggregate.rb
103
103
  - lib/factree/conclusion.rb
104
104
  - lib/factree/dsl.rb
105
+ - lib/factree/fact_source.rb
105
106
  - lib/factree/facts.rb
106
107
  - lib/factree/facts_spy.rb
107
108
  - lib/factree/path.rb
108
109
  - lib/factree/pathfinder.rb
109
110
  - lib/factree/version.rb
110
- homepage: https://github.com/jstrater/factree
111
+ homepage: https://github.com/ConsultingMD/factree
111
112
  licenses:
112
113
  - MIT
113
114
  metadata: {}