class Aam::Annotation::Model

Public Class Methods

new(base, klass) click to toggle source
# File lib/aam/annotation.rb, line 68
def initialize(base, klass)
  @base = base
  @klass = klass
end

Public Instance Methods

schema_info() click to toggle source
# File lib/aam/annotation.rb, line 73
def schema_info
  @schema_info ||= Generator.new(@klass, @base.options).generate + "\n"
end
write_to_relation_files() click to toggle source
# File lib/aam/annotation.rb, line 77
def write_to_relation_files
  puts ""
  puts "--> #{@klass}"
  target_files = search_paths.collect {|search_path|
    v = Pathname.glob((@base.root_dir + search_path).expand_path)
    v.reject{|e|e.to_s.include?("node_modules")}
  }.flatten.uniq
  target_files.each {|e| annotate_write(e) }
end

Private Instance Methods

annotate_write(file_name) click to toggle source
# File lib/aam/annotation.rb, line 119
def annotate_write(file_name)
  body = file_name.read
  regexp = /^#{SCHEMA_HEADER}\n(#.*\n)*\n+/
  if body.match(regexp)
    body = body.sub(regexp, schema_info)
  elsif body.include?(MAGIC_COMMENT_LINE)
    body = body.sub(/#{Regexp.escape(MAGIC_COMMENT_LINE)}\s*/) {MAGIC_COMMENT_LINE + schema_info}
  else
    body = body.sub(/^\s*/, schema_info)
  end
  body = insert_magick_comment(body)
  unless @base.options[:dry_run]
    file_name.write(body)
  end
  puts "write: #{file_name}"
  @base.counts[:success] += 1
end
insert_magick_comment(body, force = false) click to toggle source
# File lib/aam/annotation.rb, line 137
def insert_magick_comment(body, force = false)
  if force
    body = body.sub(/#{Regexp.escape(MAGIC_COMMENT_LINE)}\s*/, "")
  end
  unless body.include?(MAGIC_COMMENT_LINE)
    body = body.sub(/^\s*/, MAGIC_COMMENT_LINE)
  end
  body
end
search_paths() click to toggle source

TODO: アプリの構成に依存しすぎ?

# File lib/aam/annotation.rb, line 90
def search_paths
  paths = []
  paths << "app/models/**/#{@klass.name.underscore}.rb"
  # paths << "app/models/**/#{@klass.name.underscore}_{search,observer,callback,sweeper}.rb"
  paths << "test/unit/**/#{@klass.name.underscore}_test.rb"
  paths << "test/fixtures/**/#{@klass.name.underscore.pluralize}.yml"
  paths << "test/unit/helpers/**/#{@klass.name.underscore}_helper_test.rb"
  paths << "spec/models/**/#{@klass.name.underscore}_spec.rb"
  paths << "{test,spec}/**/#{@klass.name.underscore}_factory.rb"
  [:pluralize, :singularize].each{|method|
    prefix = @klass.name.underscore.send(method)
    [
      "app/controllers/**/#{prefix}_controller.rb",
      "app/helpers/**/#{prefix}_helper.rb",
      "test/functional/**/#{prefix}_controller_test.rb",
      "test/factories/**/#{prefix}_factory.rb",
      "test/factories/**/#{prefix}.rb",
      "db/seeds/**/{[0-9]*_,}#{prefix}_setup.rb",
      "db/seeds/**/{[0-9]*_,}#{prefix}_seed.rb",
      "db/seeds/**/{[0-9]*_,}#{prefix}.rb",
      "db/migrate/*_{create,to,from}_#{prefix}.rb",
      "spec/**/#{prefix}_{controller,helper}_spec.rb",
    ].each{|path|
      paths << path
    }
  }
  paths
end