class Kanji::Generators::Type

Attributes

attributes[R]
name[R]

Public Class Methods

new(name, attributes) click to toggle source
# File lib/kanji/generators/type.rb, line 8
def initialize(name, attributes)
  @name = name
  @attributes = attributes.map { |attr| attr.split(":") }
  super(destination)
end

Public Instance Methods

populate_templates() click to toggle source
# File lib/kanji/generators/type.rb, line 14
def populate_templates
  add_type
  add_repository
  add_mutations
  add_migration
end

Private Instance Methods

add_migration() click to toggle source
# File lib/kanji/generators/type.rb, line 57
def add_migration
  add_template("migration.rb.tt", "db/migrate/#{Time.now.strftime("%Y%m%d%H%M%S")}_create_#{pluralized_type_name}.rb")
end
add_mutations() click to toggle source
# File lib/kanji/generators/type.rb, line 49
def add_mutations
  fields = "\n\n      field :create#{class_name}, Types::#{class_name}[:create_mutation]\n" +
  "      field :update#{class_name}, Types::#{class_name}[:update_mutation]\n" +
  "      field :destroy#{class_name}, Types::#{class_name}[:destroy_mutation]\n"

  generator.processor.insert_into_file "./app/mutation_type.rb", fields, after: /description ".*"/
end
add_repository() click to toggle source
# File lib/kanji/generators/type.rb, line 41
def add_repository
  add_template("app/repositories/repo.rb.tt", "app/repositories/#{pluralized_type_name}.rb")
end
add_type() click to toggle source
# File lib/kanji/generators/type.rb, line 37
def add_type
  add_template("app/types/type.rb.tt", "app/types/#{type_name}.rb")
end
application_class() click to toggle source
# File lib/kanji/generators/type.rb, line 61
def application_class
  @_application_class ||= Dry::Core::Inflector.camelize(File.basename(Dir.pwd))
end
class_name() click to toggle source
# File lib/kanji/generators/type.rb, line 73
def class_name
  @_class_name ||= Dry::Core::Inflector.camelize(name)
end
destination() click to toggle source
# File lib/kanji/generators/type.rb, line 45
def destination
  "./"
end
lookup_column_type(type_name) click to toggle source
# File lib/kanji/generators/type.rb, line 95
def lookup_column_type(type_name)
  map = {
    "string" => "varchar(255)",
    "integer" => "integer",
    "decimal" => "numeric",
    "text" => "text",
    "boolean" => "boolean",
    "datetime" => "timestamp",
    "float" => "double precision"
  }

  map.fetch(type_name, nil)
end
lookup_type(type_name) click to toggle source
# File lib/kanji/generators/type.rb, line 81
def lookup_type(type_name)
  map = {
    "string" => "Kanji::Types::String",
    "integer" => "Kanji::Types::Int",
    "decimal" => "Kanji::Types::Decimal",
    "text" => "Kanji::Types::String",
    "boolean" => "Kanji::Types::Bool",
    "float" => "Kanji::Types::Float",
    "datetime" => "Kanji::Types::DateTime"
  }

  map.fetch(type_name, "Kanji::Types::Any")
end
pluralized_class_name() click to toggle source
# File lib/kanji/generators/type.rb, line 77
def pluralized_class_name
  @_pluralized_class_name ||= Dry::Core::Inflector.pluralize(class_name)
end
pluralized_type_name() click to toggle source
# File lib/kanji/generators/type.rb, line 69
def pluralized_type_name
  @_pluralized_type_name ||= Dry::Core::Inflector.pluralize(type_name)
end
template_scope() click to toggle source
# File lib/kanji/generators/type.rb, line 24
def template_scope
  {
    type_name: type_name,
    pluralized_type_name: pluralized_type_name,
    class_name: class_name,
    pluralized_class_name: pluralized_class_name,
    attributes: attributes,
    application_class: application_class,
    lookup_type: -> (name) { lookup_type(name) },
    lookup_column_type: -> (name) { lookup_column_type(name) }
  }
end
type_name() click to toggle source
# File lib/kanji/generators/type.rb, line 65
def type_name
  @_type_name ||= Dry::Core::Inflector.underscore(name)
end