Class: AMF::ClassMapping

Inherits:
Object
  • Object
show all
Defined in:
lib/amf/class_mapping.rb

Defined Under Namespace

Classes: MappingSet

Instance Method Summary collapse

Instance Method Details

#deep_const_get(path) ⇒ Object

Return the constant located at path. The format of path has to be either ::A::B::C or A::B::C. In any case A has to be located at the top level (absolute namespace path?). If there doesn’t exist a constant at the given path, an ArgumentError is raised.



107
108
109
110
111
112
113
114
115
116
# File 'lib/amf/class_mapping.rb', line 107

def deep_const_get(path) # :nodoc:
  path = path.to_s
  path.split(/::/).inject(Object) do |p, c|
    case
    when c.empty?             then p
    when p.const_defined?(c)  then p.const_get(c)
    else                      raise ArgumentError, "can't find const #{path}"
    end
  end
end

#default_as_prefixObject



64
65
66
# File 'lib/amf/class_mapping.rb', line 64

def default_as_prefix
  mappings.default_as_prefix
end

#default_as_prefix=(value) ⇒ Object



68
69
70
# File 'lib/amf/class_mapping.rb', line 68

def default_as_prefix=(value)
  mappings.default_as_prefix = value
end

#define {|mappings| ... } ⇒ Object

Define class mappings in the block. Block is passed a MappingSet object as the first parameter.

Example:

AMF::ClassMapper.define do |m|
  m.map :as => 'AsClass', :ruby => 'RubyClass'
end

Yields:

  • (mappings)


60
61
62
# File 'lib/amf/class_mapping.rb', line 60

def define #:yields: mapping_set
  yield mappings
end

#get_as_class_name(obj) ⇒ Object

Returns the AS class name for the given ruby object. Will also take a string containing the ruby class name



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/amf/class_mapping.rb', line 74

def get_as_class_name(obj)
  # Get class name
  if obj.is_a?(String)
    ruby_class_name = obj
  else
    ruby_class_name = obj.class.name
  end

  # Get mapped AS class name
  mappings.get_as_class_name(ruby_class_name)
end

#get_ruby_class_name(as_class_name) ⇒ Object



86
87
88
# File 'lib/amf/class_mapping.rb', line 86

def get_ruby_class_name(as_class_name)
  mappings.get_ruby_class_name(as_class_name.to_s)
end

#get_ruby_obj(as_class_name) ⇒ Object

Instantiates a ruby object using the mapping configuration based on the source AS class name. If there is no mapping defined, it returns a hash.



92
93
94
95
96
97
98
99
100
101
# File 'lib/amf/class_mapping.rb', line 92

def get_ruby_obj(as_class_name)
  ruby_class_name = mappings.get_ruby_class_name(as_class_name)
  if ruby_class_name.nil?
    # Populate a simple hash, since no mapping
    return Hash.new
  else
    ruby_class = deep_const_get(ruby_class_name)
    return ruby_class.new
  end
end

#populate_ruby_obj(obj, props, dynamic_props = nil) ⇒ Object

Populates the ruby object using the given properties



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/amf/class_mapping.rb', line 119

def populate_ruby_obj(obj, props, dynamic_props=nil)
  props.merge! dynamic_props if dynamic_props
  hash_like = obj.respond_to?("[]=")
  props.each do |key, value|
    if obj.respond_to?("#{key}=")
      obj.send("#{key}=", value)
    elsif hash_like
      obj[key.to_sym] = value
    end
  end
  obj
end