Class: PeakFlowUtils::ParseJson
- Inherits:
-
Object
- Object
- PeakFlowUtils::ParseJson
- Defined in:
- app/services/peak_flow_utils/parse_json.rb
Instance Method Summary collapse
- #active_record?(object) ⇒ Boolean
-
#initialize(object) ⇒ ParseJson
constructor
A new instance of ParseJson.
- #parse ⇒ Object
- #parse_to_json(object) ⇒ Object
Constructor Details
#initialize(object) ⇒ ParseJson
Returns a new instance of ParseJson.
2 3 4 |
# File 'app/services/peak_flow_utils/parse_json.rb', line 2 def initialize(object) @object = object end |
Instance Method Details
#active_record?(object) ⇒ Boolean
10 11 12 13 14 |
# File 'app/services/peak_flow_utils/parse_json.rb', line 10 def active_record?(object) object.class.ancestors.any? do |ancestor| ancestor.name == "ActiveRecord::Base" end end |
#parse ⇒ Object
6 7 8 |
# File 'app/services/peak_flow_utils/parse_json.rb', line 6 def parse parse_to_json(@object) end |
#parse_to_json(object) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/services/peak_flow_utils/parse_json.rb', line 16 def parse_to_json(object) if object.is_a?(Hash) result = {} object.each do |key, value| result[key] = parse_to_json(value) end return result elsif object.is_a?(Array) return object.map do |value| parse_to_json(value) end elsif active_record?(object) result = "#<#{object.class.name} id: #{object.id}" result << " name: \"#{object.name}\"" if object.respond_to?(:name) result << ">" return result end object end |