Module: JsonPersist
- Defined in:
- lib/ascension/to_json.rb
Instance Method Summary collapse
- #as_json(*args) ⇒ Object
- #new_hash_json(attr, h, obj) ⇒ Object
- #to_json(*args) ⇒ Object
- #to_json_hash ⇒ Object
Instance Method Details
#as_json(*args) ⇒ Object
3 4 5 |
# File 'lib/ascension/to_json.rb', line 3 def as_json(*args) to_json_hash end |
#new_hash_json(attr, h, obj) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/ascension/to_json.rb', line 6 def new_hash_json(attr,h,obj) if obj.can_mongo_convert? if obj.respond_to?(:select) && false h.merge(attr => obj.to_mongo_hash) elsif [Numeric,String].any? { |c| obj.kind_of?(c) } h.merge(attr => obj) else h.merge(attr => obj.as_json) end else h end rescue return h end |
#to_json(*args) ⇒ Object
42 43 44 |
# File 'lib/ascension/to_json.rb', line 42 def to_json(*args) as_json(*args).to_json end |
#to_json_hash ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ascension/to_json.rb', line 21 def to_json_hash res = mongo_child_attributes.inject({}) do |h,attr| obj = send(attr) #raise "#{attr} is nil" unless obj new_hash_json(attr,h,obj) end.merge("_mongo_class" => self.class.to_s) klass.mongo_reference_attributes.each do |attr| val = send(attr) res[attr] = val.to_mongo_ref_hash if val end if respond_to?(:addl_json_attributes) && true puts "in addl_json_attributes part" addl = [addl_json_attributes].flatten.select { |x| x } addl.each do |attr| puts "addl attr #{attr}" res = new_hash_json(attr,res,send(attr)) end end res end |