Class: GraphqlMigrateExecution::Action

Inherits:
Object
  • Object
show all
Includes:
Colorize
Defined in:
lib/graphql_migrate_execution/action.rb

Defined Under Namespace

Modules: Colorize

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(migration, filepath, ruby_source) ⇒ Action

Returns a new instance of Action.



13
14
15
16
17
18
19
20
21
# File 'lib/graphql_migrate_execution/action.rb', line 13

def initialize(migration, filepath, ruby_source)
  @migration = migration
  @filepath = filepath
  @ruby_source = ruby_source
  @message = "".dup
  @result_source = @ruby_source.dup
  @strategy_name_padding = nil
  @field_name_padding = nil
end

Instance Attribute Details

#field_name_paddingObject (readonly)

Returns the value of attribute field_name_padding.



23
24
25
# File 'lib/graphql_migrate_execution/action.rb', line 23

def field_name_padding
  @field_name_padding
end

#filepathObject (readonly)

Returns the value of attribute filepath.



23
24
25
# File 'lib/graphql_migrate_execution/action.rb', line 23

def filepath
  @filepath
end

#messageObject (readonly)

Returns the value of attribute message.



23
24
25
# File 'lib/graphql_migrate_execution/action.rb', line 23

def message
  @message
end

#migrationObject (readonly)

Returns the value of attribute migration.



23
24
25
# File 'lib/graphql_migrate_execution/action.rb', line 23

def migration
  @migration
end

#result_sourceObject (readonly)

Returns the value of attribute result_source.



23
24
25
# File 'lib/graphql_migrate_execution/action.rb', line 23

def result_source
  @result_source
end

#strategy_name_paddingObject (readonly)

Returns the value of attribute strategy_name_padding.



23
24
25
# File 'lib/graphql_migrate_execution/action.rb', line 23

def strategy_name_padding
  @strategy_name_padding
end

Instance Method Details

#runObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/graphql_migrate_execution/action.rb', line 25

def run
  parse_result = Prism.parse(@ruby_source, filepath: @filepath)
  type_definitions = Hash.new { |h, k| h[k] = TypeDefinition.new(k, @migration) }
  visitor = Visitor.new(@ruby_source, type_definitions)
  visitor.visit(parse_result.value)
  total_field_definitions = 0
  field_definitions_by_strategy = Hash.new { |h, k| h[k] = [] }
  type_definitions.each do |name, type_defn|
    type_defn.field_definitions.each do |f_name, f_defn|
      total_field_definitions += 1
      f_defn.check_for_resolver_method
      field_definitions_by_strategy[f_defn.migration_strategy] << f_defn
    end
  end

  @message << "#{colorize(@filepath, :BOLD)}: "
  @message << "Found #{total_field_definitions} field definition#{total_field_definitions == 1 ? "" : "s"}:\n"
  @strategy_name_padding = field_definitions_by_strategy.each_key.map { |sc| sc.strategy_name.size }.max
  @field_name_padding = field_definitions_by_strategy.each_value.flat_map { |fds| fds.map { |fd| fd.path.size } }.max
  field_definitions_by_strategy.each do |strategy_class, field_definitions|
    strategy = strategy_class.new(self, field_definitions)
    strategy.run
  end
  @message << "\n"
end