class FXF::Parser

`FXF::Parser` isn't intended to be used directly. FXF.parse creates a `FXF::Parser` and calls its `parse` method.

Public Class Methods

new(raw) click to toggle source
# File lib/fxf.rb, line 223
def initialize(raw)
        @raw = raw
end

Public Instance Methods

build_array(dfn, arr) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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