Class: RightRails::JavaScriptGenerator::Util
- Inherits:
-
Object
- Object
- RightRails::JavaScriptGenerator::Util
- Defined in:
- lib/right_rails/java_script_generator.rb
Overview
We use this class to cleanup the main namespace of the JavaScriptGenerator instances So that the mesod_missing didn’t interferate with the util methods
Instance Attribute Summary collapse
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Instance Method Summary collapse
-
#build_script ⇒ Object
builds the end script.
-
#dom_id(record) ⇒ Object
returns a conventional dom id for the record.
-
#form_id_for(record) ⇒ Object
generates the form-id for the given record.
-
#initialize(template, thread = nil) ⇒ Util
constructor
A new instance of Util.
-
#make_call(string, parent = nil) ⇒ Object
builds a new method call object.
-
#proc_to_function {|args| ... } ⇒ Object
converts a proc into a javascript function.
-
#record(command) ⇒ Object
Records a new call.
-
#render(what) ⇒ Object
retnders the thing.
-
#to_js_args(args) ⇒ Object
converts the list of values into a javascript function arguments list.
-
#to_js_type(value) ⇒ Object
converts any ruby type into an javascript type.
-
#write(script) ⇒ Object
writes a pline script code into the thread.
Constructor Details
#initialize(template, thread = nil) ⇒ Util
Returns a new instance of Util.
193 194 195 196 |
# File 'lib/right_rails/java_script_generator.rb', line 193 def initialize(template, thread=nil) @template = template @thread = thread || [] end |
Instance Attribute Details
#template ⇒ Object (readonly)
Returns the value of attribute template.
191 192 193 |
# File 'lib/right_rails/java_script_generator.rb', line 191 def template @template end |
Instance Method Details
#build_script ⇒ Object
builds the end script
234 235 236 237 238 239 240 |
# File 'lib/right_rails/java_script_generator.rb', line 234 def build_script list = @thread.collect do |line| line.is_a?(String) ? line : (line.to_s + ';') end list.join('') end |
#dom_id(record) ⇒ Object
returns a conventional dom id for the record
199 200 201 202 203 204 205 |
# File 'lib/right_rails/java_script_generator.rb', line 199 def dom_id(record) if [String, Symbol].include?(record.class) "#{record}" else @template.dom_id(record) end end |
#form_id_for(record) ⇒ Object
generates the form-id for the given record
208 209 210 |
# File 'lib/right_rails/java_script_generator.rb', line 208 def form_id_for(record) record.new_record? ? "new_#{record.class.table_name.singularize}" : "edit_#{dom_id(record)}" end |
#make_call(string, parent = nil) ⇒ Object
builds a new method call object
218 219 220 |
# File 'lib/right_rails/java_script_generator.rb', line 218 def make_call(string, parent=nil) MethodCall.new(string, self, parent) end |
#proc_to_function {|args| ... } ⇒ Object
converts a proc into a javascript function
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/right_rails/java_script_generator.rb', line 297 def proc_to_function(&block) thread = [] args = [] names = [] name = 'a' page = RightRails::JavaScriptGenerator.new(@template, thread) block.arity.times do |i| args << page.get(name) names << name name = name.succ end # swapping the current thread with the block's one old_thread = @thread @thread = thread yield(*args) # swapping the current therad back @thread = old_thread "function(#{names.join(',')}){#{page.to_s}}" end |
#record(command) ⇒ Object
Records a new call
223 224 225 226 |
# File 'lib/right_rails/java_script_generator.rb', line 223 def record(command) @thread << (line = make_call(command)) line end |
#render(what) ⇒ Object
retnders the thing
213 214 215 |
# File 'lib/right_rails/java_script_generator.rb', line 213 def render(what) @template.render(what) end |
#to_js_args(args) ⇒ Object
converts the list of values into a javascript function arguments list
243 244 245 246 247 248 249 |
# File 'lib/right_rails/java_script_generator.rb', line 243 def to_js_args(args) list = args.collect do |value| to_js_type(value) end list.join(',') end |
#to_js_type(value) ⇒ Object
converts any ruby type into an javascript type
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/right_rails/java_script_generator.rb', line 252 def to_js_type(value) case value.class.name.to_sym when :Float, :Fixnum, :TrueClass, :FalseClass, :Symbol then value.to_s when :NilClass then 'null' when :Array then "[#{to_js_args(value)}]" when :Proc then proc_to_function(&value) else # the other method-calls processing if value.is_a?(MethodCall) # removing the call out of the calls thread top = value parent = value while parent top = parent parent = parent.instance_variable_get('@parent') end @thread.reject!{ |item| item == top } value.to_s # converting all sorts of strings elsif value.is_a?(String) "\"#{@template.escape_javascript(value)}\"" # simple hashes processing elsif value.is_a?(Hash) pairs = [] value.each do |key, value| pairs << "#{to_js_type(key)}:#{to_js_type(value)}" end "{#{pairs.sort.join(',')}}" # JSON exportable values processing elsif value.respond_to?(:to_json) to_js_type(value.to_json) # throwing an ansupported class name else throw "RightRails::JavaScriptGenerator doesn't support instances of #{value.class.name} yet" end end end |
#write(script) ⇒ Object
writes a pline script code into the thread
229 230 231 |
# File 'lib/right_rails/java_script_generator.rb', line 229 def write(script) @thread << script end |