Class: MiniCamel::Dto

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/mini_camel/dto.rb

Direct Known Subclasses

StrictDto

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Dto

Returns a new instance of Dto.



32
33
34
35
# File 'lib/mini_camel/dto.rb', line 32

def initialize(hash = {})
  super
  @table = Hashie::Mash.new(hash)
end

Class Method Details

.build(hash_object) ⇒ Object

ensures that every hash like object is a dto



12
13
14
15
# File 'lib/mini_camel/dto.rb', line 12

def self.build(hash_object)
  hash_object = MultiJson.load(MultiJson.dump(self.new(hash_object)))
  deep_build(hash_object)
end

.deep_build(hash_object) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mini_camel/dto.rb', line 18

def self.deep_build(hash_object)
  case hash_object
  when Hash
    hash_object.each do |k, v|
      hash_object[k] = deep_build(v)
    end
    self.new(hash_object)
  when Array
    hash_object.map{|item| deep_build(item)}
  else
    hash_object
  end
end

.emptyObject



7
8
9
# File 'lib/mini_camel/dto.rb', line 7

def self.empty
  new
end

Instance Method Details

#[](key) ⇒ Object



41
42
43
# File 'lib/mini_camel/dto.rb', line 41

def [](key)
  fetch(key)
end

#fetch(key, default: nil) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/mini_camel/dto.rb', line 45

def fetch(key, default: nil)
  if @table.has_key?(key)
    @table.fetch(key, default)
  else
    return default unless default.nil?
  end
end

#update(data = {}) ⇒ Object



37
38
39
# File 'lib/mini_camel/dto.rb', line 37

def update(data = {})
  modifiable.update(data)
end