Top Level Namespace

Defined Under Namespace

Modules: Linguify Classes: Proc, Ruby2Js, Sexp, String

Instance Method Summary collapse

Instance Method Details

#reduce(regexp, &code) ⇒ Object

Defines a reduction rule.

Examples:

Define a reduction rule that reduce a text to a javascript reduction named query.

reduce /a possible javascript NOSQL query/ => {:to => 'query', :lang => :js} do
  @db.forEach(lambda{ |record|
      emit(record);
    }
  )
end

Parameters:

  • pattern (Hash)

    The step matching pattern

  • code (Proc)

    The code

    The step matching pattern only one key-value pair, where the

    key is the step matching pattern and the
    value is on of:
      nil      - indicating the last reduction,
      +String+ - the name of the reduction,
      +Hash+   - the name of the reduction and adittional parameters
    

    Supported parameters:

    :to     - the name of the reduction
    :lang   - what language the code block translates to. (:ruby or :js)
    :inline - if true, calls to this reduction will be inlined
    


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/linguify.rb', line 55

def reduce(regexp,&code)
  rule = if regexp.kind_of? Regexp
    {
      :match  => regexp,
      :result => '',
      :lang   => :ruby,
      :inline => false,
      :proc => code
    }
  elsif regexp.values[0].kind_of?(Hash)
    {
      :match  => regexp.keys[0],
      :result => regexp.values[0][:to]     || '',
      :lang   => regexp.values[0][:lang]   || :ruby,
      :inline => regexp.values[0][:inline] || false,
      :proc => code
    }
  else
   {
      :match  => regexp.keys[0],
      :result => regexp.values[0],
      :lang   => :ruby,
      :inline => false,
      :proc => code
    }
  end
  Linguify::rules << rule
end