Skip to content

Commit

Permalink
Re work API to accept booleans and hashes.
Browse files Browse the repository at this point in the history
Update readme to make demonstrate new api.
  • Loading branch information
nicholalexander committed Mar 16, 2024
1 parent 5d7b80b commit a7b1146
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions lib/steady_state/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions spec/steady_state/attribute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit a7b1146

Please sign in to comment.