Module: MPS::Element

Constant Summary collapse

PADDING =
'  '

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#body_strObject (readonly)

Returns the value of attribute body_str.



27
28
29
# File 'lib/mps/elements/element.rb', line 27

def body_str
  @body_str
end

#disp_strObject

Returns the value of attribute disp_str.



26
27
28
# File 'lib/mps/elements/element.rb', line 26

def disp_str
  @disp_str
end

#parsed_argsObject (readonly)

Returns the value of attribute parsed_args.



27
28
29
# File 'lib/mps/elements/element.rb', line 27

def parsed_args
  @parsed_args
end

#raw_argsObject (readonly)

Returns the value of attribute raw_args.



27
28
29
# File 'lib/mps/elements/element.rb', line 27

def raw_args
  @raw_args
end

Class Method Details

.split_args(raw) ⇒ Object

Parses “work, release, status: done” → { attrs: { status: “done” }, tags: [“work”, “release”] } Parts containing “:” become named attrs; bare words become tags.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mps/elements/element.rb', line 9

def self.split_args(raw)
  return { attrs: {}, tags: [] } if raw.nil? || raw.strip.empty?
  attrs = {}
  tags  = []
  raw.split(",").each do |part|
    part = part.strip
    next if part.empty?
    if part.include?(":")
      k, v = part.split(":", 2).map(&:strip)
      attrs[k.to_sym] = v
    else
      tags << part
    end
  end
  { attrs: attrs, tags: tags }
end

Instance Method Details

#display_str(padding_size = @refs.size - 1) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/mps/elements/element.rb', line 41

def display_str(padding_size = @refs.size - 1)
  strs = @body_str.strip.lines.map(&:strip)
  header = strs.first
  res_strs = [(PADDING * padding_size) + header]
  strs[1..].each { |str| res_strs << (PADDING * padding_size) + str }
  res_strs.join("\n")
end

#initialize(args:, refs:, body_str:) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/mps/elements/element.rb', line 29

def initialize(args:, refs:, body_str:)
  @raw_args    = args.to_s
  @refs        = refs
  @body_str    = body_str
  @ref         = refs.map(&:to_s).join(".")
  @parsed_args = self.class.respond_to?(:parse_args) ? self.class.parse_args(@raw_args) : {}
end

#tagsObject



37
38
39
# File 'lib/mps/elements/element.rb', line 37

def tags
  @parsed_args.fetch(:tags, [])
end