Class: SDL::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/sdl/parser.rb

Overview

The parser takes a string and converts it to a Field. A field is described using a series of directives separated by a colon.

Examples:

  • title:string{120}
  • body:text:nullable
  • status:enum{draft,published}
  • user:belongs_to:foreign_key
  • image:has_one_attached

Available directives:

  • id
  • string
  • string{limit}
  • boolean
  • integer
  • integer{limit}
  • float
  • decimal
  • decimal{precision,scale}
  • date
  • datetime
  • text
  • text{limit}
  • binary
  • binary{limit}
  • enum{value,...}
  • belongs_to
  • belongs_to{model}
  • has_one
  • has_one{model}
  • has_many
  • has_many{model}
  • has_one_attached
  • has_many_attached
  • unique
  • nullable
  • index
  • foreign_key
  • default{value}

Instance Method Summary collapse

Instance Method Details

#parse(value) ⇒ Field

Parses a string into a Field

Parameters:

  • value (String)

Returns:

Raises:

  • (ParseError)

    when a directive is not recognized



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sdl/parser.rb', line 58

def parse(value)
  name, *args = value.split(":")
  name = name.to_sym

  opts = {type: :string}
  args.each { |arg| parse!(arg, opts) }
  type = opts.delete(:type)

  # Attempt to coerce the default
  opts[:default] = coerce(opts[:default], type) if opts[:default]

  if type.is_a?(Symbol)
    Attribute.new(name, type, **opts)
  else
    type.new(name, **opts)
  end
end