Module: MPS::Element

Defined Under Namespace

Modules: ClassMethods

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.



65
66
67
# File 'lib/mps/elements/element.rb', line 65

def body_str
  @body_str
end

#parsed_argsObject (readonly)

Returns the value of attribute parsed_args.



65
66
67
# File 'lib/mps/elements/element.rb', line 65

def parsed_args
  @parsed_args
end

#raw_argsObject (readonly)

Returns the value of attribute raw_args.



65
66
67
# File 'lib/mps/elements/element.rb', line 65

def raw_args
  @raw_args
end

Class Method Details

.included(base) ⇒ Object

Called when Element is included in a class. Extends the class with ClassMethods, enabling the ‘attribute` schema DSL.



7
8
9
# File 'lib/mps/elements/element.rb', line 7

def self.included(base)
  base.extend(ClassMethods)
end

.split_args(raw) ⇒ Object

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mps/elements/element.rb', line 48

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

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



67
68
69
70
71
72
73
# File 'lib/mps/elements/element.rb', line 67

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



75
76
77
# File 'lib/mps/elements/element.rb', line 75

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