Class: Sparrow::EntityGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/sparrow/entity/entity_generator.rb

Overview

创建一个自定义的创建 Sparrow::Base 的类。

Author:

Constant Summary collapse

TARGET_RELATIVE_PATH =

默认的引入应用的相对安装路径

'app/entities'
MODULE_TEMPLATE =

单模组模板

<<~MODULE.strip
  module %{module_name}
  %{module_content}
  end
MODULE
CLASS_TEMPLATE =

单类模板

<<~KLASS.strip
  class %{class_name} < Sparrow::Base
    #
    # Useage:
    #
    # use field DSL to define attribute.
    # like: <tt>field :name, String</tt> to define an string attribute called +name+.
    #
  end
KLASS

Instance Method Summary collapse

Instance Method Details

#create_sparrow_class_fileObject

复制模板文件到对应 Rails 项目中



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/generators/sparrow/entity/entity_generator.rb', line 37

def create_sparrow_class_file
  # 如果存在命名空间,则分别生成对应模型
  if class_path.present?
    class_path.each_with_index do |sub, index|
      @sub_paths = class_path[0..index]
      parent_paths = @sub_paths.dup
      parent_paths.pop
      sub_path = ::File.join(TARGET_RELATIVE_PATH, parent_paths, "#{sub}.rb")
      template 'module.rb.tt', sub_path
    end
  end

  # 类文件最终路径
  sparrow_file_path = ::File.join(TARGET_RELATIVE_PATH, class_path, "#{file_name}.rb")
  # 类文件生成
  template 'sparrow_entity.rb.tt', sparrow_file_path
end