From a7b114682b691cbd165b8ad1ed80821687cef2aa Mon Sep 17 00:00:00 2001 From: nichol alexander Date: Sat, 16 Mar 2024 10:41:46 -0400 Subject: [PATCH] Re work API to accept booleans and hashes. Update readme to make demonstrate new api. --- README.md | 10 +++++----- lib/steady_state/attribute.rb | 16 ++++++++-------- spec/steady_state/attribute_spec.rb | 4 ++-- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 8911933..5afeab0 100644 --- a/README.md +++ b/README.md @@ -213,17 +213,17 @@ end `steady_state` also follows the same `prefix` api as `delegate` in Rails. You may optionally define your scopes to be prefixed to the name of the state machine with `prefix: true`, or you may provide a custom prefix with `prefix: :some_custom_name`. This may be useful when dealing with multiple state machines on one object. ```ruby -steady_state :temperature, scopes: true, prefix: true do +steady_state :temperature, scopes: { prefix: true } do state 'cold', default: true end -steady_state :conductivity, scopes: true, prefix: :sigma do - state 'conductive', default: true +steady_state :color_temperature, scopes: { prefix: 'color' } do + state 'cold', default: true end Material.solid # => query for 'solid' records -Material.temperature_cold # => query for 'cold' records -Material.sigma_conductive # => query for 'conductive' records +Material.temperature_cold # => query for records with a cold temperature +Material.color_cold # => query for for records with a cold color temperature ``` ### Next and Previous States diff --git a/lib/steady_state/attribute.rb b/lib/steady_state/attribute.rb index 2056b12..fad35df 100644 --- a/lib/steady_state/attribute.rb +++ b/lib/steady_state/attribute.rb @@ -15,7 +15,7 @@ module Attribute end class_methods do - def steady_state(attr_name, predicates: true, states_getter: true, scopes: SteadyState.active_record?(self), prefix: false, &block) # rubocop:disable Metrics/ + def steady_state(attr_name, predicates: true, states_getter: true, scopes: SteadyState.active_record?(self), &block) # rubocop:disable Metrics/ overrides = Module.new do define_method :"validate_#{attr_name}_transition_to" do |next_value| if public_send(attr_name).may_become?(next_value) @@ -55,15 +55,15 @@ def steady_state(attr_name, predicates: true, states_getter: true, scopes: Stead end delegate(*state_machines[attr_name].predicates, to: attr_name, allow_nil: true) if predicates - if scopes - scope_prefix = if prefix - "#{prefix == true ? attr_name : prefix}_" - else - "" - end + prefix = if scopes == true + '' + elsif scopes.is_a?(Hash) && scopes[:prefix] + "#{scopes[:prefix] == true ? attr_name : scopes[:prefix]}_" + end + state_machines[attr_name].states.each do |state| - scope :"#{scope_prefix}#{state}", -> { where(attr_name.to_sym => state) } + scope :"#{prefix}#{state}", -> { where(attr_name.to_sym => state) } end end diff --git a/spec/steady_state/attribute_spec.rb b/spec/steady_state/attribute_spec.rb index fab6447..8e5c4ec 100644 --- a/spec/steady_state/attribute_spec.rb +++ b/spec/steady_state/attribute_spec.rb @@ -395,7 +395,7 @@ def self.scope(name, callable) end context 'enabled with prefix: true' do - let(:opts) { { scopes: true, prefix: true } } + let(:opts) { { scopes: { prefix: true } } } it 'defines a scope for each state, prefixed with the name of the state machine' do expect(steady_state_class.defined_scopes.keys).to eq %i(car_driving car_stopped car_parked) @@ -410,7 +410,7 @@ def self.scope(name, callable) end context 'enabled with a custom prefix such as prefix: :automobile' do - let(:opts) { { scopes: true, prefix: :automobile } } + let(:opts) { { scopes: { prefix: :automobile } } } it 'defines a scope for each state with the custom prefix' do expect(steady_state_class.defined_scopes.keys).to eq %i(automobile_driving automobile_stopped automobile_parked)