module Plumb::Attributes::ClassMethods
def [](type_specs)
def [](type_specs) type_specs = type_specs._schema if type_specs.is_a?(Plumb::HashClass) klass = Class.new(self) type_specs.each do |key, type| klass.attribute(key, type) end klass end
def __plumb_define_attribute_reader_method__(name)
def __plumb_define_attribute_reader_method__(name) define_method(name) { @attributes[name] } end
def __plumb_define_attribute_writer_method__(name)
def __plumb_define_attribute_writer_method__(name) define_method("#{name}=") do |value| type = self.class._schema.at_key(name) result = type.resolve(value) @attributes[name] = result.value if result.valid? @errors.delete(name) else @errors.merge!(name => result.errors) end result.value end end
def __set_nested_class__(name, klass)
def __set_nested_class__(name, klass) name = name.to_s.split('_').map(&:capitalize).join.sub(/s$/, '') const_set(name, klass) unless const_defined?(name) end
def _schema
def _schema @_schema ||= HashClass.new end
def attribute(name, type = Types::Any, writer: false, &block)
attribute(:friends, [Person])
attribute(:friends, Types::Array[Person])
attribute(:friends, []) # same as Types::Array[Types::Any]
attribute(:friends, Types::Array) # same as Types::Array[Types::Any]
attribute(:friends, Types::Array) { attribute(:name, String) }
attribute(:name, String)
attribute(:friend, MyStruct) { attribute(:name, String) }
attribute(:friend) { attribute(:name, String) }
def attribute(name, type = Types::Any, writer: false, &block) key = Key.wrap(name) name = key.to_sym type = Composable.wrap(type) if block_given? # :foo, Array[Data] or :foo, Struct 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 = __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) elsif node.is_a?(Class) && node <= Plumb::Attributes build_nested(name, node, &block) else node end end end @_schema = _schema + { key => type } __plumb_define_attribute_reader_method__(name) return name unless writer __plumb_define_attribute_writer_method__(name) end
def attribute?(name, *args, &block)
def attribute?(name, *args, &block) attribute(Key.new(name, optional: true), *args, &block) end
def build_nested(name, node, &block)
def build_nested(name, node, &block) if node.is_a?(Class) && node <= Plumb::Attributes sub = Class.new(node) sub.instance_exec(&block) __set_nested_class__(name, sub) return Composable.wrap(sub) end return node unless node.is_a?(Plumb::Step) child = node.children.first return node unless child <= Plumb::Attributes sub = Class.new(child) sub.instance_exec(&block) __set_nested_class__(name, sub) Composable.wrap(sub) end
def call(result)
-
(Plumb::Result::Valid, Plumb::Result::Invalid)
-
Parameters:
-
result
(Plumb::Result::Valid
) --
def call(result) return result if result.value.is_a?(self) return result.invalid(errors: ['Must be a Hash of attributes']) unless result.value.respond_to?(:to_h) instance = new(result.value.to_h) instance.valid? ? result.valid(instance) : result.invalid(instance, errors: instance.errors.to_h) end
def inherited(subclass)
def inherited(subclass) _schema._schema.each do |key, type| subclass.attribute(key, type) end super end
def node_name = :data
def node_name = :data