module Machiner::States::SingletonMethods

Public Class Methods

extended(base) click to toggle source
# File lib/machiner/states.rb, line 31
def self.extended(base)
  return unless base.ancestors.include?(InstanceMethods)

  base_container = base.ancestors[1..].detect { |i| i.respond_to? :state_container }&.state_container
  return unless base_container

  return if base.state_container.supercontainers.include?(base_container)

  base.state_container.supercontainers << base_container
end

Public Instance Methods

included(base) click to toggle source
Calls superclass method
# File lib/machiner/states.rb, line 21
def included(base)
  super
  base.extend SingletonMethods
end
inherited(base) click to toggle source
Calls superclass method
# File lib/machiner/states.rb, line 26
def inherited(base)
  super
  base.extend SingletonMethods
end
meta(**meta_hash) click to toggle source
# File lib/machiner/states.rb, line 17
def meta(**meta_hash)
  @meta = meta_hash || {}
end
state(key, value = nil, **meta, &block) click to toggle source
# File lib/machiner/states.rb, line 42
def state(key, value = nil, **meta, &block)
  meta = @meta.merge(meta) if @meta
  state_container.register(key, value, **meta, &block)
end
state?(state_name, *data, **meta) click to toggle source
# File lib/machiner/states.rb, line 51
def state?(state_name, *data, **meta)
  callable = state_container[state_name, **meta]
  return false unless callable

  if callable && callable.arity > data.size
    callable.call(*data.clone, self)
  else
    callable.call(*data.clone)
  end
end
state_container() click to toggle source
# File lib/machiner/states.rb, line 13
def state_container
  @state_container ||= Container.new
end
state_names() click to toggle source
# File lib/machiner/states.rb, line 47
def state_names
  state_container.keys.uniq
end
states(data) click to toggle source
# File lib/machiner/states.rb, line 62
def states(data)
  state_container.keys.select { |state| state_container[state].call(data) }
end