Sha256: f0e3d66a249f6441d11c119fbad5252a8b8d588582eaff8d4cd72d3d27a7526a
Contents?: true
Size: 1.36 KB
Versions: 6
Compression:
Stored size: 1.36 KB
Contents
module Manifestly module Entity class Base @attributes = nil def initialize(data = {}) invalids = data.keys - attributes raise "The following invalid #{self.class} keys were found: #{invalids}" unless invalids.empty? self.attributes = data end def attributes self.class.attributes end def self.attributes @attributes ||= [] end def attributes=(attrs) attrs.each { |k, v| send(:"#{k}=", v) } end def self.attr_accessor(*attrs) attrs.each { |attr| attributes << attr } super end def self.attr_reader(*attrs) attrs.each { |attr| attributes << attr } super end def self.invalid_method(*names) names&.each { |name| define_method(name) { |*_, **_| raise 'invalid method' } } end def self.invalid_class_method(*names) names&.each { |name| define_method(name) { |*_, **_| raise 'invalid method' } } end 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 end end end
Version data entries
6 entries across 6 versions & 1 rubygems