module Sanity::Mutatable::ClassMethods

Constants

ALL_MUTATIONS
DEFAULT_INSTANCE_MUTATIONS
DEFAULT_KLASS_MUTATIONS

Private Instance Methods

mutatable(**options) click to toggle source
# File lib/sanity/mutatable.rb, line 35
def mutatable(**options)
  options.fetch(:only, ALL_MUTATIONS).each do |mutation|
    if DEFAULT_KLASS_MUTATIONS.include? mutation.to_sym
      define_singleton_method(mutation) do |**args|
        Module.const_get("Sanity::Http::#{mutation.to_s.classify}").call(**args.merge(resource_klass: self))
      end
    end

    if DEFAULT_INSTANCE_MUTATIONS.include? mutation.to_sym
      define_method(mutation) do |**args|
        Module.const_get("Sanity::Http::#{mutation.to_s.classify}").call(
          **args.merge(params: attributes, resource_klass: self.class)
        )
      end
    end
  end

  define_singleton_method("mutatable_api_endpoint") { options.fetch(:api_endpoint, "") }
end