module Optics
Constants
- VERSION
Public Class Methods
hash_lens(*keys)
click to toggle source
# File lib/ruby-optics/optics.rb, line 8 def self.hash_lens(*keys) head, *tail = keys [build_hash_lens(head), *tail].reduce { |result_lens, key| result_lens.compose_lens(build_hash_lens(key)) } end
indiffirent_access_hash_lens(*keys)
click to toggle source
# File lib/ruby-optics/optics.rb, line 16 def self.indiffirent_access_hash_lens(*keys) head, *tail = keys opts = { indiffirent_access: true } [build_hash_lens(head, opts), *tail].reduce { |result_lens, key| result_lens.compose_lens(build_hash_lens(key, opts)) } end
struct_lens(*method_names)
click to toggle source
# File lib/ruby-optics/optics.rb, line 25 def self.struct_lens(*method_names) head, *tail = method_names [build_struct_lens(head), *tail].reduce { |result_lens, key| result_lens.compose_lens(build_struct_lens(key)) } end
Private Class Methods
build_hash_lens(key, indiffirent_access: false)
click to toggle source
# File lib/ruby-optics/optics.rb, line 35 def self.build_hash_lens(key, indiffirent_access: false) ::Lens.new( -> (hash) { if indiffirent_access case key when String val = hash[key] val.nil? ? hash[key.to_sym] : val when Symbol val = hash[key] val.nil? ? hash[key.to_s] : val else hash[key] end else hash[key] end }, -> (new_value, hash) { hash.merge(key => new_value) } ).nullable end
build_struct_lens(method_name)
click to toggle source
# File lib/ruby-optics/optics.rb, line 57 def self.build_struct_lens(method_name) ::Lens.new( -> (struct) { struct[method_name] }, -> (new_value, struct) { struct.class.new( *struct.members.map { |member| (member == method_name) ? new_value : struct[member] } ) } ) end