Class: Mkmatter::Answers

Inherits:
Object
  • Object
show all
Defined in:
lib/mkmatter/answers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(question_hash, publish) ⇒ Answers

Returns a new instance of Answers.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mkmatter/answers.rb', line 12

def initialize(question_hash, publish)
  @title       = question_hash[:title]
  @tags        = question_hash[:tags]
  @categories  = question_hash[:categories]
  Time.zone    = question_hash[:time_zone]
  now          = Time.zone.now
  @date        = now.to_s
  @slug_date   = now.strftime('%Y-%m-%d')
  @published   = publish
  @file_format = question_hash[:file_format]
  @matter      = {
      layout:     question_hash[:layout],
      title:      @title,
      categories: @categories,
      tags:       @tags,
      date:       @date,
  }
  @matter[:published] = @published if publish
end

Instance Attribute Details

#answer_hashObject

Returns the value of attribute answer_hash.



8
9
10
# File 'lib/mkmatter/answers.rb', line 8

def answer_hash
  @answer_hash
end

#categoriesObject

Returns the value of attribute categories.



6
7
8
# File 'lib/mkmatter/answers.rb', line 6

def categories
  @categories
end

#dateObject

Returns the value of attribute date.



7
8
9
# File 'lib/mkmatter/answers.rb', line 7

def date
  @date
end

#draftObject

Returns the value of attribute draft.



7
8
9
# File 'lib/mkmatter/answers.rb', line 7

def draft
  @draft
end

#file_formatObject

Returns the value of attribute file_format.



9
10
11
# File 'lib/mkmatter/answers.rb', line 9

def file_format
  @file_format
end

#matterObject (readonly)

Returns the value of attribute matter.



10
11
12
# File 'lib/mkmatter/answers.rb', line 10

def matter
  @matter
end

#publishedObject

Returns the value of attribute published.



9
10
11
# File 'lib/mkmatter/answers.rb', line 9

def published
  @published
end

#slug_dateObject

Returns the value of attribute slug_date.



8
9
10
# File 'lib/mkmatter/answers.rb', line 8

def slug_date
  @slug_date
end

#tagsObject

Returns the value of attribute tags.



6
7
8
# File 'lib/mkmatter/answers.rb', line 6

def tags
  @tags
end

#titleObject

Returns the value of attribute title.



6
7
8
# File 'lib/mkmatter/answers.rb', line 6

def title
  @title
end

Instance Method Details

#dumpString

Dumps all file applicable metadata to a provided output.

Returns:

  • (String)

    yaml front matter



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mkmatter/answers.rb', line 47

def dump
  custom_fields = nil
  hl            = HighLine.new($stdin, $stderr, 80)
  # Custom matter
  if hl.agree('Do you want to add custom fields? (usable as {{LAYOUT_TYPE.FIELD}} in templates) ', true)
    hl.say('Your fields should be inputted as FIELD=>TEXT HERE')
    hl.say("Enter 'EOL' on a new line and press enter when you are done.")
    hl.say("<% HighLine.color('NOTE', :bold, :red) %>: Input is <% HighLine.color('NOT', :bold, :red) %> evaluated!")
    custom_fields = hl.ask('Fields?') do |q|
      q.gather = /^EOL$/
    end
  end
  if custom_fields
    fields = Hash.new
    custom_fields.each do |field|
      field = field.split(/=>/)
      fields.store(field[0].to_s, field[1])
    end
    self.to_h = fields
  elsif custom_fields.nil?
    hl.say('No extra fields were added.')
  else
  end
  self.to_h.stringify_keys.to_yaml(indentation: 2)
  '---'
end

#to_hHash Also known as: inspect

Returns attribute ‘.matter`

Returns:

  • (Hash)

    returns attribute ‘.matter`



33
34
35
# File 'lib/mkmatter/answers.rb', line 33

def to_h
  @matter
end

#to_h=(hash) ⇒ nil

Returns merges hash into attribute ‘.matter`.

Parameters:

  • hash (Hash)

    other hash

Returns:

  • (nil)

    merges hash into attribute ‘.matter`



39
40
41
# File 'lib/mkmatter/answers.rb', line 39

def to_h=(hash)
  @matter.merge!(hash)
end