【问题标题】:Metrics/AbcSize: Assignment Branch Condition size for fill_arrays is too high. [<9, 21, 0> 22.85/17]Metrics/AbcSize:fill_arrays 的分配分支条件大小太高。 [<9, 21, 0> 22.85/17]
【发布时间】:2021-06-16 08:47:05
【问题描述】:

我需要用 ffaker 生成随机数据,不知道如何在这里修复 abcsize

这就是我所拥有的

class Library
  attr_accessor :authors, :books, :orders, :readers

  def initialize(authors = [], books = [], orders = [], readers = [])
    @books = books
    @orders = orders
    @readers = readers
    @authors = authors
  end

  def create_data
    @authors = []
    @books = []
    @readers = []
    @orders = []

    100.times do
      authorname = FFaker::Book.author
      biography = FFaker::Book.description
      title = FFaker::Book.title
      name = FFaker::Name.name
      email = FFaker::Internet.email
      city = FFaker::Address.city
      street = FFaker::Address.street_name
      house = rand(1 - 10_000)
      date = Time.now.utc.strftime('%d.%m.%Y')

      @authors.push(Author.new(authorname, biography))
      @books.push(Book.new(title, authorname))
      @readers.push(Reader.new(name, email, city, street, house))
      @orders.push(Order.new(title, name, date))
    end
  end
end

所以我决定创建一些私有方法,但仍然有很大的 abcsize

class Library
  attr_accessor :authors, :books, :orders, :readers

  def initialize(authors = [], books = [], orders = [], readers = [])
    @books = books
    @orders = orders
    @readers = readers
    @authors = authors
  end

  def create_data
    create_arrays
    fill_arrays
  end
   
  private

  def create_arrays
    @authors = []
    @books = []
    @readers = []
    @orders = []
  end

  def fill_arrays
    100.times do
      authorname = FFaker::Book.author
      biography = FFaker::Book.description
      title = FFaker::Book.title
      name = FFaker::Name.name
      email = FFaker::Internet.email
      city = FFaker::Address.city
      street = FFaker::Address.street_name
      house = rand(1 - 10_000)
      date = Time.now.utc.strftime('%d.%m.%Y')

      @authors.push(Author.new(authorname, biography))
      @books.push(Book.new(title, authorname))
      @readers.push(Reader.new(name, email, city, street, house))
      @orders.push(Order.new(title, name, date))
    end
  end
end

我无法真正将那个 ffaker 部分分开,因为:

  1. Books 中的作者必须是 Author 类的实例。
  2. 来自 Orders 的 Book,必须是 Book 类的实例。

如果有人可以帮助我,我将不胜感激,在重构方面没有太多经验

方法分离部分按照@Sergii-k 的建议工作,但现在我在关联方面遇到了一些麻烦

class Library
  attr_accessor :authors, :books, :orders, :readers

  def initialize(authors: [], books: [], orders: [], readers: [])
    @books = books
    @orders = orders
    @readers = readers
    @authors = authors
  end

  def create_arrays
    @authors = []
    @books = []
    @readers = []
    @orders = []
  end

  def create_data
    create_arrays
    fill_arrays
  end

  def show
    create_data
    puts @authors
    puts @books
    puts @orders
    puts @readers
  end

  def build_author
    name = FFaker::Book.author
    biography = FFaker::Book.description
    Author.new(name, biography)
  end

  def build_book(author)
    title = FFaker::Book.title
    Book.new(title, author.name)
  end

  def build_reader
    name = FFaker::Name.name
    email = FFaker::Internet.email
    city = FFaker::Address.city
    street = FFaker::Address.street_name
    house = rand(1 - 10_000)
    Reader.new(name, email, city, street, house)
  end

  def build_order(reader)
    date = Time.now.utc.strftime('%d.%m.%Y')
    title = FFaker::Book.title
    Order.new(title, reader.name, date)
  end

  def fill_arrays
    1.times do
      author = build_author
      reader = build_reader

      @authors.push(author)
      @books.push(build_book(author))
      @readers.push(reader)
      @orders.push(build_order(reader))
    end
  end
end

 

这是现在的问题部分

  def build_book(author)
    title = FFaker::Book.title
    Book.new(title, author.name)
  end

  def build_order(reader)
    date = Time.now.utc.strftime('%d.%m.%Y')
    title = FFaker::Book.title
    Order.new(title, reader.name, date)
  end

我现在得到了 2 个不同的标题,因为我在这两种方法中都生成了它们。尝试传递参数,但没有成功

  def build_order(book, reader)
    date = Time.now.utc.strftime('%d.%m.%Y')
    Order.new(book.title, reader.name, date)
  end

完成,效果很好!

class Library
  attr_accessor :authors, :books, :orders, :readers

  def initialize(authors: [], books: [], orders: [], readers: [])
    @books = books
    @orders = orders
    @readers = readers
    @authors = authors
  end

  def create_arrays
    @authors = []
    @books = []
    @readers = []
    @orders = []
  end

  def create_data
    create_arrays
    fill_arrays
  end

  def show
    create_data
    puts @authors
    puts @books
    puts @orders
    puts @readers
  end

  def build_author
    name = FFaker::Book.author
    biography = FFaker::Book.description
    Author.new(name, biography)
  end

  def build_book(author)
    title = FFaker::Book.title
    Book.new(title, author.name)
  end

  def build_reader
    name = FFaker::Name.name
    email = FFaker::Internet.email
    city = FFaker::Address.city
    street = FFaker::Address.street_name
    house = rand(1 - 10_000)
    Reader.new(name, email, city, street, house)
  end

  def build_order(book, reader)
    date = Time.now.utc.strftime('%d.%m.%Y')
    Order.new(book.title, reader.name, date)
  end

  def fill_arrays
    1.times do
      author = build_author
      reader = build_reader
      book   = build_book(author)

      @authors.push(author)
      @books.push(book)
      @readers.push(reader)
      @orders.push(build_order(book, reader))
    end
  end
end

这是来自 yml 文件的日志

--- !ruby/object:Library
books:
- !ruby/object:Book
  title: Case of the Missing Hungry Imp
  author: Twila Mante
orders:
- !ruby/object:Order
  book: Case of the Missing Hungry Imp
  reader: Monty Feeney
  date: 19.03.2021
readers:
- !ruby/object:Reader
  name: Monty Feeney
  email: lamonica.friesen@williamson.com
  city: Croninport
  street: Pfeffer Neck
  house: 3002
authors:
- !ruby/object:Author
  name: Twila Mante
  biography: Ut porro deserunt voluptatem velit. Atque dicta labore ratione minima
    sapiente. Dolor doloremque dolorem harum sint. At voluptatum molestias adipisci
    vero. Perspiciatis rerum nesciunt maiores vitae.

【问题讨论】:

    标签: ruby-on-rails ruby refactoring faker rubocop


    【解决方案1】:

    你可以通过方法分离走得更远:

    def build_author
      # ...
      Author.new(authorname, biography)
    end
    
    def build_book(author)
      # ...
      Book.new(title, author.authorname)
    end
    
    def build_reader
      # ...
      Reader.new(name, email, city, street, house)
    end
    
    def build_order(book, reader)
      # ...
      Order.new(book.title, reader.name, date)
    end
    
    def fill_arrays
      100.times do
        author = build_author
        reader = build_reader
        book   = build_book(author)
    
        @authors.push(build_author)
        @books.push(book)
        @readers.push(reader)
        @orders.push(build_order(book, reader))
      end
    end
    

    更新:我注意到,一些参数是跨方法共享的。您可以将它们作为参数传递。

    更新:固定书名。

    【讨论】:

    • 它部分工作,我需要将 book.title 传递给 build_order 以便作者所写的书被读者购买
    • 请检查问题,我编辑了它。感谢您的宝贵时间!
    • 我错过了“title”变量。解决方案非常简单。
    猜你喜欢
    • 2019-10-17
    • 1970-01-01
    • 2020-05-22
    • 1970-01-01
    • 2016-04-13
    • 2023-04-07
    • 2015-09-05
    • 2022-06-20
    • 1970-01-01
    相关资源
    最近更新 更多