Class: OldTypedParams::Params

Inherits:
Object
  • Object
show all
Includes:
Contracts::Builtin, Contracts::Core, TypedContracts
Defined in:
lib/typed_params.rb

Instance Method Summary collapse

Constructor Details

#initialize(base, schema) ⇒ Params

Returns a new instance of Params.



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/typed_params.rb', line 36

def initialize(base, schema)
  @schema = schema
  schema.map do |param_name, type|
    data = base[param_name]
    (class << self; self; end).send(:attr_reader, param_name)
    if type.is_a?(Hash)
      instance_variable_set("@#{param_name}", Params.new(data, type))
    else
      handle_non_hash(param_name, type, data)
    end
  end
end

Instance Method Details

#to_value_h(hsh = @schema) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/typed_params.rb', line 50

def to_value_h(hsh = @schema)
  hsh.each_with_object({}) { |(k, _), acc|
    data = send(k)
    acc[k] = if data.is_a?(Params)
      data.to_value_h
    elsif data.class.in?([Kleisli::Maybe::Some, Kleisli::Maybe::None])
      data.value
    else
      data
    end
  }.compact
end