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, include_post_fields = true) ⇒ Answers

Returns a new instance of Answers.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mkmatter/answers.rb', line 9

def initialize(question_hash, publish, include_post_fields = true)
  @title        = question_hash[:title]
  @layout       = question_hash[:layout]
  if @layout == 'post'
    include_post_fields = true
  end
  @tags         = question_hash[:tags] if question_hash[:tags].nil? == false
  @categories   = question_hash[:categories] if question_hash[:categories].nil? == false
  if include_post_fields == true || @layout == 'post'
    Time.zone =  get_time_zone_full(Time.now.getlocal) || 'Eastern Time (US & Canada)'
  end
  # Time.zone     = question_hash[:time_zone] || Time.now.zone
  @now = Time.zone.now || Time.now.getlocal
  @date         = @now.to_s if include_post_fields == true
  @slug_date    = @now.strftime('%Y-%m-%d') if include_post_fields == true
  @published    = publish
  @file_format  = question_hash[:file_format]
  @extra_fields = question_hash[:extra_fields]
  if question_hash[:summary]
    @summary = question_hash[:summary] unless question_hash[:summary].empty?
  end

  @matter = {
    layout: @layout,
    title: @title
  }
  if @layout == 'post' || include_post_fields
    @matter[:tags] = @tags if @tags
    @matter[:categories] = @categories if @categories
    @matter[:summary] = @summary if @summary
    @matter[:date] = @date

  end
  if @draft
    @matter[:draft] = @draft
  end
  if @extra_fields
    @matter.merge!(@extra_fields)
  end
  @matter[:published] = @published if publish
end

Instance Attribute Details

#answer_hashObject

Returns the value of attribute answer_hash.



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

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.



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

def date
  @date
end

#draftObject

Returns the value of attribute draft.



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

def draft
  @draft
end

#extra_fieldsObject

Returns the value of attribute extra_fields.



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

def extra_fields
  @extra_fields
end

#file_formatObject

Returns the value of attribute file_format.



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

def file_format
  @file_format
end

#matterObject

Returns the value of attribute matter.



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

def matter
  @matter
end

#publishedObject

Returns the value of attribute published.



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

def published
  @published
end

#slug_dateObject

Returns the value of attribute slug_date.



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

def slug_date
  @slug_date
end

#summaryObject

Returns the value of attribute summary.



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

def summary
  @summary
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

#[](arg) ⇒ Object



56
57
58
# File 'lib/mkmatter/answers.rb', line 56

def [](arg)
  self.instance_variable_get("@#{arg}")
end

#to_hHash

Returns attribute ‘.matter`

Returns:

  • (Hash)

    returns attribute ‘.matter`



52
53
54
# File 'lib/mkmatter/answers.rb', line 52

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`



61
62
63
# File 'lib/mkmatter/answers.rb', line 61

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

#to_yaml(*args, **kwargs) ⇒ Object



65
66
67
# File 'lib/mkmatter/answers.rb', line 65

def to_yaml(*args, **kwargs)
  @matter.stringify_keys.to_yaml(*args, **kwargs)
end