Class: Hackpad::Cli::Pad

Inherits:
Object
  • Object
show all
Defined in:
lib/hackpad/cli/pad.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Pad

Returns a new instance of Pad.



17
18
19
# File 'lib/hackpad/cli/pad.rb', line 17

def initialize(id)
  @id = id
end

Instance Attribute Details

#cached_atObject (readonly)

Returns the value of attribute cached_at.



15
16
17
# File 'lib/hackpad/cli/pad.rb', line 15

def cached_at
  @cached_at
end

#contentObject (readonly)

Returns the value of attribute content.



15
16
17
# File 'lib/hackpad/cli/pad.rb', line 15

def content
  @content
end

#guest_policyObject (readonly)

Returns the value of attribute guest_policy.



15
16
17
# File 'lib/hackpad/cli/pad.rb', line 15

def guest_policy
  @guest_policy
end

#idObject (readonly)

Returns the value of attribute id.



15
16
17
# File 'lib/hackpad/cli/pad.rb', line 15

def id
  @id
end

#moderatedObject (readonly)

Returns the value of attribute moderated.



15
16
17
# File 'lib/hackpad/cli/pad.rb', line 15

def moderated
  @moderated
end

Instance Method Details

#cached?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/hackpad/cli/pad.rb', line 58

def cached?
  Store.exist? 'meta', @id
end

#charsObject



25
26
27
# File 'lib/hackpad/cli/pad.rb', line 25

def chars
  @content.length if @content
end

#linesObject



29
30
31
# File 'lib/hackpad/cli/pad.rb', line 29

def lines
  @content.lines.count if @content
end

#load(ext, refresh = false) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hackpad/cli/pad.rb', line 33

def load(ext, refresh = false)
  fail UnknownFormat unless FORMATS.include? ext
  fail UndefinedPad unless @id
  if refresh || !Store.exist?(ext, @id)
    load_from_provider Api, ext
    Store.save(self, ext)
    Store.save_options(@id, @options)
  else
    load_from_provider Store, ext
  end
end

#load_from_provider(klass, ext) ⇒ Object



45
46
47
48
49
# File 'lib/hackpad/cli/pad.rb', line 45

def load_from_provider(klass, ext)
  @content = klass.read @id, ext
  @options = klass.read_options(@id)
  load_options @options
end

#load_options(options) ⇒ Object



51
52
53
54
55
56
# File 'lib/hackpad/cli/pad.rb', line 51

def load_options(options)
  @guest_policy = options['options']['guestPolicy']
  @moderated = options['options']['isModerated']
  options['cached_at'] ||= Time.now
  @cached_at = options['cached_at']
end

#titleObject



21
22
23
# File 'lib/hackpad/cli/pad.rb', line 21

def title
  @title ||= (@content.lines.first.strip if @content)
end