Module: JsonApiClient::Utils
- Defined in:
- lib/json_api_client/utils.rb
Class Method Summary collapse
Class Method Details
.compute_type(klass, type_name) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/json_api_client/utils.rb', line 4 def self.compute_type(klass, type_name) return klass.custom_type_to_class.fetch(type_name).constantize if klass.custom_type_to_class.key?(type_name) # If the type is prefixed with a scope operator then we assume that # the type_name is an absolute reference. return type_name.constantize if type_name.match(/^::/) # Check the klass association definitions association_klass_match = klass.associations.find { |a| a.attr_name.to_s.singularize == type_name.underscore } association_klass = association_klass_match.[:class] if association_klass_match return association_klass if association_klass # Build a list of candidates to search for candidates = [] klass.name.scan(/::|$/) { candidates.unshift "#{$`}::#{type_name}" } candidates << type_name candidates.each do |candidate| begin constant = candidate.constantize return constant if candidate == constant.to_s rescue NameError => e # We don't want to swallow NoMethodError < NameError errors raise e unless e.instance_of?(NameError) end end raise NameError, "uninitialized constant #{candidates.first}" end |
.parse_includes(klass, *tables) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/json_api_client/utils.rb', line 33 def self.parse_includes(klass, *tables) tables.map do |table| case table when Hash table.map do |k, v| parse_includes(klass, *v).map do |sub| "#{k}.#{sub}" end end when Array table.map do |v| parse_includes(klass, *v) end else klass.key_formatter.format(table) end end.flatten end |