class Manifestly::Entity::Base

Public Class Methods

attr_accessor(*attrs) click to toggle source
Calls superclass method
# File lib/manifestly/entity/base.rb, line 25
def self.attr_accessor(*attrs)
  attrs.each { |attr| attributes << attr }
  super
end
attr_reader(*attrs) click to toggle source
Calls superclass method
# File lib/manifestly/entity/base.rb, line 30
def self.attr_reader(*attrs)
  attrs.each { |attr| attributes << attr }
  super
end
attributes() click to toggle source
# File lib/manifestly/entity/base.rb, line 17
def self.attributes
  @attributes ||= []
end
invalid_class_method(*names) click to toggle source
# File lib/manifestly/entity/base.rb, line 39
def self.invalid_class_method(*names)
  names&.each { |name| define_method(name) { |*_, **_| raise 'invalid method' } }
end
invalid_method(*names) click to toggle source
# File lib/manifestly/entity/base.rb, line 35
def self.invalid_method(*names)
  names&.each { |name| define_method(name) { |*_, **_| raise 'invalid method' } }
end
new(data = {}) click to toggle source
# File lib/manifestly/entity/base.rb, line 6
def initialize(data = {})
  invalids = data.keys - attributes
  raise "The following invalid #{self.class} keys were found: #{invalids}" unless invalids.empty?

  self.attributes = data
end

Public Instance Methods

attributes() click to toggle source
# File lib/manifestly/entity/base.rb, line 13
def attributes
  self.class.attributes
end
attributes=(attrs) click to toggle source
# File lib/manifestly/entity/base.rb, line 21
def attributes=(attrs)
  attrs.each { |k, v| send(:"#{k}=", v) }
end
to_h() click to toggle source
# File lib/manifestly/entity/base.rb, line 43
def to_h
  {}.tap do |hsh|
    attributes.each do |attr|
      value = send(attr)
      if value.is_a?(Array)
        value = value.map do |it|
          next it.to_h if it.is_a?(Manifestly::Entity::Base)

          it
        end
      end
      hsh[attr] = value
    end
  end
end