8000 Suggestion: Replace your implementation of `Faker::String.from_regexp` with a dependency · Issue #328 · ffaker/ffaker · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Suggestion: Replace your implementation of Faker::String.from_regexp with a dependency #328
Open
@tom-lord

Description

@tom-lord

I recently stumbled across this gem, and noticed that you are using a custom implementation of Faker::String.from_regexp to generate strings from a regular expression:

def from_regexp(exp)
result = ''
@last_token = nil
# Drop surrounding /'s and split into characters
tokens = exp.inspect[1...-1].split(//)
result << process_token(tokens) until tokens.empty?
result
end

This implementation is OK for the basics, but is far from comprehensive! There is an open issue about one such bug, and another open issue that's apparently unresolved.

The regexp language is vast and complicated; solving this problem for all possible regular expressions is far from easy. However, this library provides a much more complete solution. For backwards compatibility, you could implement this as:

module FFaker
  module String
    def self.from_regexp(exp)
      puts 'Warning: FFaker::String.from_regexp is deprecated. Use exp.random_example'
      exp.random_example
    end
  end
end

There are a couple of potential minor issues we could discuss before dropping in this replacement (e.g. Regexp#random_example could be a refinement instead of a global class extension?), but more the most part it should be a very simple change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0