Module: GqlSerializer
- Defined in:
- lib/gql_serializer.rb,
lib/gql_serializer/version.rb,
lib/gql_serializer/extensions.rb,
lib/gql_serializer/configuration.rb
Defined Under Namespace
Modules: ActiveRecord, Array, Hash, Object, Relation Classes: Configuration
Constant Summary collapse
- VERSION =
"3.0.1"
Class Method Summary collapse
- ._preload(records, include_hasharray) ⇒ Object
- .coerce_value(value) ⇒ Object
- .configuration ⇒ Object
- .configure {|configuration| ... } ⇒ Object
- .parse_query(input) ⇒ Object
- .query_include(model, hasharray) ⇒ Object
-
.serialize(records, hasharray, options, instructions = {}) ⇒ Object
example hasharray = [“id”, “name:real_name”, “tags”, { “panels” => [“id”, { “cards” => [“content”] }] }].
- .serialize_active_record(record, hasharray, options, instructions = {}) ⇒ Object
- .serialize_hash(record, hasharray, options, instructions = {}) ⇒ Object
- .serialize_object(record, hasharray, options, instructions = {}) ⇒ Object
Class Method Details
._preload(records, include_hasharray) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/gql_serializer/extensions.rb', line 55 def self._preload(records, include_hasharray) if ::ActiveRecord::VERSION::MAJOR >= 7 ::ActiveRecord::Associations::Preloader. new(records: records, associations: include_hasharray).call else ::ActiveRecord::Associations::Preloader. new.preload(records, include_hasharray) end end |
.coerce_value(value) ⇒ Object
139 140 141 142 143 144 |
# File 'lib/gql_serializer.rb', line 139 def self.coerce_value(value) return value.to_f if value.is_a? BigDecimal return value.new_offset(0).strftime("%FT%TZ") if value.is_a? DateTime return value.utc.iso8601 if value.is_a? Time value end |
.configuration ⇒ Object
8 9 10 |
# File 'lib/gql_serializer.rb', line 8 def self.configuration @configuration ||= Configuration.new end |
.configure {|configuration| ... } ⇒ Object
12 13 14 |
# File 'lib/gql_serializer.rb', line 12 def self.configure yield configuration end |
.parse_query(input) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/gql_serializer.rb', line 17 def self.parse_query(input) query = input.dup query.strip! query.gsub!(/[\r\n\t ]+/, ' ') query.gsub!(/\{ /, '{') query.gsub!(/ }/, '}') result, _ = self.parse_it(query) result end |
.query_include(model, hasharray) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/gql_serializer.rb', line 28 def self.query_include(model, hasharray) return [] if !model.respond_to? :reflections include_array = [] relations = model.reflections hasharray.each do |element| if element.is_a? String key = element.split(':')[0] include_array.push(key) if relations[key] elsif element.is_a? Hash key = element.keys.first.split(':')[0] relation_model = model.reflections&.[](key)&.klass next if relation_model.nil? relation_hasharray = self.query_include(relation_model, element.values.first) if relation_hasharray.empty? include_array.push(key) else include_array.push({key => relation_hasharray}) end end end include_array end |
.serialize(records, hasharray, options, instructions = {}) ⇒ Object
example hasharray = [“id”, “name:real_name”, “tags”, { “panels” => [“id”, { “cards” => [“content”] }] }]
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/gql_serializer.rb', line 52 def self.serialize(records, hasharray, , instructions = {}) if records.nil? return nil end if records.is_a?(Hash) return self.serialize_hash(records, hasharray, , instructions) end if records.respond_to? :map return records.map do |record| self.serialize(record, hasharray, , instructions) end end if records.class.respond_to? :reflections return self.serialize_active_record(records, hasharray, , instructions) end if !hasharray.empty? return self.serialize_object(records, hasharray, , instructions) end return coerce_value(records) end |
.serialize_active_record(record, hasharray, options, instructions = {}) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/gql_serializer.rb', line 79 def self.serialize_active_record(record, hasharray, , instructions = {}) id = "#{record.class}, #{hasharray}" instruction = instructions[id] if (!instruction) instruction = {klass: record.class, hasharray: hasharray, attributes: []} instructions[id] = instruction model = record.class all_relations = model.reflections.keys relations = hasharray.filter do |relation| key, _ = self.get_keys(relation, ) all_relations.include?(key) end if (hasharray - relations).empty? attributes = model.attribute_names + relations else attributes = hasharray end attributes.each do |attribute| key, alias_key, sub_hasharray = self.get_keys(attribute, ) instruction[:attributes].push({key: key, alias_key: alias_key, hasharray: sub_hasharray}) end end hash = {} instruction[:attributes].each do |attribute| value = record.public_send(attribute[:key]) hash[attribute[:alias_key]] = self.serialize(value, attribute[:hasharray], , instructions) end hash end |
.serialize_hash(record, hasharray, options, instructions = {}) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/gql_serializer.rb', line 126 def self.serialize_hash(record, hasharray, , instructions = {}) hash = {} attributes = hasharray.empty? ? record.keys : hasharray attributes.each do |attribute| key, alias_key, sub_hasharray = self.get_keys(attribute, ) value = self.get_hash_value(record, key) hash[alias_key] = self.serialize(value, sub_hasharray, , instructions) end hash end |
.serialize_object(record, hasharray, options, instructions = {}) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/gql_serializer.rb', line 114 def self.serialize_object(record, hasharray, , instructions = {}) hash = {} hasharray.each do |attribute| key, alias_key, sub_hasharray = self.get_keys(attribute, ) value = record.public_send(key) hash[alias_key] = self.serialize(value, sub_hasharray, , instructions) end hash end |