Class: PaperTrailDiff::AssociationTree
- Inherits:
-
Object
- Object
- PaperTrailDiff::AssociationTree
- Defined in:
- lib/paper_trail_diff/configuration.rb,
sig/generated/paper_trail_diff/configuration.rbs
Overview
An immutable, bounded tree built from explicit association paths.
Instance Attribute Summary collapse
Class Method Summary collapse
-
.build(values) ⇒ AssociationTree
: (untyped) -> AssociationTree.
Instance Method Summary collapse
-
#child(name) ⇒ AssociationTree?
: (String | Symbol) -> AssociationTree?.
-
#empty? ⇒ Boolean
: () -> bool.
-
#initialize(children) ⇒ AssociationTree
constructor
: (Hash[String, AssociationTree]) -> void.
-
#paths(prefix: '') ⇒ Array[String]
: (?prefix: String) -> Array.
-
#select(names) ⇒ AssociationTree
: (Array) -> AssociationTree.
Constructor Details
#initialize(children) ⇒ AssociationTree
: (Hash[String, AssociationTree]) -> void
52 53 54 55 |
# File 'lib/paper_trail_diff/configuration.rb', line 52 def initialize(children) @children = children.dup.freeze freeze end |
Instance Attribute Details
#children ⇒ Hash[String, AssociationTree] (readonly)
7 8 9 |
# File 'lib/paper_trail_diff/configuration.rb', line 7 def children @children end |
Class Method Details
.build(values) ⇒ AssociationTree
: (untyped) -> AssociationTree
11 12 13 14 15 16 17 18 19 |
# File 'lib/paper_trail_diff/configuration.rb', line 11 def build(values) unless valid_values?(values) raise ConfigurationError, 'associations: must be an array of strings or symbols' end tree = {} #: Hash[String, untyped] values.each { |value| add_path(tree, value.to_s) } from_hash(tree) end |
Instance Method Details
#child(name) ⇒ AssociationTree?
: (String | Symbol) -> AssociationTree?
63 64 65 |
# File 'lib/paper_trail_diff/configuration.rb', line 63 def child(name) children[name.to_s] end |
#empty? ⇒ Boolean
: () -> bool
58 59 60 |
# File 'lib/paper_trail_diff/configuration.rb', line 58 def empty? children.empty? end |
#paths(prefix: '') ⇒ Array[String]
: (?prefix: String) -> Array
73 74 75 76 77 78 |
# File 'lib/paper_trail_diff/configuration.rb', line 73 def paths(prefix: '') children.flat_map do |name, subtree| path = prefix.empty? ? name : "#{prefix}.#{name}" [path, *subtree.paths(prefix: path)] end.freeze end |
#select(names) ⇒ AssociationTree
: (Array) -> AssociationTree
68 69 70 |
# File 'lib/paper_trail_diff/configuration.rb', line 68 def select(names) self.class.new(children.slice(*names)) end |