Sha256: 097f4cd7ca964ea8f73f90c9bc523f50f329866f2d7a0787fc83ffff04d49613

Contents?: true

Size: 1006 Bytes

Versions: 4

Compression:

Stored size: 1006 Bytes

Contents

class Trix.BasicObject
  @proxyMethod: (expression) ->
    {name, toMethod, toProperty, optional} = parseProxyMethodExpression(expression)

    @::[name] = ->
      object = if toMethod?
        if optional then @[toMethod]?() else @[toMethod]()
      else if toProperty?
        @[toProperty]

      if optional
        subject = object?[name]
        apply.call(subject, object, arguments) if subject?
      else
        subject = object[name]
        apply.call(subject, object, arguments)

  parseProxyMethodExpression = (expression) ->
    unless match = expression.match(proxyMethodExpressionPattern)
      throw new Error "can't parse @proxyMethod expression: #{expression}"

    args = name: match[4]

    if match[2]?
      args.toMethod = match[1]
    else
      args.toProperty = match[1]

    if match[3]?
      args.optional = true

    args

  {apply} = Function.prototype

  proxyMethodExpressionPattern = ///
    ^
      (.+?)
        (\(\))?
        (\?)?
      \.
      (.+?)
    $
  ///

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
vapid-0.1.3 lib/vapid/vendor/trix/src/trix/core/basic_object.coffee
vapid-0.1.2 lib/vapid/vendor/trix/src/trix/core/basic_object.coffee
vapid-0.1.1 lib/vapid/vendor/trix/src/trix/core/basic_object.coffee
vapid-0.1.0 lib/vapid/vendor/trix/src/trix/core/basic_object.coffee