Class: MarkdownComposer::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown_composer/source.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, type: "explicit", title: nil, markdown: nil, html: nil, preferred_format: :markdown, metadata: {}) ⇒ Source

Returns a new instance of Source.



15
16
17
18
19
20
21
22
23
# File 'lib/markdown_composer/source.rb', line 15

def initialize(key:, type: "explicit", title: nil, markdown: nil, html: nil, preferred_format: :markdown, metadata: {})
  @key = key.to_s
  @type = type.to_s
  @title = title
  @markdown = markdown
  @html = html
  @preferred_format = preferred_format.to_sym
  @metadata =  || {}
end

Instance Attribute Details

#htmlObject (readonly)

Returns the value of attribute html.



5
6
7
# File 'lib/markdown_composer/source.rb', line 5

def html
  @html
end

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/markdown_composer/source.rb', line 5

def key
  @key
end

#markdownObject (readonly)

Returns the value of attribute markdown.



5
6
7
# File 'lib/markdown_composer/source.rb', line 5

def markdown
  @markdown
end

#metadataObject (readonly)

Returns the value of attribute metadata.



5
6
7
# File 'lib/markdown_composer/source.rb', line 5

def 
  @metadata
end

#preferred_formatObject (readonly)

Returns the value of attribute preferred_format.



5
6
7
# File 'lib/markdown_composer/source.rb', line 5

def preferred_format
  @preferred_format
end

#titleObject (readonly)

Returns the value of attribute title.



5
6
7
# File 'lib/markdown_composer/source.rb', line 5

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/markdown_composer/source.rb', line 5

def type
  @type
end

Class Method Details

.build(value) ⇒ Object



7
8
9
# File 'lib/markdown_composer/source.rb', line 7

def self.build(value)
  value.is_a?(Source) ? value : new(**symbolize(value))
end

.symbolize(value) ⇒ Object



11
12
13
# File 'lib/markdown_composer/source.rb', line 11

def self.symbolize(value)
  value.to_h.transform_keys(&:to_sym)
end

Instance Method Details

#contentObject



33
34
35
# File 'lib/markdown_composer/source.rb', line 33

def content
  format == :html ? html.to_s : markdown.to_s
end

#formatObject



25
26
27
28
29
30
31
# File 'lib/markdown_composer/source.rb', line 25

def format
  return :html if preferred_format == :html && html_present?
  return :markdown if markdown_present?
  return :html if html_present?

  preferred_format
end

#html_present?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/markdown_composer/source.rb', line 41

def html_present?
  !html.to_s.empty?
end

#markdown_present?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/markdown_composer/source.rb', line 37

def markdown_present?
  !markdown.to_s.empty?
end

#to_hObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/markdown_composer/source.rb', line 45

def to_h
  {
    key: key,
    type: type,
    title: title,
    markdown: markdown,
    html: html,
    preferred_format: preferred_format,
    metadata: 
  }.compact
end