Sha256: bfab0ae8d67e92fe7491ea5c0e95effd10e4bcaf05e98bd91c5aeca67ae07e3c

Contents?: true

Size: 1.67 KB

Versions: 25

Compression:

Stored size: 1.67 KB

Contents

module GraphitiGql
  class Schema
    module Fields
      class Stats
        def initialize(resource)
          @resource = resource
        end

        def apply(type)
          type.field :stats, build_stat_class, null: false
          type.define_method :stats do
            # Process grouped (to-many relationship) stats
            stats = object.proxy.stats.deep_dup
            stats.each_pair do |attr, calc|
              calc.each_pair do |calc_name, value|
                if value.is_a?(Hash)
                  stats[attr][calc_name] = value[parent.id]
                end
              end
            end
            stats
          end
          type
        end

        private

        def build_stat_class
          name = Registry.instance.key_for(@resource)
          stat_graphql_name = "#{name}Stats"
          return Registry.instance[stat_graphql_name][:type] if Registry.instance[stat_graphql_name]
          klass = Class.new(Schema.base_object)
          klass.graphql_name(stat_graphql_name)
          @resource.stats.each_pair do |name, config|
            calc_class = build_calc_class(stat_graphql_name, name, config.calculations.keys)
            klass.field name, calc_class, null: false
          end
          Registry.instance[stat_graphql_name] = { type: klass }
          klass
        end
    
        def build_calc_class(stat_graphql_name, stat_name, calculations)
          name = "#{stat_graphql_name}#{stat_name}Calculations"
          klass = Class.new(Schema.base_object)
          klass.graphql_name(name)
          calculations.each do |calc|
            klass.field calc, Float, null: false
          end
          klass
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
graphiti_gql-0.2.27 lib/graphiti_gql/schema/fields/stats.rb
graphiti_gql-0.2.26 lib/graphiti_gql/schema/fields/stats.rb
graphiti_gql-0.2.25 lib/graphiti_gql/schema/fields/stats.rb
graphiti_gql-0.2.24 lib/graphiti_gql/schema/fields/stats.rb
graphiti_gql-0.2.23 lib/graphiti_gql/schema/fields/stats.rb
graphiti_gql-0.2.22 lib/graphiti_gql/schema/fields/stats.rb
graphiti_gql-0.2.21 lib/graphiti_gql/schema/fields/stats.rb
graphiti_gql-0.2.20 lib/graphiti_gql/schema/fields/stats.rb
graphiti_gql-0.2.19 lib/graphiti_gql/schema/fields/stats.rb
graphiti_gql-0.2.18 lib/graphiti_gql/schema/fields/stats.rb
graphiti_gql-0.2.17 lib/graphiti_gql/schema/fields/stats.rb
graphiti_gql-0.2.16 lib/graphiti_gql/schema/fields/stats.rb
graphiti_gql-0.2.15 lib/graphiti_gql/schema/fields/stats.rb
graphiti_gql-0.2.14 lib/graphiti_gql/schema/fields/stats.rb
graphiti_gql-0.2.12 lib/graphiti_gql/schema/fields/stats.rb
graphiti_gql-0.2.11 lib/graphiti_gql/schema/fields/stats.rb
graphiti_gql-0.2.10 lib/graphiti_gql/schema/fields/stats.rb
graphiti_gql-0.2.9 lib/graphiti_gql/schema/fields/stats.rb
graphiti_gql-0.2.8 lib/graphiti_gql/schema/fields/stats.rb
graphiti_gql-0.2.7 lib/graphiti_gql/schema/fields/stats.rb