Module: MPS::Element::ClassMethods

Defined in:
lib/mps/elements/element.rb

Instance Method Summary collapse

Instance Method Details

#_schemaObject

Internal mutable schema hash.



24
25
26
# File 'lib/mps/elements/element.rb', line 24

def _schema
  @_schema ||= {}
end

#attribute(name, type: :string, default: nil, flag: nil, aliases: []) ⇒ Object

Declare a typed attribute for this element class.

Parameters:

  • name (Symbol)

    parsed_args key

  • type (Symbol) (defaults to: :string)

    :string, :time, :integer (for future coercion)

  • default (defaults to: nil)

    value when the attribute is absent from args

  • flag (String) (defaults to: nil)

    CLI flag name (without –); nil = not filterable

  • aliases (Array<String>) (defaults to: [])

    short CLI aliases for this flag



19
20
21
# File 'lib/mps/elements/element.rb', line 19

def attribute(name, type: :string, default: nil, flag: nil, aliases: [])
  _schema[name] = { type: type, default: default, flag: flag, aliases: Array(aliases) }
end

#parse_args(raw) ⇒ Object

Schema-driven parse_args. Subclasses do NOT need to override this. Any key: value pair in raw whose key matches a declared attribute name is extracted; remaining bare words become tags.



36
37
38
39
40
41
42
43
# File 'lib/mps/elements/element.rb', line 36

def parse_args(raw)
  split = ::MPS::Element.split_args(raw)
  result = { tags: split[:tags] }
  _schema.each do |name, defn|
    result[name] = split[:attrs].fetch(name, defn[:default])
  end
  result
end

#schemaObject

Frozen public view of the schema.



29
30
31
# File 'lib/mps/elements/element.rb', line 29

def schema
  _schema.dup.freeze
end