Module: SatoriLikeDictionary::Renderer

Included in:
Entry, Word
Defined in:
lib/satori_like_dictionary.rb

Overview

satori token renderer

Constant Summary collapse

NUMBERS =

number map

{"" => 0, "" => 1, "" => 2, "" => 3, "" => 4, "" => 5, "" => 6, "" => 7, "" => 8, "" => 9}

Instance Method Summary collapse

Instance Method Details

#render(events, request) ⇒ String|OpenStruct

render the content

Parameters:

  • events (Events)

    events definition

  • request (OpenStruct)

    request hash

Returns:

  • (String|OpenStruct)

    result


147
148
149
150
151
152
153
154
# File 'lib/satori_like_dictionary.rb', line 147

def render(events, request)
  value = render_base(events, request)
  if request.__satori_target_character
    OpenStruct.new({Value: value, Reference0: request.__satori_target_character})
  else
    value
  end
end

#render_base(events, request) ⇒ String

process basic replacements

Parameters:

  • events (Events)

    events definition

  • request (OpenStruct)

    request hash

Returns:

  • (String)

    result


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/satori_like_dictionary.rb', line 115

def render_base(events, request)
  value = render_template(events, request)
    .gsub(/\b_(\S+)/, "\\q[\\1,\\1]")
    .gsub(/(([^)]*))/) do
      content = $1
      if content.match(/^[0-90-9]+$/)
        "\\s[#{content.gsub(/[0-9]/) {|m| NUMBERS[m]} }]"
      else
        begin
          entry = events.send(content, request) # event
        rescue ArgumentError
          entry = events.send(content) # satori dictionary
        rescue NoMethodError
          entry = nil # wrong event
        end
        if entry.respond_to?(:render) # satori entry
          entry.render(events, request)
        elsif entry.respond_to?(:Value) # ostruct value
          entry.Value
        else # simple value
          entry
        end
      end
    end
    .gsub(/\r?\n/, "\\n")
  value
end

#render_template(events, request) ⇒ String

execute template

Parameters:

  • events (Events)

    events definition

  • request (OpenStruct)

    request hash

Returns:

  • (String)

    result


105
106
107
108
109
# File 'lib/satori_like_dictionary.rb', line 105

def render_template(events, request)
  template = NanoTemplate.new.template(to_template)
  context = TemplateContext.new(events, request)
  template.call(context)
end