lib/haku/core.rb in haku-1.0.0 vs lib/haku/core.rb in haku-1.1.0

- old
+ new

@@ -7,43 +7,63 @@ module Haku module Core extend ActiveSupport::Concern included do + prepend Callable + attr_reader :params + class_attribute :haku_inputs, default: [] class_attribute :haku_success_callbacks, default: [] class_attribute :haku_failure_callbacks, default: [] end module ClassMethods + def inherited(base) + super + + base.class_eval do + prepend Callable + end + end + def call(params={}) - new(params).run + new(params).call end + def input(*names) + self.haku_inputs += names + end + def on_success(*methods) self.haku_success_callbacks += methods end def on_failure(*methods) self.haku_failure_callbacks += methods end end - def initialize(params={}) - @params = params + module Callable + def call + result = super - @params.each_key do |key| - define_singleton_method(key) { @params[key] } unless respond_to?(key) + Result.new(_haku_status, _haku_response.merge(result: result)).tap do + _haku_run_callbacks + end end end - def run - result = call + def initialize(params={}) + @params = params - Result.new(_haku_status, _haku_response.merge(result: result)).tap do - _haku_run_callbacks + @_haku_status = :success + @_haku_response = {} + + self.class.haku_inputs.each do |name| + define_singleton_method(name) { @params[name] } unless respond_to?(name) end end private @@ -62,14 +82,14 @@ def _haku_normalize_response(response) response.is_a?(Hash) ? response : { data: response } end def _haku_status - @_haku_status || :success + @_haku_status end def _haku_response - @_haku_response || {} + @_haku_response end def _haku_run_callbacks (self.class.send("haku_#{_haku_status}_callbacks") || []).each { |cb| send(cb) } end