⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vo_helper.rb

📁 Flex for rails的开发demo源码
💻 RB
字号:
module RubyAMF
  module VoHelper
    class VoHash < Hash
      attr_accessor :_explicitType
      attr_accessor :rmembers
      # the rest of the members can just be entered as ["keys"]=
    end
    
    require 'app/configuration' # cant put this at the top because VoHash has to be instantiated for app/configuration to work
    class VoUtil
      
      include RubyAMF::Configuration
      include RubyAMF::Exceptions
      
      def self.get_vo_hash_for_incoming(vo_hash, actionscript_class)
        unless map = ClassMappings.get_vo_mapping_for_actionscript_class(actionscript_class)
          return vo_hash #if no mappings return the voHash
        end
        vo_hash._explicitType = map[:ruby]
        if map[:type] == "active_record"
          return self.get_active_record_from_vo_hash(vo_hash, map) #if this was an active record VO, return it prematurely
        else
          return vo_hash
          begin
            ruby_obj = map[:ruby].constantize.new #this returns an instance of the Ruby class
          rescue NameError => e
            r||=0
            r+=1
            require ClassMappings.path + '/' + map[:ruby].underscore + '.rb'   #require the file
            retry if r<=1
          rescue ArgumentError => e
            puts map[:ruby]
            puts e.message
            raise RUBYAMFException.new(RUBYAMFException.VO_ERROR, "Please ensure that #{map[:ruby]} does not require arguments for initialization")
          end
        end
        
        #assign values to new VO object
        vo_hash.each do |attribute, value|
          attribute = attribute.to_s.dup
          attribute.to_snake! if ClassMappings.translate_case
          ruby_obj.instance_variable_set("@#{attribute}", value)          
        end
        
        #Assing RubyAMF tracking vars
        # ruby_obj._explicitType = map[:ruby] #assign the VO it's 'mapped_to' classname
        ruby_obj
      rescue LoadError => le
        puts le.message
        puts le.backtrace
        raise RUBYAMFException.new(RUBYAMFException.VO_ERROR, "Tho VO definition #{actionscript_class} could not be found. #{le.message}")
      end
      
      # Aryk: I tried to make this more efficent and clean.
      def self.get_vo_hash_for_outgoing(obj)
        new_object = VoHash.new #use VoHash because one day, we might do away with the class Object patching
        instance_vars = obj.instance_variables
        if map = ClassMappings.get_vo_mapping_for_ruby_class(obj.class.to_s)
          if map[:type]=="active_record"
            attributes_hash = obj.attributes
            (map[:attributes]||attributes_hash.keys).each do |attr| # need to use dup because sometimes the attr is frozen from the AR attributes hash
              attr_name = attr
              attr_name = attr_name.dup.to_camel! if ClassMappings.translate_case # need to do it this way because the string might be frozen if it came from the attributes_hash.keys
              new_object[attr_name] = attributes_hash[attr]
            end
            instance_vars = [] # reset the instance_vars for the associations, this way no unwanted instance variables (ie @new_record, @read_only) can get through
            # Note: if you did not specify associations, it will not show up even if you eager loaded them.
            if map[:associations] # Aryk: if they opted for assocations, make sure that they are loaded in. This is great for composed_of, since it cannot be included on a find
              map[:associations].each do |assoc|
                instance_vars << ("@"+assoc) if obj.send(assoc) # this will make sure they are instantiated and only load it if they have a value.
              end
            elsif ClassMappings.check_for_associations
              instance_vars = obj.instance_variables.reject{|assoc| ["@attributes","@new_record","@read_only"].include?(assoc)}
            end
          end
          new_object._explicitType = map[:actionscript] # Aryk: This only works on the Hash because rubyAMF extended class Object to have this accessor, probably not the best idea, but its already there.   
        end
        instance_vars.each do |var| # this also picks up the eager loaded associations, because association called "has_many_assoc" has an instance variable called "@has_many_assoc"
          attr_name = var[1..-1]
          attr_name.to_camel! if ClassMappings.translate_case
          new_object[attr_name] = obj.instance_variable_get(var)
        end
        new_object
      rescue Exception => e
        puts e.message
        puts e.backtrace
        raise RUBYAMFException.new(RUBYAMFException.VO_ERROR, e.message)
      end
    end
  end
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -