Class: Mayu::State::Loader

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/mayu/state/loader.rb

Defined Under Namespace

Classes: ReducerBuilder

Instance Method Summary collapse

Constructor Details

#initialize(directory) ⇒ Loader

Returns a new instance of Loader.



192
193
194
# File 'lib/mayu/state/loader.rb', line 192

def initialize(directory)
  @directory = directory
end

Instance Method Details

#loadObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/mayu/state/loader.rb', line 197

def load
  Dir[File.join(@directory, "*.rb")]
    .map do |path|
      data = ReducerBuilder.build(File.read(path), path)
      name = File.basename(path, ".*").capitalize.to_sym

      [
        name,
        ->(state, action) do
          state ||= data[:initial_state]

          data[:reducers]
            .filter { _1 === action }
            .reduce(
              state || data[:initial_state]
            ) { |state, (_, reducer)| reducer.call(state, action) }
        end
      ]
    end
    .to_h
end