class DTO
DTO
class allows to create a structure that support export to json and construct with a hash.
Example of use:
class User < DTO.new(:first_name, :last_name, :birthday) end
or
User = Class.new(DTO.new :first_name, :last_name, :birthday)
Public Class Methods
new(attrs = {})
click to toggle source
# File lib/dto.rb, line 16 def initialize(attrs = {}) members.each do |member| send("#{member}=", attrs[member]) end end
Public Instance Methods
to_json()
click to toggle source
# File lib/dto.rb, line 22 def to_json attrs = {} members.each { |attr| attrs[attr] = send(attr) } attrs.to_json end
to_s()
click to toggle source
# File lib/dto.rb, line 28 def to_s members.map { |attr| "#{attr}: #{send(attr)}" }.join(", ") end