Skip to content

Instantly share code, notes, and snippets.

@serradura
Created February 7, 2022 03:05
Show Gist options
  • Save serradura/8711f21dac3e6c23fb34f05337671070 to your computer and use it in GitHub Desktop.
Save serradura/8711f21dac3e6c23fb34f05337671070 to your computer and use it in GitHub Desktop.
Micro::Attributes::FormObject
# frozen_string_literal: true
module Micro::Attributes
module FormObject
module ClassMethods
def empty
instance = self.allocate
instance.send(:attributes=, Kind::Empty::HASH)
instance
end
end
module InstanceMethods
def initialize(input)
self.attributes = input
run_validations!
end
def persisted?
false
end
def has_errors?
!errors.empty?
end
private def __call_after_attributes_assign
end
end
def self.included(base)
base.send(:include, Micro::Attributes.with(:activemodel_validation))
base.send(:include, ActiveModel::Conversion)
base.extend(ActiveModel::Naming)
base.extend(ActiveModel::Translation)
base.extend(ClassMethods)
base.send(:include, InstanceMethods)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment