Class: MdDoc

Inherits:
Gloo::Core::Obj
  • Object
show all
Defined in:
lib/md_doc.rb

Constant Summary collapse

KEYWORD =
'md_doc'.freeze
KEYWORD_SHORT =
'md_doc'.freeze
PATH =
'path'.freeze
FRONTMATTER =
'frontmatter'.freeze
BODY =
'body'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.messagesObject

Get a list of message names that this object receives.



76
77
78
# File 'lib/md_doc.rb', line 76

def self.messages
  return super + %w[read write]
end

.short_typenameObject

The short name of the object type.



34
35
36
# File 'lib/md_doc.rb', line 34

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



27
28
29
# File 'lib/md_doc.rb', line 27

def self.typename
  return KEYWORD
end

Instance Method Details

#add_children_on_create?Boolean

Does this object have children to add when an object is created in interactive mode?

Returns:

  • (Boolean)


54
55
56
# File 'lib/md_doc.rb', line 54

def add_children_on_create?
  return true
end

#add_default_childrenObject

Add the default children: path, frontmatter, body.



61
62
63
64
65
66
# File 'lib/md_doc.rb', line 61

def add_default_children
  fac = @engine.factory
  fac.create_file PATH, nil, self
  fac.create_can FRONTMATTER, self
  fac.create_text BODY, nil, self
end

#msg_readObject

Read the file at path, parse frontmatter and body, populate children. Frontmatter children are created dynamically from whatever keys are present.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/md_doc.rb', line 84

def msg_read
  path = resolve_path
  return unless path

  unless File.exist?( path )
    @engine.log.error "md_doc file not found: #{path}"
    return
  end

  content = File.read( path )
  fm_hash, body_text = parse_frontmatter( content )

  fm_can = find_child FRONTMATTER
  if fm_can && fm_hash
    fm_hash.each do |key, val|
      child = fm_can.find_add_child( key.to_s, 'string' )
      child.set_value val.to_s
    end
  end

  body = find_child BODY
  body.set_value( body_text ) if body
end

#msg_writeObject

Serialize frontmatter and body children back to the file at path. Creates the file if it does not exist.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/md_doc.rb', line 112

def msg_write
  path = resolve_path
  return unless path

  fm_can = find_child FRONTMATTER
  fm_hash = {}
  if fm_can
    fm_can.children.each do |child|
      fm_hash[ child.name ] = child.value
    end
  end

  body = find_child BODY
  body_text = body ? body.value.to_s : ''

  content = build_content( fm_hash, body_text )
  File.write( File.expand_path( path ), content )
end

#set_value(new_value) ⇒ Object

Set the value with any necessary type conversions.



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

def set_value( new_value )
  self.value = new_value.to_s
end