From ee0c352398f7eb303865fcf5c7cb8a90bb72b998 Mon Sep 17 00:00:00 2001 From: Ismael Celis Date: Mon, 26 Aug 2024 20:45:55 +0100 Subject: [PATCH] Basic hooks for Plumb::Attributes --- lib/plumb/attributes.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/plumb/attributes.rb b/lib/plumb/attributes.rb index dc543b9..d6e38b5 100644 --- a/lib/plumb/attributes.rb +++ b/lib/plumb/attributes.rb @@ -211,11 +211,11 @@ def attribute(name, type = Types::Any, &block) name = key.to_sym type = Composable.wrap(type) if block_given? # :foo, Array[Data] or :foo, Struct - type = Types::Data if type == Types::Any + type = __plumb_struct_class__ if type == Types::Any type = Plumb.decorate(type) do |node| if node.is_a?(Plumb::ArrayClass) child = node.children.first - child = Types::Data if child == Types::Any + child = __plumb_struct_class__ if child == Types::Any Types::Array[build_nested(name, child, &block)] elsif node.is_a?(Plumb::Step) build_nested(name, node, &block) @@ -228,6 +228,12 @@ def attribute(name, type = Types::Any, &block) end @_schema = _schema + { key => type } + __plumb_define_attribute_method__(name) + end + + def __plumb_struct_class__ = Types::Data + + def __plumb_define_attribute_method__(name) define_method(name) { @attributes[name] } end