Module: LunaPark::Extensions::Wrappable

Included in:
LunaPark::Entities::Simple, Values::Compound
Defined in:
lib/luna_park/extensions/wrappable.rb

Overview

class-level mixin

Examples:

class Account
  extend LunaPark::Extensions::Wrappable

  attr_reader :type, :id

  def initialize(type:, id:)
    @type, @id = type, id
  end
end

hash = { type: 'user', id: 42 }
acccount = Account.new(hash)

Account.new(hash)      # => #<Account type='user', id=42>
Account.new(acccount)  # => raise ArgumentError
Account.wrap(hash)     # => #<Account type='user', id=42>
Account.wrap(acccount) # => #<Account type='user', id=42>
Account.wrap(nil)      # => nil
Account.wrap(true)     # => raise 'Account can not wrap TrueClass'

Account.wrap().eql?() # => true

Instance Method Summary collapse

Instance Method Details

#wrap(input) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/luna_park/extensions/wrappable.rb', line 33

def wrap(input)
  case input
  when self then input
  when Hash then new(input)
  when nil  then nil
  else raise Errors::Unwrapable, "#{self} can not wrap #{input.class}"
  end
end