Module: LazyGraph::NodeProperties
- Defined in:
- lib/lazy_graph/node/node_properties.rb
Class Method Summary collapse
-
.build(members:, invisible:) ⇒ Object
Builds an Anonymous Struct with the given members Invisible members are ignored when the struct is serialized.
Class Method Details
.build(members:, invisible:) ⇒ Object
Builds an Anonymous Struct with the given members Invisible members are ignored when the struct is serialized
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/lazy_graph/node/node_properties.rb', line 5 def self.build(members:, invisible:) Struct.new(*members, keyword_init: true) do define_method(:initialize) do |kws| members.each { |k| self[k] = kws[k].then { |v| v.nil? ? MissingValue::BLANK : v } } end define_method(:key?) do |x| !self[x].equal?(MissingValue::BLANK) end define_method(:[]=) do |key, val| super(key, val) end define_method(:members) do members end define_method(:invisible) do invisible end def to_hash to_h end define_method(:each_key, &members.method(:each)) def dup self.class.new(members.each_with_object({}) { _2[_1] = _2[_1].dup }) end def get_first_of(*props) key = props.find do |prop| !self[prop].is_a?(MissingValue) end key ? self[key] : MissingValue::BLANK end def pretty_print(q) q.group(1, '<Props ', '>') do q.seplist(members.zip(values).reject do |m, v| m == :DEBUG && (v.nil? || v.is_a?(MissingValue)) end) do |member, value| q.group do q.text "#{member}=" value.respond_to?(:pretty_print) ? q.pp(value) : q.text(value.inspect) end end end end end end |