DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Alex Dovzhanyn
Alex Dovzhanyn

Posted on

Rydux - A Redux store for your ruby application

Hey all, just wanted to write this post to let you know about a small ruby gem I've created, Rydux. This gem allows you to decouple the state from your logic in your application, and implements a more functional paradigm to ruby programming. You can check it out here.

To use the gem, require it somewhere in the root of your application (or somewhere the store will be accessible everywhere you need it). Next, create some reducers in your application, you can place them anywhere, but a reducers/ directory is recommended. A sample reducer looks something like this:

  # reducers/user_reducer.rb

  class UserReducer < Rydux::Reducer

    # Your reducer MUST have a map_state function in order to do anything.
    def self.map_state(action, state = {})
      case action[:type]
      when 'SOME_RANDOM_ACTION' # You can add as many actions here as you'd like
        state.merge(some_random_data: true)
      when 'APPEND_PAYLOAD'
        state.merge(action[:payload])
      else
        state
      end
    end

  end

Enter fullscreen mode Exit fullscreen mode

Create a store somewhere easily accessible in your application:

    require 'reducers/user_reducer'

    # The key passed into .new here is the key at which this value
    # will be stored in the state. E.g. { user: whatever_user_state }
    Store = Rydux::Store.new(user: UserReducer)

Enter fullscreen mode Exit fullscreen mode

Have something subscribe to the store:

    class MyClass
      def initialize
        Store.subscribe(self)
      end

      # Every instance that subscribes to the store will
      # get this state_changed method called whenever the state
      # in the store changes. Do whatever you want with your state here.
      def state_changed(state)
        # ...
      end
    end
Enter fullscreen mode Exit fullscreen mode

To update the store with new data, you can dispatch actions, like so:

    Store.dispatch(type: 'SOME_RANDOM_ACTION')
Enter fullscreen mode Exit fullscreen mode

Putting it all together:

require 'rydux'

class UserReducer < Rydux::Reducer
  @@initial_state = { name: 'Alex', age: 20 }

  def self.map_state(action, state = @@initial_state)
    case action[:type]
    when 'CHANGE_USER_NAME'
      state.merge(name: action[:payload][:name])
    else
      state
    end
  end

end

Store = Rydux::Store.new(user: UserReducer)

class Friend
  def initialize
    Store.subscribe(self)
    @users_name = Store.state.user.name
  end

  def state_changed(state)
    @users_name = state.user.name
  end

  def greet_user
    puts "Hello, #{@users_name}"
  end
end

# Create a new friend (this will subscribe it to the store)
friend = Friend.new
friend.greet_user #=> Hello, Alex

# Change a value in the store
Store.dispatch(type: 'CHANGE_USER_NAME', payload: { name: 'Mike' })
friend.greet_user #=> Hello, Mike
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
ben profile image
Ben Halpern

Wow, interesting.

An Animated Guide to Node.js Event Lop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.