Class: Synthra::Personas::Persona
- Inherits:
-
Object
- Object
- Synthra::Personas::Persona
- Defined in:
- lib/synthra/personas.rb
Overview
A persona definition
Instance Attribute Summary collapse
-
#generation_mode ⇒ Object
readonly
Returns the value of attribute generation_mode.
-
#generation_seed ⇒ Object
readonly
Returns the value of attribute generation_seed.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#overrides ⇒ Object
readonly
Returns the value of attribute overrides.
-
#traits ⇒ Object
readonly
Returns the value of attribute traits.
-
#transforms ⇒ Object
readonly
Returns the value of attribute transforms.
Instance Method Summary collapse
-
#after(&block) ⇒ Object
Add after hook.
-
#apply { ... } ⇒ Object
Apply this persona and execute block.
-
#before(&block) ⇒ Object
Add before hook.
-
#include_trait(trait_name) ⇒ Object
Include a trait.
-
#initialize(name) ⇒ Persona
constructor
A new instance of Persona.
-
#mode(mode_name) ⇒ Object
Set generation mode.
- #normalize_schema_name(schema) ⇒ Object private
-
#override(schema, **fields) ⇒ Object
Add overrides for a schema.
-
#overrides_for(schema_name) ⇒ Hash
Get effective overrides for a schema.
-
#seed(value) ⇒ Object
Set random seed.
-
#transform(schema = nil) {|data, context| ... } ⇒ Object
Add a transform function.
Constructor Details
#initialize(name) ⇒ Persona
Returns a new instance of Persona.
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/synthra/personas.rb', line 164 def initialize(name) @name = name @generation_mode = :random @generation_seed = nil @overrides = {} @traits = [] @transforms = [] @before_hooks = [] @after_hooks = [] end |
Instance Attribute Details
#generation_mode ⇒ Object (readonly)
Returns the value of attribute generation_mode.
161 162 163 |
# File 'lib/synthra/personas.rb', line 161 def generation_mode @generation_mode end |
#generation_seed ⇒ Object (readonly)
Returns the value of attribute generation_seed.
161 162 163 |
# File 'lib/synthra/personas.rb', line 161 def generation_seed @generation_seed end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
161 162 163 |
# File 'lib/synthra/personas.rb', line 161 def name @name end |
#overrides ⇒ Object (readonly)
Returns the value of attribute overrides.
162 163 164 |
# File 'lib/synthra/personas.rb', line 162 def overrides @overrides end |
#traits ⇒ Object (readonly)
Returns the value of attribute traits.
162 163 164 |
# File 'lib/synthra/personas.rb', line 162 def traits @traits end |
#transforms ⇒ Object (readonly)
Returns the value of attribute transforms.
162 163 164 |
# File 'lib/synthra/personas.rb', line 162 def transforms @transforms end |
Instance Method Details
#after(&block) ⇒ Object
Add after hook
225 226 227 |
# File 'lib/synthra/personas.rb', line 225 def after(&block) @after_hooks << block end |
#apply { ... } ⇒ Object
Apply this persona and execute block
234 235 236 237 |
# File 'lib/synthra/personas.rb', line 234 def apply(&block) context = PersonaContext.new(self) context.apply(&block) end |
#before(&block) ⇒ Object
Add before hook
220 221 222 |
# File 'lib/synthra/personas.rb', line 220 def before(&block) @before_hooks << block end |
#include_trait(trait_name) ⇒ Object
Include a trait
206 207 208 |
# File 'lib/synthra/personas.rb', line 206 def include_trait(trait_name) @traits << trait_name end |
#mode(mode_name) ⇒ Object
Set generation mode
179 180 181 |
# File 'lib/synthra/personas.rb', line 179 def mode(mode_name) @generation_mode = mode_name end |
#normalize_schema_name(schema) ⇒ Object (private)
261 262 263 264 265 266 267 268 |
# File 'lib/synthra/personas.rb', line 261 def normalize_schema_name(schema) case schema when String then schema when Symbol then schema.to_s when Class then schema.name else schema.to_s end end |
#override(schema, **fields) ⇒ Object
Add overrides for a schema
196 197 198 199 200 |
# File 'lib/synthra/personas.rb', line 196 def override(schema, **fields) schema_name = normalize_schema_name(schema) @overrides[schema_name] ||= {} @overrides[schema_name].merge!(fields) end |
#overrides_for(schema_name) ⇒ Hash
Get effective overrides for a schema
244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/synthra/personas.rb', line 244 def overrides_for(schema_name) result = {} # Apply trait overrides first @traits.each do |trait_name| trait = Traits[trait_name] result.merge!(trait.overrides_for(schema_name)) if trait end # Apply persona overrides result.merge!(@overrides[schema_name] || {}) result end |
#seed(value) ⇒ Object
Set random seed
187 188 189 |
# File 'lib/synthra/personas.rb', line 187 def seed(value) @generation_seed = value end |
#transform(schema = nil) {|data, context| ... } ⇒ Object
Add a transform function
215 216 217 |
# File 'lib/synthra/personas.rb', line 215 def transform(schema = nil, &block) @transforms << { schema: schema&.to_s, block: block } end |