Class: Dogfood::Pool
- Inherits:
-
Object
- Object
- Dogfood::Pool
- Defined in:
- lib/dogfood/pool.rb
Overview
A data pool. Wraps either YAML-defined entries or an existing Ruby module (e.g. Pools::Customers) behind a uniform interface consumed by the expression evaluator: $poolpool.NAMEpool.NAME.random, $poolpool.NAMEpool.NAME.sample(3).
Defined Under Namespace
Classes: CatView
Constant Summary collapse
- UnknownCategory =
Class.new(StandardError)
- UnknownPool =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#all ⇒ Object
readonly
Returns the value of attribute all.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #category(name) ⇒ Object
-
#initialize(name, entries: nil, categorized: nil, ruby_module: nil) ⇒ Pool
constructor
A new instance of Pool.
- #random(rng) ⇒ Object
- #sample(count, rng) ⇒ Object
Constructor Details
#initialize(name, entries: nil, categorized: nil, ruby_module: nil) ⇒ Pool
Returns a new instance of Pool.
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/dogfood/pool.rb', line 38 def initialize(name, entries: nil, categorized: nil, ruby_module: nil) @name = name @entries = entries @categorized = categorized @ruby_module = ruby_module @all = if @entries @entries.map { |e| strip_weight(e) } elsif @categorized @categorized.values.flatten.map { |e| strip_weight(e) } else [] end end |
Instance Attribute Details
#all ⇒ Object (readonly)
Returns the value of attribute all.
11 12 13 |
# File 'lib/dogfood/pool.rb', line 11 def all @all end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/dogfood/pool.rb', line 11 def name @name end |
Class Method Details
.from_module(mod) ⇒ Object
23 24 25 26 |
# File 'lib/dogfood/pool.rb', line 23 def self.from_module(mod) name = mod.name.split("::").last.to_sym new(name, ruby_module: mod) end |
.from_yaml(name, yaml) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/dogfood/pool.rb', line 13 def self.from_yaml(name, yaml) categorized = yaml["categorized"] || yaml["categories"] if categorized categories = yaml["categories"].transform_keys(&:to_sym) new(name, categorized: categories) else new(name, entries: normalize_entries(yaml["entries"] || [])) end end |
.normalize_entries(entries) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/dogfood/pool.rb', line 28 def self.normalize_entries(entries) entries.map do |e| if e.is_a?(Hash) e.transform_keys(&:to_sym) else { value: e } end end end |
Instance Method Details
#category(name) ⇒ Object
69 70 71 72 73 |
# File 'lib/dogfood/pool.rb', line 69 def category(name) raise UnknownCategory, "pool #{@name} has no category #{name}" unless @categorized && @categorized.key?(name) CatView.new(self, name) end |
#random(rng) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/dogfood/pool.rb', line 52 def random(rng) if @ruby_module @ruby_module.random(rng) elsif @categorized cat = rng.pick(@categorized.keys) { category: cat, value: rng.pick(@categorized[cat]) } elsif weighted? weighted_pick(rng) else strip_weight(rng.pick(@entries)) end end |
#sample(count, rng) ⇒ Object
65 66 67 |
# File 'lib/dogfood/pool.rb', line 65 def sample(count, rng) Array.new(count) { random(rng) } end |