Class: FXF::Parser
- Inherits:
-
Object
- Object
- FXF::Parser
- Defined in:
- lib/fxf.rb
Overview
‘FXF::Parser` isn’t intended to be used directly. FXF.parse creates a ‘FXF::Parser` and calls its `parse` method.
Defined Under Namespace
Classes: ObjectHolder
Instance Method Summary collapse
-
#build_array(dfn, arr) ⇒ Object
————————————————————————— build_array.
-
#build_hash(dfn, hsh) ⇒ Object
————————————————————————— build_hash.
-
#build_object(key) ⇒ Object
————————————————————————— build_object.
-
#initialize(raw) ⇒ Parser
constructor
————————————————————————— initialize.
-
#parse ⇒ Object
————————————————————————— parse.
-
#placeholder_add(placeholders, el) ⇒ Object
————————————————————————— placeholder_add.
-
#placeholder_mod ⇒ Object
————————————————————————— placeholder_mod.
Constructor Details
#initialize(raw) ⇒ Parser
initialize
223 224 225 |
# File 'lib/fxf.rb', line 223 def initialize(raw) @raw = raw end |
Instance Method Details
#build_array(dfn, arr) ⇒ Object
build_array
423 424 425 426 427 428 429 430 431 |
# File 'lib/fxf.rb', line 423 def build_array(dfn, arr) # loop through elements dfn.each do |ref| arr.push build_object(ref) end # return return arr end |
#build_hash(dfn, hsh) ⇒ Object
build_hash
406 407 408 409 410 411 412 413 414 |
# File 'lib/fxf.rb', line 406 def build_hash(dfn, hsh) # loop through elements dfn.each do |key, ref| hsh[key] = build_object(ref) end # return return hsh end |
#build_object(key) ⇒ Object
build_object
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/fxf.rb', line 351 def build_object(key) # $tm.hrm rv = nil # if already found if @found.has_key?(key) return @found[key] end # get object definition dfn = @org.delete(key) # scalar if FXF::SCALARS.include?(dfn.class) @found[key] = dfn return dfn # array elsif dfn.is_a?(Array) # if object description if dfn[0].is_a?(Hash) details = {} build_hash(dfn[0]['details'], details) dfn = FXF::Parser::ObjectHolder.new dfn[0]['scope'], dfn[0]['class'], details @found[key] = dfn return dfn else @found[key] = [] @collections.push @found[key] return build_array(dfn, @found[key]) end # hash elsif dfn.is_a?(Hash) @found[key] = {} @collections.push @found[key] return build_hash(dfn, @found[key]) # else unknown else puts 'unknown: ' + dfn.class.to_s exit end # return return rv end |
#parse ⇒ Object
parse
234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/fxf.rb', line 234 def parse() @full = JSON.parse(@raw) @org = @full['objects'] @found = {} @collections = [] rv = self.build_object(@full['root']) # look for object place holders placeholder_mod() # return return rv end |
#placeholder_add(placeholders, el) ⇒ Object
placeholder_add
335 336 337 338 339 340 341 342 |
# File 'lib/fxf.rb', line 335 def placeholder_add(placeholders, el) # $tm.hrm # if placeholder, and not in placeholders array, add to array if el.is_a?(FXF::Parser::ObjectHolder) and (not placeholders.include?(el)) placeholders.push el end end |
#placeholder_mod ⇒ Object
placeholder_mod
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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/fxf.rb', line 255 def placeholder_mod # $tm.hrm placeholders = [] phs_to_obj = {} # build unique list of placeholders @collections.each do |collection| if collection.is_a?(Array) collection.each do |el| placeholder_add placeholders, el end elsif collection.is_a?(Hash) collection.values.each do |el| placeholder_add placeholders, el end end end # build a hash of placeholders to objects placeholders.each do |ph| # standard if ph.scope == 'standard' if translator = FXF::Standard::FROM_FXF[ph.clss] phs_to_obj[ph] = translator.call(ph) else raise 'do-not-have-standard-translator: ' + ph.clss end # custom elsif ph.scope == 'custom' if Module.const_defined?(ph.clss) clss = Module.const_get(ph.clss) if clss.respond_to?('from_fxf') phs_to_obj[ph] = clss.from_fxf(ph.details) else raise 'class-does-not-have-from-fxf: ' + ph.clss end else raise 'unknown-custom-class: ' + ph.clss end # else don't know how to build this object else phs_to_obj[ph] = ph end end # substitute placeholders to objects @collections.each do |collection| if collection.is_a?(Array) collection.map! do |el| rv = nil if phs_to_obj.has_key?(el) rv = phs_to_obj[el] else rv = el end rv end elsif collection.is_a?(Hash) collection.each do |k, v| if phs_to_obj.has_key?(v) collection[k] = phs_to_obj[v] end end end end end |