Class: GraphAgent::State::Schema
- Inherits:
-
Object
- Object
- GraphAgent::State::Schema
- Defined in:
- lib/graph_agent/state/schema.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Field
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
Instance Method Summary collapse
- #apply(state, updates) ⇒ Object
- #field(name, type: nil, reducer: nil, default: nil) ⇒ Object
- #initial_state ⇒ Object
-
#initialize(&block) ⇒ Schema
constructor
A new instance of Schema.
Constructor Details
#initialize(&block) ⇒ Schema
Returns a new instance of Schema.
10 11 12 13 |
# File 'lib/graph_agent/state/schema.rb', line 10 def initialize(&block) @fields = {} instance_eval(&block) if block end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
6 7 8 |
# File 'lib/graph_agent/state/schema.rb', line 6 def fields @fields end |
Instance Method Details
#apply(state, updates) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/graph_agent/state/schema.rb', line 40 def apply(state, updates) updates.each do |key, value| key = key.to_sym f = @fields[key] if f&.reducer state[key] = f.reducer.call(state[key], value) else state[key] = value end end state end |
#field(name, type: nil, reducer: nil, default: nil) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/graph_agent/state/schema.rb', line 15 def field(name, type: nil, reducer: nil, default: nil) @fields[name.to_sym] = Field.new( name: name.to_sym, type: type, reducer: reducer, default: default ) end |
#initial_state ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/graph_agent/state/schema.rb', line 24 def initial_state @fields.transform_values do |f| if f.default.nil? nil elsif f.default.respond_to?(:dup) begin f.default.dup rescue TypeError f.default end else f.default end end end |