Class: HierarchyString
Overview
Class representing a hierarchy of substrings stored as Hash nodes HierarchyString is a class that represents and manipulates strings based on a hierarchical structure. The input to the class can be a single hash or an array of nested hashes, where each hash contains a text string and an optional decoration or transformation (like ‘:downcase`, `:upcase`, etc.).
The primary functionalities of the class include:
-
Initialization: The class can be initialized with either a single hash or an array of nested hashes. Each hash contains a @text_sym key representing the string and a @style_sym key representing the transformation (optional).
-
Concatenation: The ‘concatenate` method concatenates all text strings in the hierarchy into a single string.
-
Decoration: The ‘decorate` method applies the specified transformation (like `:downcase`, `:upcase`) to the text in the hierarchy and returns the decorated string.
-
**Text Replacement**: The ‘replace_text!` method allows in-place replacement of text in the hierarchy by applying a block to each text string.
-
**Method Delegation**: The class uses ‘method_missing` and `respond_to_missing?` to delegate undefined method calls to the string object, allowing for dynamic method handling on the concatenated string (e.g., `capitalize`).
This class is useful for situations where strings are represented in a hierarchical or nested structure and need to be manipulated or transformed in a consistent and customizable manner.
Instance Attribute Summary collapse
-
#substrings ⇒ Object
Returns the value of attribute substrings.
Instance Method Summary collapse
-
#concatenate ⇒ Object
Method to concatenate all substrings into a single string.
-
#decorate ⇒ Object
Method to decorate all substrings into a single string.
-
#initialize(substrings, text_sym: :text, style_sym: :color) ⇒ HierarchyString
constructor
Initialize with a single hash or an array of hashes.
- #map_substring_text_yield(tree, &block) ⇒ Object
-
#method_missing(method_name, *arguments, &block) ⇒ Object
Handle string inspection methods and pass them to the concatenated string.
-
#replace_text! ⇒ Object
operate on substring.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Ensure proper handling of method checks.
Constructor Details
#initialize(substrings, text_sym: :text, style_sym: :color) ⇒ HierarchyString
Initialize with a single hash or an array of hashes
33 34 35 36 37 |
# File 'lib/hierarchy_string.rb', line 33 def initialize(substrings, text_sym: :text, style_sym: :color) @substrings = parse_substrings(substrings) @text_sym = text_sym @style_sym = style_sym end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
Handle string inspection methods and pass them to the concatenated string
86 87 88 89 90 91 92 |
# File 'lib/hierarchy_string.rb', line 86 def method_missing(method_name, *arguments, &block) if ''.respond_to?(method_name) concatenate.send(method_name, *arguments, &block) else super end end |
Instance Attribute Details
#substrings ⇒ Object
Returns the value of attribute substrings.
30 31 32 |
# File 'lib/hierarchy_string.rb', line 30 def substrings @substrings end |
Instance Method Details
#concatenate ⇒ Object
Method to concatenate all substrings into a single string
76 77 78 |
# File 'lib/hierarchy_string.rb', line 76 def concatenate concatenate_substrings(@substrings) end |
#decorate ⇒ Object
Method to decorate all substrings into a single string
81 82 83 |
# File 'lib/hierarchy_string.rb', line 81 def decorate decorate_substrings(@substrings) end |
#map_substring_text_yield(tree, &block) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/hierarchy_string.rb', line 39 def map_substring_text_yield(tree, &block) case tree when Array tree.each.with_index do |node, ind| case node when String tree[ind] = yield node else map_substring_text_yield(node, &block) end end when Hash text = yield tree[@text_sym] tree[@text_sym] = text tree when String yield tree else raise ArgumentError, 'Invalid type.' end end |
#replace_text! ⇒ Object
operate on substring
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/hierarchy_string.rb', line 63 def replace_text! map_substring_text_yield(@substrings) do |node| case node when Hash text = yield node[@text_sym] node[@text_sym] = text when String yield node end end end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Ensure proper handling of method checks
95 96 97 |
# File 'lib/hierarchy_string.rb', line 95 def respond_to_missing?(method_name, include_private = false) ''.respond_to?(method_name) || super end |