Class: Typed::Struct::Updater

Inherits:
Object
  • Object
show all
Defined in:
lib/typed/struct.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, params) ⇒ Updater

Returns a new instance of Updater.



9
10
11
12
# File 'lib/typed/struct.rb', line 9

def initialize(target, params)
    @target = target
    @params = params
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



7
8
9
# File 'lib/typed/struct.rb', line 7

def params
  @params
end

#targetObject (readonly)

Returns the value of attribute target.



7
8
9
# File 'lib/typed/struct.rb', line 7

def target
  @target
end

Instance Method Details

#assign(from, to: from, &value_builder) ⇒ Object



14
15
16
17
18
19
20
21
22
# 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