Class: Lich::Common::SettingsProxy
- Inherits:
-
Object
- Object
- Lich::Common::SettingsProxy
- Defined in:
- documented/common/settings/settings_proxy.rb
Overview
SettingsProxy is defined here but relies on Settings module being fully defined first, especially Settings._log. The actual require_relative for settings_proxy.rb is now at the end of settings.rb.
Constant Summary collapse
- LOG_PREFIX =
Log prefix for SettingsProxy debug messages.
"[SettingsProxy]".freeze
- IMPLICIT_CONVERSIONS =
The implicit conversions this class defines for every target (to_ary, to_hash, ...). Whether a proxy has one is whether its target has it.
[:to_str, :to_sym, :to_int, :to_r, :to_ary, :to_hash, :to_proc, :to_io, :to_path].freeze
- NON_DESTRUCTIVE_METHODS =
List of non-destructive methods that return a new container or view without modifying the target. Includes collection methods like :map, :select, :sort, :keys, as well as query methods like :size, :empty?, and :count.
[ :+, :-, :&, :|, :*, :all?, :any?, :assoc, :at, :bsearch, :bsearch_index, :chunk, :chunk_while, :collect, :collect_concat, :compact, :compare_by_identity?, :count, :cycle, :default, :default_proc, :detect, :dig, :drop, :drop_while, :each_cons, :each_entry, :each_slice, :empty?, # each_with_index and each_with_object are defined above :entries, :except, :fetch, :fetch_values, :filter, :find, :find_all, :find_index, :first, :flat_map, :flatten, :frozen?, :grep, :grep_v, :group_by, :has_value?, :include?, :inject, :invert, :join, :key, :keys, :last, :lazy, :length, :map, :max, :max_by, :member?, :merge, :min, :min_by, :minmax, :minmax_by, :none?, :one?, :pack, :partition, :permutation, :product, :rassoc, :reduce, :reject, :reverse, :rotate, :sample, :select, :shuffle, :size, :slice, :slice_after, :slice_before, :slice_when, :sort, :sort_by, :sum, :take, :take_while, :to_a, :to_h, :to_proc, :transform_keys, :transform_values, :uniq, :values, :values_at, :zip ].freeze
- NON_DESTRUCTIVE_CONTAINER_VIEWS =
Subset of non-destructive methods that return container "views"
[ :map, :collect, :select, :filter, :reject, :find_all, :grep, :grep_v, :sort, :sort_by, :uniq, :compact, :flatten, :slice, :take, :drop, :values ].freeze
- MUTATING_METHODS =
Core Ruby methods that change a Hash or an Array in place. A call to one of them through a proxy is applied to this session's copy and then replayed on the row as stored (Settings.save_proxy_changes), so that what other sessions saved in the same container is kept. Their blocks (delete_if { ... }) therefore run once on this session's copy and again on the stored container, and once more each time another session's write makes the save start over, so they must not have side effects. Both runs hold the lock of the script's settings in that scope (see Settings.with_proxy_lock), so a slow block delays the script's other threads writing settings in that scope until it returns.
[ :<<, :[]=, :append, :clear, :collect!, :compact!, :compare_by_identity, :concat, :default=, :default_proc=, :delete, :delete_at, :delete_if, :fill, :filter!, :flatten!, :insert, :keep_if, :map!, :merge!, :pop, :prepend, :push, :rehash, :reject!, :replace, :reverse!, :rotate!, :select!, :shift, :shuffle!, :slice!, :sort!, :sort_by!, :store, :transform_keys!, :transform_values!, :uniq!, :unshift, :update ].freeze
- POSITION_METHODS =
Array methods whose arguments include a position (an index, a start or a range): []=, delete_at, insert, fill and slice!.
[:[]=, :delete_at, :insert, :fill, :slice!].freeze
- NO_POSITION =
An index no Array reaches: delete_at and slice! with it change nothing and return nil, whatever the size of the stored Array.
-(2**62)
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#path_classes ⇒ Object
readonly
For each segment of #path, the class (Hash or Array) of the container it indexed in this session's copy when this proxy was made; nil when not known.
-
#pathless_cause ⇒ Object
readonly
For a detached proxy without a path: what it is ("a view of the whole settings", "the default fetch returned", ...), which the message for a refused write names; nil when not known.
-
#refusals ⇒ Object
readonly
For a container element yielded by #each, or a container taken from one (at any depth): a Hash shared by everything that one call to #each yielded, counting, for each kind of reason, the writes through them that were refused (Settings.refuse_proxy_save tells the user about the first one for each kind only).
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#script_name ⇒ Object
readonly
Returns the value of attribute script_name.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Instance Method Summary collapse
-
#[](key) ⇒ Object, SettingsProxy
Retrieves the value at the given key, wrapping containers in SettingsProxy to track changes.
-
#[]=(key, value) ⇒ Object, SettingsProxy
Sets the value at the given key, unwrapping any SettingsProxy arguments before storage.
-
#anchor_path ⇒ Object
The path of the outermost element #each yielded that #moved? found moved; nil when none has.
-
#binary_op(operator, other) ⇒ Object
private
Applies a binary operator to the target, unwrapping SettingsProxy arguments.
-
#clone(freeze: nil) ⇒ Object
As #dup; the copy is frozen when
freezeis true, or when it is nil and the target is frozen, as Object#clone does. -
#copied_by ⇒ Object
For a #copy?, the method that built it (merge, select, first, ...); nil when not known.
-
#copy? ⇒ Boolean
True for a new container built from an element #each yielded (see #initialize); Settings.save_proxy_changes refuses a write to it.
-
#detached? ⇒ Boolean
Minimal change: expose detached? status.
-
#dup ⇒ Object
A copy of the container is not part of the settings, so dup and clone return a plain copy of the target (proxies unwrapped, containers inside it copied too) instead of a proxy that shares the target and the path, through which a change to the copy (list.dup.pop) would change and store the settings.
- #each(&block) ⇒ Object
-
#each_with_index ⇒ Object
As Enumerable#each_with_index, with the elements #each yields.
-
#each_with_object(memo) ⇒ Object
As Enumerable#each_with_object, with the elements #each yields.
-
#handle_method_result(result, method = nil, args = []) ⇒ Object, SettingsProxy
private
Wraps method call results, returning self if the target was modified in place, or a new proxy/value otherwise.
-
#handle_non_destructive_result(method, result, args = []) ⇒ Object
A container returned by a non-destructive method.
-
#hash ⇒ Integer
Returns the hash code of this proxy's target.
-
#initialize(settings_module, scope, path, target, detached: false, script_name: nil, source: nil, unlocated: false, refusals: nil, copy: false, path_classes: nil, pathless_cause: nil, foreign_values: false, anchor: nil) ⇒ SettingsProxy
constructor
Minimal change: add detached flag (default false) Added script_name parameter to support InstanceSettings (core Lich functionality that runs outside of Script context).
-
#inspect ⇒ Object
Updated inspect method to show the target's inspect string.
-
#instance_of?(klass) ⇒ Boolean
Returns true if this proxy's target is an instance of exactly the given class, not a subclass.
-
#is_a?(klass) ⇒ Boolean
Returns true if this proxy's target is an instance of the given class or a subclass.
-
#kind_of?(klass) ⇒ Boolean
Returns true if this proxy's target is an instance of the given class or a subclass.
-
#method_missing(method, *args) {|*args| ... } ⇒ Object, SettingsProxy
Delegates method calls to the target, handling both non-destructive and destructive methods.
-
#moved? ⇒ Boolean
True when a container element #each yielded (this proxy's, the one this proxy's container was taken from, or one an outer loop yielded that holds either) is no longer at the key or index it was yielded at in this session's copy: the loop removed it (list.each { |e| list.delete(e) ... }), put another value there, or moved it.
-
#nil? ⇒ Boolean
Returns true if this proxy's target is nil.
-
#pretty_print(pp) ⇒ void
Pretty-prints this proxy's target using the given PP context.
-
#proxy_details ⇒ Object
New method to show the proxy's internal details.
-
#respond_to?(method, include_private = false) ⇒ Boolean
An implicit conversion (see IMPLICIT_CONVERSIONS) is reported only when the target has it, so that Ruby, which asks before converting, treats a proxy as it treats its target: a Hash proxy is not taken apart like an Array by |key, (a, b)| or a, b = proxy.
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
private
Reports whether the target responds to a method, allowing method_missing to handle it.
-
#to_a ⇒ Object
Collection conversions.
-
#to_ary ⇒ Array
Converts this proxy's target to an Array, raising NoMethodError if unsupported.
-
#to_c ⇒ Complex
Converts this proxy's target to a Complex number, returning Complex(0, 0) if unsupported.
-
#to_enum(*args, &block) ⇒ Object
Special case for to_enum.
-
#to_f ⇒ Float
Converts this proxy's target to a Float, returning 0.0 if unsupported.
-
#to_h ⇒ Hash
Converts this proxy's target to a Hash, returning {} if unsupported.
-
#to_hash ⇒ Hash
Converts this proxy's target to a Hash, raising NoMethodError if unsupported.
-
#to_i ⇒ Object
Numeric conversions.
-
#to_int ⇒ Integer
Converts this proxy's target to an Integer, raising NoMethodError if unsupported.
-
#to_io ⇒ IO
Converts this proxy's target to an IO object, raising NoMethodError if unsupported.
-
#to_json(*args) ⇒ Object
added 20250620 for JSON.pretty_generate.
-
#to_path ⇒ String
Converts this proxy's target to a file path String, raising NoMethodError if unsupported.
-
#to_proc ⇒ Object
Other common conversions.
-
#to_r ⇒ Rational
Converts this proxy's target to a Rational, raising NoMethodError if unsupported.
-
#to_s ⇒ Object
String conversions.
-
#to_str ⇒ String
Converts this proxy's target to a String, raising NoMethodError if unsupported.
-
#to_sym ⇒ Symbol
Converts this proxy's target to a Symbol, raising NoMethodError if unsupported.
-
#unlocated? ⇒ Boolean
True for the proxy of a view's element that can't be found in the settings (see #initialize).
Constructor Details
#initialize(settings_module, scope, path, target, detached: false, script_name: nil, source: nil, unlocated: false, refusals: nil, copy: false, path_classes: nil, pathless_cause: nil, foreign_values: false, anchor: nil) ⇒ SettingsProxy
Minimal change: add detached flag (default false) Added script_name parameter to support InstanceSettings (core Lich functionality that runs outside of Script context). When script_name is provided, it takes precedence over Script.current.name in the save path.
source, for a detached view (what select or sort returned), is the
container the view was taken from, so that an element taken from the
view (view.first) can be found in it and saved where it is.
unlocated marks a proxy (detached, without a path) for an element
of a view that can't be found in the settings: a write through it is
refused with a message instead of being saved somewhere else.
refusals, shared by the container elements one call to #each
yields and every container taken from them ([], dig, fetch, find,
first, and the copies and views built from them, at any depth),
counts the writes through them that were refused while the loop
runs, so that the user is told once per loop for each kind of reason
(see #refusals and Settings.refuse_proxy_save).
copy marks a new container built from an element #each yielded
(row.merge(..), row.select { ... }, row.first(2)): like a plain
Hash's, it is not part of the settings, so a write to it is refused
with a message; its elements are found where the settings hold them
(+source+ is that element's container). It is the method that built
it (see #copied_by), or true.
path_classes (see #path_classes) defaults, for a path of at most
one segment, to the root's class (a Hash), and otherwise to nil.
pathless_cause (see #pathless_cause) says, for a detached proxy
without a path, what it is.
foreign_values marks a view taken from a container that may hold,
at a key, a value that is not its source's (see
#may_hold_foreign_values?): its elements are found in the source by
identity only (see #source_segment).
anchor (see #moved?) is, for a container element #each yielded
and every container taken from it, a list with one entry for each
loop, outermost first, that yielded it or a container it is in:
[the container of this session's copy that held the element, its
key or index there, the element, the element's path].
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'documented/common/settings/settings_proxy.rb', line 57 def initialize(settings_module, scope, path, target, detached: false, script_name: nil, source: nil, unlocated: false, refusals: nil, copy: false, path_classes: nil, pathless_cause: nil, foreign_values: false, anchor: nil) @settings_module = settings_module # This should be the Settings module itself @scope = scope @path = path.dup @target = target @detached = detached @script_name = script_name @source = source @unlocated = unlocated @refusals = refusals @copy = copy @path_classes = path_classes&.size == @path.size ? path_classes.dup : (Array.new(@path.size, Hash) if @path.size <= 1) @pathless_cause = pathless_cause @foreign_values = foreign_values @anchor = anchor&.dup&.freeze @settings_module._log(Settings::LOG_LEVEL_DEBUG, LOG_PREFIX, -> { "INIT scope: #{@scope.inspect}, path: #{@path.inspect}, target_class: #{@target.class}, target_object_id: #{@target.object_id}, detached: #{@detached}, script_name: #{@script_name.inspect}" }) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) {|*args| ... } ⇒ Object, SettingsProxy
Delegates method calls to the target, handling both non-destructive and destructive methods.
Non-destructive methods return new containers, optionally tagged as detached views. Destructive methods are replayed on the stored row to preserve concurrent changes from other sessions. Detached views are automatically reattached to live containers before mutating operations.
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 |
# File 'documented/common/settings/settings_proxy.rb', line 614 def method_missing(method, *args, &block) @settings_module._log(Settings::LOG_LEVEL_DEBUG, LOG_PREFIX, -> { "CALL scope: #{@scope.inspect}, path: #{@path.inspect}, method: #{method}, args: #{args.inspect}, target_object_id: #{@target.object_id}" }) if @target.respond_to?(method) if NON_DESTRUCTIVE_METHODS.include?(method) @settings_module._log(Settings::LOG_LEVEL_DEBUG, LOG_PREFIX, -> { "CALL non-destructive method: #{method}" }) target_dup = @target.dup unwrapped_args = args.map { |arg| arg.is_a?(SettingsProxy) ? @settings_module.unwrap_proxies(arg) : arg } # Corrected result = target_dup.send(method, *unwrapped_args, &block) # Minimal change: pass method name so we can tag views as detached and keep path return handle_non_destructive_result(method, result, unwrapped_args) else @settings_module._log(Settings::LOG_LEVEL_DEBUG, LOG_PREFIX, -> { "CALL destructive method: #{method}" }) # NEW (5.12.7+): auto-reattach derived views before mutating # ensure destructive methods (.push) do not target a proxy non-destructive method (.sort) # # The view is bound to the container at its path, as the stored # row holds it (Settings._reattach_live!), and the call is made on # that container and stored there. When there is none of the # view's kind, nothing is changed or stored, the user is told, # and the call returns nil. # # A detached proxy without a path (a view's element not found in # the settings, a view of a root proxy, the default fetch # returned, or a container built from one) is never reattached: # with an empty path that would bind it to the whole row. The # call runs on its copy, and the save that follows stores nothing # and tells the user (Settings.save_proxy_changes). if detached? && !@path.empty? unless @settings_module._reattach_live!(self, method) @settings_module._log(Settings::LOG_LEVEL_ERROR, LOG_PREFIX, -> { "CALL reattach failed; aborting destructive op #{method} on detached view" }) return nil end end unwrapped_args = args.map { |arg| arg.is_a?(SettingsProxy) ? @settings_module.unwrap_proxies(arg) : arg } # Corrected return call_mutating_method(method, unwrapped_args, block) if MUTATING_METHODS.include?(method) # Any other method (key?, each_pair, ...) may or may not change the # container. What it changed, if anything, is saved. before = dump_of_target known = own_containers result = @target.send(method, *unwrapped_args, &block) save_changes_since(method, before, known) if before.nil? || dump_of_target != before return handle_method_result(result, method, unwrapped_args) end else super end end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
76 77 78 |
# File 'documented/common/settings/settings_proxy.rb', line 76 def path @path end |
#path_classes ⇒ Object (readonly)
For each segment of #path, the class (Hash or Array) of the container it indexed in this session's copy when this proxy was made; nil when not known. When another session removed a container on the path, Settings.save_proxy_changes goes by it, rather than by the segment's class, to tell a Hash with Integer keys from an Array.
125 126 127 |
# File 'documented/common/settings/settings_proxy.rb', line 125 def path_classes @path_classes end |
#pathless_cause ⇒ Object (readonly)
For a detached proxy without a path: what it is ("a view of the whole settings", "the default fetch returned", ...), which the message for a refused write names; nil when not known.
130 131 132 |
# File 'documented/common/settings/settings_proxy.rb', line 130 def pathless_cause @pathless_cause end |
#refusals ⇒ Object (readonly)
For a container element yielded by #each, or a container taken from one (at any depth): a Hash shared by everything that one call to #each yielded, counting, for each kind of reason, the writes through them that were refused (Settings.refuse_proxy_save tells the user about the first one for each kind only). It is frozen when the loop ends; after that every refused write is told. nil for any other proxy.
85 86 87 |
# File 'documented/common/settings/settings_proxy.rb', line 85 def refusals @refusals end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
76 77 78 |
# File 'documented/common/settings/settings_proxy.rb', line 76 def scope @scope end |
#script_name ⇒ Object (readonly)
Returns the value of attribute script_name.
76 77 78 |
# File 'documented/common/settings/settings_proxy.rb', line 76 def script_name @script_name end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
76 77 78 |
# File 'documented/common/settings/settings_proxy.rb', line 76 def target @target end |
Instance Method Details
#[](key) ⇒ Object, SettingsProxy
Retrieves the value at the given key, wrapping containers in SettingsProxy to track changes.
For detached views or containers built from other containers, the element is looked up in the source container rather than by position. The returned proxy shares the refusals tracking and anchor chain with the parent proxy.
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 |
# File 'documented/common/settings/settings_proxy.rb', line 548 def [](key) @settings_module._log(Settings::LOG_LEVEL_DEBUG, LOG_PREFIX, -> { "GET scope: #{@scope.inspect}, path: #{@path.inspect}, key: #{key.inspect}, target_object_id: #{@target.object_id}" }) value = @target[key] if @settings_module.container?(value) # In a detached view (what select or sort returned), or a container # built from another one (#built_from_source?), a position is not # the element's position in the settings, so the element is looked # up in the container it was taken from. A negative Array index is # made positive, as for at and dig. (built_from_source? && view_element_proxy([key], value)) || proxy_at(@path + (segment_in(@target, key) || [key]), value) else value end end |
#[]=(key, value) ⇒ Object, SettingsProxy
Sets the value at the given key, unwrapping any SettingsProxy arguments before storage.
The change is applied to this session's copy of the target and then replayed on the stored row to preserve changes made by other sessions. This operation is protected by a lock.
591 592 593 594 595 596 597 598 599 600 601 |
# File 'documented/common/settings/settings_proxy.rb', line 591 def []=(key, value) @settings_module._log(Settings::LOG_LEVEL_DEBUG, LOG_PREFIX, -> { "SET scope: #{@scope.inspect}, path: #{@path.inspect}, key: #{key.inspect}, value: #{value.inspect}, target_object_id: #{@target.object_id}" }) actual_value = value.is_a?(SettingsProxy) ? @settings_module.unwrap_proxies(value) : value # Corrected to use @settings_module # Under the entry's lock from the change to this session's copy until # the proxy shows the stored container (see Settings.with_proxy_lock). @settings_module.with_proxy_lock(self) do change_and_replay(:[]=, [key, actual_value], nil) end # This is Ruby expected behavior to return the value. value end |
#anchor_path ⇒ Object
The path of the outermost element #each yielded that #moved? found moved; nil when none has.
101 102 103 |
# File 'documented/common/settings/settings_proxy.rb', line 101 def anchor_path moved_anchor&.last end |
#binary_op(operator, other) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Applies a binary operator to the target, unwrapping SettingsProxy arguments.
168 169 170 171 |
# File 'documented/common/settings/settings_proxy.rb', line 168 def binary_op(operator, other) other_value = other.is_a?(SettingsProxy) ? other.target : other @target.send(operator, other_value) end |
#clone(freeze: nil) ⇒ Object
As #dup; the copy is frozen when freeze is true, or when it is nil
and the target is frozen, as Object#clone does.
361 362 363 364 365 |
# File 'documented/common/settings/settings_proxy.rb', line 361 def clone(freeze: nil) copy = dup copy.freeze if freeze || (freeze.nil? && @target.frozen?) copy end |
#copied_by ⇒ Object
For a #copy?, the method that built it (merge, select, first, ...); nil when not known.
151 152 153 |
# File 'documented/common/settings/settings_proxy.rb', line 151 def copied_by @copy if @copy.is_a?(Symbol) end |
#copy? ⇒ Boolean
True for a new container built from an element #each yielded (see #initialize); Settings.save_proxy_changes refuses a write to it.
145 146 147 |
# File 'documented/common/settings/settings_proxy.rb', line 145 def copy? !!@copy end |
#detached? ⇒ Boolean
Minimal change: expose detached? status
133 134 135 |
# File 'documented/common/settings/settings_proxy.rb', line 133 def detached? !!@detached end |
#dup ⇒ Object
A copy of the container is not part of the settings, so dup and clone return a plain copy of the target (proxies unwrapped, containers inside it copied too) instead of a proxy that shares the target and the path, through which a change to the copy (list.dup.pop) would change and store the settings. A value that can't be copied whole is copied container by container.
352 353 354 355 356 357 |
# File 'documented/common/settings/settings_proxy.rb', line 352 def dup Marshal.load(Marshal.dump(@target)) rescue TypeError # unwrap_proxies is private to Settings. @settings_module.__send__(:unwrap_proxies, @target) end |
#each(&block) ⇒ Object
Yields each element as Array#each and Hash#each do, a container element as a proxy with that element's path, so that a write through it (rows.each { |row| row = 1 }) is stored at that element, as for rows or rows.find { ... }. A Hash yields one [key, value] Array per entry, value proxied, which a |key, value| block takes apart, exactly as Hash#each's own blocks do.
For a detached view (what select or sort returned), or another container built from one (what reverse, last(2) or list - x returned), an element is proxied with the path it has in the container it was taken from (see #view_element_proxy); a [key, value] pair of a Hash (what sort_by returned for a Hash) is yielded as the Hash yields it, with its value proxied at that key. One the settings don't hold (an element of what map returned) is yielded detached and unlocated, so a write through it is refused, and the user is told once per loop (see #refusals).
441 442 443 444 445 |
# File 'documented/common/settings/settings_proxy.rb', line 441 def each(&block) return enum_for(:each) { size_of_target } unless block each_element(&block) end |
#each_with_index ⇒ Object
As Enumerable#each_with_index, with the elements #each yields.
448 449 450 451 452 453 454 455 456 |
# File 'documented/common/settings/settings_proxy.rb', line 448 def each_with_index return enum_for(:each_with_index) { size_of_target } unless block_given? index = 0 each_element do |element| yield element, index index += 1 end end |
#each_with_object(memo) ⇒ Object
As Enumerable#each_with_object, with the elements #each yields.
459 460 461 462 463 464 |
# File 'documented/common/settings/settings_proxy.rb', line 459 def each_with_object(memo) return enum_for(:each_with_object, memo) { size_of_target } unless block_given? each_element { |element| yield element, memo } memo end |
#handle_method_result(result, method = nil, args = []) ⇒ Object, SettingsProxy
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Wraps method call results, returning self if the target was modified in place, or a new proxy/value otherwise.
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 |
# File 'documented/common/settings/settings_proxy.rb', line 1085 def handle_method_result(result, method = nil, args = []) if result.equal?(@target) self # Return self if the method modified the target in-place and returned it elsif @settings_module.container?(result) && (element_path = element_path_for(method, args, result)) proxy_at(element_path, result) elsif @settings_module.container?(result) # If a new container is returned (e.g. some destructive methods might return a new object) # Wrap it in a new proxy, maintaining the current path and scope. SettingsProxy.new(@settings_module, @scope, @path, result, script_name: @script_name, path_classes: @path_classes) else result end end |
#handle_non_destructive_result(method, result, args = []) ⇒ Object
A container returned by a non-destructive method. When it is one of this proxy's elements (find, first, last, at, fetch, min_by, sample, ...) or, for dig, an element further down, the proxy for it has that element's path, so that a write through it saves to that element. Any other container (a new one, such as what merge or first(2) return) keeps this proxy's path, and the views listed in NON_DESTRUCTIVE_CONTAINER_VIEWS are tagged as detached, except that a new container built from an element #each yielded (row.merge(..), row.select { ... }) is a copy (see #copy?): a write to it is not stored, as for a plain Hash, and the user is told.
The elements of such a container are looked up in the container it was built from (see #built_from_source? and #view_element_proxy).
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 |
# File 'documented/common/settings/settings_proxy.rb', line 1032 def handle_non_destructive_result(method, result, args = []) @settings_module.reset_path_and_return( if @settings_module.container?(result) && built_from_source? && (element = view_element_proxy(keys_for(method, args), result)) element elsif @settings_module.container?(result) && (element_path = element_path_for(method, args, result)) proxy_at(element_path, result) elsif @settings_module.container?(result) && (pathless_element? || fetch_default?(method, args)) pathless_derived_proxy(result, fetch_default?(method, args) ? "the default fetch returned" : nil) elsif @settings_module.container?(result) && yielded? SettingsProxy.new(@settings_module, @scope, @path.dup, result, script_name: @script_name, source: view_source, refusals: @refusals, copy: method, path_classes: @path_classes, anchor: @anchor) elsif @settings_module.container?(result) if @target.is_a?(Array) && [:find, :detect].include?(method) # For Array#find / Array#detect, identify the element index in the # original array and create a proxy that points to that element. idx = @target.index(result) if !idx.nil? element_path = @path.dup element_path << idx proxy_at(element_path, result) else # Fallback: if we somehow can't locate the element, preserve # the old behavior (path == @path, no index). is_view = NON_DESTRUCTIVE_CONTAINER_VIEWS.include?(method) SettingsProxy.new(@settings_module, @scope, @path.dup, result, detached: is_view, script_name: @script_name, path_classes: @path_classes, pathless_cause: view_cause(is_view)) end else # Existing behavior for all other non-destructive container # methods. Every such container keeps the container it was # built from as its source, so that its elements are found # where the settings hold them (see #built_from_source?). is_view = NON_DESTRUCTIVE_CONTAINER_VIEWS.include?(method) SettingsProxy.new(@settings_module, @scope, @path.dup, result, detached: is_view, script_name: @script_name, source: view_source, path_classes: @path_classes, pathless_cause: view_cause(is_view), foreign_values: may_hold_foreign_values?, anchor: @anchor) end else # Non-container results (e.g., Hash#keys) stay as plain values result end ) end |
#hash ⇒ Integer
Returns the hash code of this proxy's target.
182 183 184 |
# File 'documented/common/settings/settings_proxy.rb', line 182 def hash @target.hash end |
#inspect ⇒ Object
Updated inspect method to show the target's inspect string
368 369 370 |
# File 'documented/common/settings/settings_proxy.rb', line 368 def inspect @target.inspect end |
#instance_of?(klass) ⇒ Boolean
Returns true if this proxy's target is an instance of exactly the given class, not a subclass.
406 407 408 |
# File 'documented/common/settings/settings_proxy.rb', line 406 def instance_of?(klass) @target.instance_of?(klass) end |
#is_a?(klass) ⇒ Boolean
Returns true if this proxy's target is an instance of the given class or a subclass.
389 390 391 |
# File 'documented/common/settings/settings_proxy.rb', line 389 def is_a?(klass) @target.is_a?(klass) end |
#kind_of?(klass) ⇒ Boolean
Returns true if this proxy's target is an instance of the given class or a subclass. Alias for #is_a?.
398 399 400 |
# File 'documented/common/settings/settings_proxy.rb', line 398 def kind_of?(klass) @target.kind_of?(klass) end |
#moved? ⇒ Boolean
True when a container element #each yielded (this proxy's, the one this proxy's container was taken from, or one an outer loop yielded that holds either) is no longer at the key or index it was yielded at in this session's copy: the loop removed it (list.each { |e| list.delete(e) ... }), put another value there, or moved it. Its path still leads to that key or index, where the settings now hold another element (or none), so Settings.save_proxy_changes refuses a write through it.
95 96 97 |
# File 'documented/common/settings/settings_proxy.rb', line 95 def moved? !moved_anchor.nil? end |
#nil? ⇒ Boolean
Returns true if this proxy's target is nil.
158 159 160 |
# File 'documented/common/settings/settings_proxy.rb', line 158 def nil? @target.nil? end |
#pretty_print(pp) ⇒ void
This method returns an undefined value.
Pretty-prints this proxy's target using the given PP context.
381 382 383 |
# File 'documented/common/settings/settings_proxy.rb', line 381 def pretty_print(pp) pp.pp(@target) end |
#proxy_details ⇒ Object
New method to show the proxy's internal details
373 374 375 |
# File 'documented/common/settings/settings_proxy.rb', line 373 def proxy_details "<SettingsProxy scope=#{@scope.inspect} path=#{@path.inspect} target_class=#{@target.class} target_object_id=#{@target.object_id} detached=#{@detached} script_name=#{@script_name.inspect}>" end |
#respond_to?(method, include_private = false) ⇒ Boolean
An implicit conversion (see IMPLICIT_CONVERSIONS) is reported only when the target has it, so that Ruby, which asks before converting, treats a proxy as it treats its target: a Hash proxy is not taken apart like an Array by |key, (a, b)| or a, b = proxy.
419 420 421 422 423 |
# File 'documented/common/settings/settings_proxy.rb', line 419 def respond_to?(method, include_private = false) return @target.respond_to?(method, include_private) if IMPLICIT_CONVERSIONS.include?(method.to_sym) super || @target.respond_to?(method, include_private) end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Reports whether the target responds to a method, allowing method_missing to handle it.
1351 1352 1353 |
# File 'documented/common/settings/settings_proxy.rb', line 1351 def respond_to_missing?(method, include_private = false) @target.respond_to?(method, include_private) || super end |
#to_a ⇒ Object
Collection conversions
277 278 279 |
# File 'documented/common/settings/settings_proxy.rb', line 277 def to_a delegate_conversion(:to_a, default: []) end |
#to_ary ⇒ Array
Converts this proxy's target to an Array, raising NoMethodError if unsupported.
285 286 287 |
# File 'documented/common/settings/settings_proxy.rb', line 285 def to_ary delegate_conversion(:to_ary, strict: true) end |
#to_c ⇒ Complex
Converts this proxy's target to a Complex number, returning Complex(0, 0) if unsupported.
272 273 274 |
# File 'documented/common/settings/settings_proxy.rb', line 272 def to_c delegate_conversion(:to_c, default: Complex(0, 0)) end |
#to_enum(*args, &block) ⇒ Object
Special case for to_enum
336 337 338 339 340 341 342 343 344 |
# File 'documented/common/settings/settings_proxy.rb', line 336 def to_enum(*args, &block) if @target.respond_to?(:to_enum) @settings_module._log(Settings::LOG_LEVEL_DEBUG, LOG_PREFIX, -> { "to_enum: delegating" }) @target.to_enum(*args, &block) else @settings_module._log(Settings::LOG_LEVEL_DEBUG, LOG_PREFIX, -> { "to_enum: using default enum_for" }) self.enum_for(*args, &block) end end |
#to_f ⇒ Float
Converts this proxy's target to a Float, returning 0.0 if unsupported.
257 258 259 |
# File 'documented/common/settings/settings_proxy.rb', line 257 def to_f delegate_conversion(:to_f, default: 0.0) end |
#to_h ⇒ Hash
Converts this proxy's target to a Hash, returning {} if unsupported.
292 293 294 |
# File 'documented/common/settings/settings_proxy.rb', line 292 def to_h delegate_conversion(:to_h, default: {}) end |
#to_hash ⇒ Hash
Converts this proxy's target to a Hash, raising NoMethodError if unsupported.
300 301 302 |
# File 'documented/common/settings/settings_proxy.rb', line 300 def to_hash delegate_conversion(:to_hash, strict: true) end |
#to_i ⇒ Object
Numeric conversions
242 243 244 |
# File 'documented/common/settings/settings_proxy.rb', line 242 def to_i delegate_conversion(:to_i, default: 0) end |
#to_int ⇒ Integer
Converts this proxy's target to an Integer, raising NoMethodError if unsupported.
250 251 252 |
# File 'documented/common/settings/settings_proxy.rb', line 250 def to_int delegate_conversion(:to_int, strict: true) end |
#to_io ⇒ IO
Converts this proxy's target to an IO object, raising NoMethodError if unsupported.
323 324 325 |
# File 'documented/common/settings/settings_proxy.rb', line 323 def to_io delegate_conversion(:to_io, strict: true) end |
#to_json(*args) ⇒ Object
added 20250620 for JSON.pretty_generate
305 306 307 308 309 310 311 312 |
# File 'documented/common/settings/settings_proxy.rb', line 305 def to_json(*args) if @target.respond_to?(:to_json) @settings_module._log(Settings::LOG_LEVEL_DEBUG, LOG_PREFIX, -> { "to_json: delegating with args" }) @target.to_json(*args) else raise NoMethodError, "undefined method :to_json for #{@target.inspect}:#{@target.class}" end end |
#to_path ⇒ String
Converts this proxy's target to a file path String, raising NoMethodError if unsupported.
331 332 333 |
# File 'documented/common/settings/settings_proxy.rb', line 331 def to_path delegate_conversion(:to_path, strict: true) end |
#to_proc ⇒ Object
Other common conversions
315 316 317 |
# File 'documented/common/settings/settings_proxy.rb', line 315 def to_proc delegate_conversion(:to_proc, strict: true) end |
#to_r ⇒ Rational
Converts this proxy's target to a Rational, raising NoMethodError if unsupported.
265 266 267 |
# File 'documented/common/settings/settings_proxy.rb', line 265 def to_r delegate_conversion(:to_r, strict: true) end |
#to_s ⇒ Object
String conversions
221 222 223 |
# File 'documented/common/settings/settings_proxy.rb', line 221 def to_s delegate_conversion(:to_s, default: '') end |
#to_str ⇒ String
Converts this proxy's target to a String, raising NoMethodError if unsupported.
229 230 231 |
# File 'documented/common/settings/settings_proxy.rb', line 229 def to_str delegate_conversion(:to_str, strict: true) end |
#to_sym ⇒ Symbol
Converts this proxy's target to a Symbol, raising NoMethodError if unsupported.
237 238 239 |
# File 'documented/common/settings/settings_proxy.rb', line 237 def to_sym delegate_conversion(:to_sym, strict: true) end |
#unlocated? ⇒ Boolean
True for the proxy of a view's element that can't be found in the settings (see #initialize).
139 140 141 |
# File 'documented/common/settings/settings_proxy.rb', line 139 def unlocated? !!@unlocated end |