Class: Koi::Entity

Inherits:
Hash
  • Object
show all
Defined in:
lib/koi.rb

Constant Summary collapse

Status =
[:created, :completed, :removed]

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Entity

Returns a new instance of Entity.



303
304
305
306
307
308
309
310
# File 'lib/koi.rb', line 303

def initialize data = {}
  self.replace status:     :created,
               created_at: Time.now,
               owner:      ENV['USER'],
               tags:       [],
               sticky:     false
  update data.reduce({}) {|h, (k,v)| h.merge(k.to_sym => v) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object

Handle things like ‘self.removed?`



334
335
336
337
338
339
340
# File 'lib/koi.rb', line 334

def method_missing meth, *args, &blk
  if meth.to_s.end_with?('?') && Status.include?(s = meth.to_s.chop.to_sym)
    self[:status] == s
  else
    super
  end
end

Instance Method Details

#new?Boolean

Returns:

  • (Boolean)


312
313
314
# File 'lib/koi.rb', line 312

def new?
  self[:status] == :created
end

#status=(st) ⇒ Object



320
321
322
323
# File 'lib/koi.rb', line 320

def status= st
  self[:status] = st
  self[:"#{st}_at"] = Time.now
end

#sticky?Boolean

Returns:

  • (Boolean)


316
317
318
# File 'lib/koi.rb', line 316

def sticky?
  self[:sticky]
end

#to_yaml(*args) ⇒ Object



325
326
327
328
329
# File 'lib/koi.rb', line 325

def to_yaml *args
  reduce({}) do |h, (k, v)|
    h.merge(k.to_s => v)
  end.to_yaml *args
end