Sha256: 2da73d95a3e7feb5c13eac4eb8875072b75e1b5be45425bd62cf00e83240a8d9

Contents?: true

Size: 600 Bytes

Versions: 55

Compression:

Stored size: 600 Bytes

Contents

# 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)
#
class DTO < Struct

  def initialize(attrs = {})
    members.each do |member|
      send("#{member}=", attrs[member])
    end
  end
  
  def to_json
    attrs = {}
    members.each { |attr| attrs[attr] = send(attr) }
    attrs.to_json
  end

  def to_s
    members.map { |attr| "#{attr}: #{send(attr)}" }.join(", ")
  end

end

Version data entries

55 entries across 55 versions & 1 rubygems

Version Path
mxhero-api-1.2.9 lib/dto.rb
mxhero-api-1.2.8 lib/dto.rb
mxhero-api-1.2.7 lib/dto.rb
mxhero-api-1.2.6 lib/dto.rb
mxhero-api-1.2.5 lib/dto.rb
mxhero-api-1.2.4 lib/dto.rb
mxhero-api-1.2.3 lib/dto.rb
mxhero-api-1.2.2 lib/dto.rb
mxhero-api-1.2.1 lib/dto.rb
mxhero-api-1.2.0 lib/dto.rb
mxhero-api-1.1.4 lib/dto.rb
mxhero-api-1.1.3 lib/dto.rb
mxhero-api-1.1.2 lib/dto.rb
mxhero-api-1.1.1 lib/dto.rb
mxhero-api-1.1.0 lib/dto.rb
mxhero-api-1.0.5 lib/dto.rb
mxhero-api-1.0.4 lib/dto.rb
mxhero-api-1.0.3 lib/dto.rb
mxhero-api-1.0.2 lib/dto.rb
mxhero-api-1.0.1 lib/dto.rb