Class: Formally::State
- Inherits:
-
Object
- Object
- Formally::State
- Defined in:
- lib/formally/state.rb
Instance Method Summary collapse
- #call(data) ⇒ Object
- #data ⇒ Object
- #errors ⇒ Object
-
#initialize(config, object) ⇒ State
constructor
A new instance of State.
- #transaction(&block) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(config, object) ⇒ State
Returns a new instance of State.
3 4 5 |
# File 'lib/formally/state.rb', line 3 def initialize config, object @config, @object = config, object end |
Instance Method Details
#call(data) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/formally/state.rb', line 7 def call data injections = { _self: @object } @config.fields.each do |name| injections[name] = @object.send name end result = @config.finalized_schema.with(injections).call data @data = result.output @errors = result.errors @filled = true end |
#data ⇒ Object
19 20 21 |
# File 'lib/formally/state.rb', line 19 def data @filled ? @data : raise(Formally::Unfilled) end |
#errors ⇒ Object
23 24 25 |
# File 'lib/formally/state.rb', line 23 def errors @filled ? @errors : raise(Formally::Unfilled) end |
#transaction(&block) ⇒ Object
31 32 33 |
# File 'lib/formally/state.rb', line 31 def transaction &block @config.transaction.call(&block) end |
#valid? ⇒ Boolean
27 28 29 |
# File 'lib/formally/state.rb', line 27 def valid? errors.empty? end |