Methods
Public Class methods
Populate an object from a hash of values. This is a truly dangerous method.
Options
- name
- force_boolean
[ show source ]
# File lib/raw/util/attr.rb, line 31
31: def populate_object(obj, values, options = {})
32: options = {
33: :force_boolean => true
34: }.update(options)
35:
36: # If a class is passed create an instance.
37:
38: obj = obj.new if obj.is_a?(Class)
39:
40: for sym in obj.class.serializable_attributes
41: anno = obj.class.ann(sym)
42:
43: unless options[:all]
44: # THINK: should skip control none attributes?
45: next if sym == obj.class.primary_key or anno[:control] == :none or anno[:disable_control]
46: end
47:
48: prop_name = sym.to_s
49:
50: # See if there is an incoming request param for this prop.
51:
52: if values.keys.include? prop_name
53:
54: prop_value = values[prop_name]
55:
56: # to_s must be called on the prop_value incase the
57: # request is IOString.
58:
59: prop_value = prop_value.to_s unless prop_value.is_a?(Hash) or prop_value.is_a?(Array)
60:
61: # If property is a Blob dont overwrite current
62: # property's data if "".
63:
64: break if anno[:class] == Og::Blob and prop_value.empty?
65:
66: prop_value = CGI.unescape(prop_value)
67:
68: if anno[:class] == String and anno[:unfiltered] != true
69: # html filter all strings by default.
70: prop_value = prop_value.html_filter
71: end
72:
73: set_attr(obj, prop_name, CGI.unescape(prop_value))
74:
75: elsif options[:force_boolean] and (anno[:class] == TrueClass or anno[:class] == FalseClass)
76: # Set a boolean property to false if it is not in the
77: # request. Requires force_boolean == true.
78:
79: set_attr(obj, prop_name, false)
80: obj.send("__force_#{prop_name}", false)
81: end
82: end
83:
84: if options[:assign_relations]
85: for rel in obj.class.relations
86: unless options[:all]
87: next if rel.options[:control] == :none or rel.options[:disable_control]
88: end
89:
90: rel_name = rel.name.to_s
91:
92: # Renew the relations from values
93:
94: if rel.kind_of?(Og::RefersTo)
95: if foreign_oid = values[rel_name]
96: foreign_oid = foreign_oid.to_s unless foreign_oid.is_a?(Hash) or foreign_oid.is_a?(Array)
97: foreign_oid = nil if foreign_oid == 'nil' or foreign_oid == 'none'
98: end
99: set_attr(obj, rel.foreign_key, foreign_oid)
100: elsif rel.kind_of?(Og::JoinsMany) || rel.kind_of?(Og::HasMany)
101: collection = obj.send(rel_name)
102: collection.remove_all
103: if values.has_key?(rel_name)
104: primary_keys = values[rel_name]
105: primary_keys.each do |v|
106: v = v.to_s
107: next if v == "nil" or v == "none"
108: collection << rel.target_class[v.to_i]
109: end
110: end
111: end
112: end
113: end
114:
115: #--
116: # gmosx, FIXME: this is a hack, will be replaced with proper
117: # code soon.
118: #++
119:
120: for callback in obj.class.assign_callbacks
121: callback.call(obj, values, options)
122: end if obj.class.respond_to?(:assign_callbacks)
123:
124: return obj
125: end
[ show source ]
# File lib/raw/util/attr.rb, line 17
17: def set_attr(obj, name, value)
18: obj.send("__force_#{name}", value)
19: rescue Object => ex
20: obj.instance_variable_set("@#{name}", value)
21: end