class Typed::Struct::Updater

TODO: This has nothing to do in this gem, should be moved to application

Attributes

params[R]
target[R]

Public Class Methods

new(target, params) click to toggle source
# File lib/typed/struct.rb, line 9
def initialize(target, params)
    @target = target
    @params = params
end

Public Instance Methods

assign(from, to: from, &value_builder) click to toggle source
# File lib/typed/struct.rb, line 14
def assign(from, to: from, &value_builder)
    check_from(from)
    return unless params.key?(from)

    input_value = params[from]
    default_getter = proc { input_value }
    processed_value = (value_builder || default_getter).call(input_value)
    target.send("#{to}=", processed_value)
end

Private Instance Methods

check_from(from) click to toggle source
# File lib/typed/struct.rb, line 26
def check_from(from)
    return if params.class.schema.key?(from)

    raise "Key #{from.inspect} does not exist on #{params.class}"
end