Class: Primrose::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/primrose/store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_state = {}) ⇒ Store

Returns a new instance of Store.



5
6
7
# File 'lib/primrose/store.rb', line 5

def initialize(initial_state = {})
  @state = Observable.new(initial_state)
end

Instance Attribute Details

#stateObject (readonly)

Returns the value of attribute state.



3
4
5
# File 'lib/primrose/store.rb', line 3

def state
  @state
end

Instance Method Details

#dispatch(action) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/primrose/store.rb', line 9

def dispatch(action)
  new_state = @state.value.dup  # Start with a copy of the current state
  puts "Dispatching action: #{action[:type]}"

  action[:updates].each do |key, value|
    puts "Updating #{key} with #{value}"
    new_state[key] = value.call(new_state[key])  # Apply update function to corresponding value in state
    puts "New value: #{new_state[key]}"
  end

  @state.alter(new_state)  # Update state
  puts "New state: #{@state}"
end