module Sanity::Attributable

Attributable is responsible for setting the appropriate attributes on an object in memory

The attribute marco is used to define the available attributes and the default return value if applicable

@example provides getter and setter methods for `_id` and sets the default value to an empty string

attribute :_id, default: ""

Attributes

attributes[R]

Public Class Methods

included(base) click to toggle source
# File lib/sanity/attributable.rb, line 15
def included(base)
  base.extend(ClassMethods)
end
new(**args) click to toggle source
# File lib/sanity/attributable.rb, line 39
def initialize(**args)
  self.class.default_attributes.merge(args).then do |attrs|
    attrs.each do |key, val|
      define_singleton_method("#{key}=") do |val|
        args[key] = val
        attributes[key] = val
      end

      define_singleton_method(key) { args[key] }
    end

    instance_variable_set("@attributes", attrs)
  end
end

Public Instance Methods

inspect() click to toggle source
# File lib/sanity/attributable.rb, line 54
def inspect
  attributes.keys.map { |key| "#{key}: #{attributes[key].inspect}" }.join(", ").then do |attrs|
    attrs.empty? ? "#<#{_instance}>" : "#<#{_instance} #{attrs}>"
  end
end

Private Instance Methods

_instance() click to toggle source
# File lib/sanity/attributable.rb, line 62
def _instance
  "#{self.class}:0x#{object_id}"
end