Class: Fatty::History::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/fatty/history/entry.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text:, kind: :command, ctx: nil, stamp: nil) ⇒ Entry

Returns a new instance of Entry.



8
9
10
11
12
13
# File 'lib/fatty/history/entry.rb', line 8

def initialize(text:, kind: :command, ctx: nil, stamp: nil)
  @text  = text.to_s
  @kind  = kind.to_sym
  @ctx   = normalize_ctx(ctx)
  @stamp = stamp || Time.now
end

Instance Attribute Details

#ctxObject (readonly)

Returns the value of attribute ctx.



6
7
8
# File 'lib/fatty/history/entry.rb', line 6

def ctx
  @ctx
end

#kindObject (readonly)

Returns the value of attribute kind.



6
7
8
# File 'lib/fatty/history/entry.rb', line 6

def kind
  @kind
end

#stampObject (readonly)

Returns the value of attribute stamp.



6
7
8
# File 'lib/fatty/history/entry.rb', line 6

def stamp
  @stamp
end

#textObject (readonly)

Returns the value of attribute text.



6
7
8
# File 'lib/fatty/history/entry.rb', line 6

def text
  @text
end

Class Method Details

.from_h(hash) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/fatty/history/entry.rb', line 36

def self.from_h(hash)
  hash = hash.transform_keys(&:to_s)

  new(
    text:  hash.fetch("text", ""),
    kind:  hash.fetch("kind", "command").to_sym,
    ctx:   History.normalize_ctx(hash["ctx"]),
    stamp: parse_stamp(hash["stamp"]),
  )
end

Instance Method Details

#command?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/fatty/history/entry.rb', line 24

def command?
  kind == :command
end

#ctx_fetch(key, default = nil) ⇒ Object



32
33
34
# File 'lib/fatty/history/entry.rb', line 32

def ctx_fetch(key, default = nil)
  ctx.fetch(key.to_s, default)
end

#search?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/fatty/history/entry.rb', line 28

def search?
  kind == :search_string || kind == :search_regex
end

#to_hObject



15
16
17
18
19
20
21
22
# File 'lib/fatty/history/entry.rb', line 15

def to_h
  {
    "text" => text,
    "kind" => kind.to_s,
    "ctx" => ctx,
    "stamp" => stamp.iso8601
  }
end