Class: Heuristics::Builder
- Inherits:
-
Object
- Object
- Heuristics::Builder
- Defined in:
- lib/heuristics/builder.rb
Instance Attribute Summary collapse
-
#assumptions ⇒ Object
readonly
Returns the value of attribute assumptions.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
Instance Method Summary collapse
- #assume(type, &block) ⇒ Object
- #assume_default(type) ⇒ Object
- #check(value) ⇒ Object
-
#initialize ⇒ Builder
constructor
A new instance of Builder.
Constructor Details
#initialize ⇒ Builder
Returns a new instance of Builder.
7 8 9 |
# File 'lib/heuristics/builder.rb', line 7 def initialize @assumptions = {} end |
Instance Attribute Details
#assumptions ⇒ Object (readonly)
Returns the value of attribute assumptions.
5 6 7 |
# File 'lib/heuristics/builder.rb', line 5 def assumptions @assumptions end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
5 6 7 |
# File 'lib/heuristics/builder.rb', line 5 def default @default end |
Instance Method Details
#assume(type, &block) ⇒ Object
11 12 13 14 |
# File 'lib/heuristics/builder.rb', line 11 def assume(type, &block) raise "An assumption with the name '#{type}' already exists" unless @assumptions[type].nil? @assumptions[type] = Proc.new(&block) end |
#assume_default(type) ⇒ Object
24 25 26 |
# File 'lib/heuristics/builder.rb', line 24 def assume_default(type) @default = type end |
#check(value) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/heuristics/builder.rb', line 16 def check(value) context = OpenStruct.new(value: value) @assumptions.map do |type, prok| (context.instance_eval(&prok) ? type : default) rescue default end.reject(&:nil?).first end |