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, Relation Classes: Configuration
Constant Summary collapse
- VERSION =
"2.2.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”, “tags”, { “panels” => [“id”, { “cards” => [“content”] }] }].
Class Method Details
._preload(records, include_hasharray) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/gql_serializer/extensions.rb', line 39 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
119 120 121 122 123 124 |
# File 'lib/gql_serializer.rb', line 119 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 |
# File 'lib/gql_serializer.rb', line 28 def self.query_include(model, hasharray) 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 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”, “tags”, { “panels” => [“id”, { “cards” => [“content”] }] }]
50 51 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 78 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 113 114 115 116 117 |
# File 'lib/gql_serializer.rb', line 50 def self.serialize(records, hasharray, , instructions = {}) if records.nil? return nil end if records.respond_to? :map return records.map do |record| self.serialize(record, hasharray, , instructions) end end record = records id = "#{record.class}, #{hasharray}" instruction = instructions[id] if (!instruction) instruction = {klass: record.class, hasharray: hasharray, relations: [], attributes: []} instructions[id] = instruction model = record.class all_relations = model.reflections.keys relations = hasharray.filter do |relation| next true if !relation.is_a?(String) key, alias_key = relation.split(':') all_relations.include?(key) end attributes = hasharray - relations attributes = model.attribute_names if attributes.empty? attributes.each do |attribute| key, alias_key = attribute.split(':') alias_key = apply_case(alias_key || key, [:case]) instruction[:attributes].push({key: key, alias_key: alias_key}) end relations.each do |relation| if relation.is_a? String key, alias_key = relation.split(':') alias_key = apply_case(alias_key || key, [:case]) instruction[:relations].push({key: key, alias_key: alias_key, hasharray: []}) else key, alias_key = relation.keys.first.split(':') alias_key = apply_case(alias_key || key, [:case]) instruction[:relations].push({key: key, alias_key: alias_key, hasharray: relation.values.first}) end end end hash = {} instruction[:attributes].each do |attribute| hash[attribute[:alias_key]] = coerce_value(record.public_send(attribute[:key])) end instruction[:relations].each do |relation| relation_records = record.public_send(relation[:key]) hash[relation[:alias_key]] = self.serialize(relation_records, relation[:hasharray], , instructions) end hash end |