Module: DataCleaner::Formats

Defined in:
lib/data_cleaner/formats.rb

Overview

DataCleaner::Formats provides a DSL for describing, and method for looking up the format of object’s attributes, such that they can be replaced with fake data, but still pass validation.

Not all attributes need be specified, only those that need be replaced.

Attributes will be processed in the order they are specified.

Example:

module DataCleaner::Formats
  format "Person" do |f|
    f.name [:first_name, " ", :last_name]
    f.email :email, &:name # passes the name to the generate email method
    # custom format, block is provided with the instance
    f.reference do |instance|
      "#{instance.name[0..2].downcase}#{rand(89) + 10}"
    end
  end
end

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.formatsObject

Returns the value of attribute formats.



23
24
25
# File 'lib/data_cleaner/formats.rb', line 23

def formats
  @formats
end

.helpersObject

Returns the value of attribute helpers.



23
24
25
# File 'lib/data_cleaner/formats.rb', line 23

def helpers
  @helpers
end

Class Method Details

.attribute_format(klass, attribute) ⇒ Object

:call-seq: Formats.attribute_format(klass, attribute) -> attribute_format

Returns the format for a particular attribute as described in a format block.



61
62
63
64
65
66
67
# File 'lib/data_cleaner/formats.rb', line 61

def self.attribute_format(klass, attribute)
  format = formats[klass.to_s]
  if format
    attribute, attribute_format = format.attributes.assoc(attribute.to_sym)
    attribute_format
  end
end

.format(klass) {|obj| ... } ⇒ Object

:call-seq: Formats.format(klass) {|format| block } -> format

Yields a format object, which can be used to describe the format of klass.

Yields:

  • (obj)


50
51
52
53
54
# File 'lib/data_cleaner/formats.rb', line 50

def self.format(klass)
  obj = Format.new(klass)
  yield obj
  formats[klass.to_s] = obj
end

.helper(name, &block) ⇒ Object

:call-seq: Formats.helper(name) {|*args| block } -> helper

Define a format helper, which can then be used in a format block

Example:

module DataCleaner::Formats
  helper :ip_address do
    Array.new(4).map {rand(255)}.join(".")
  end

  format "Server" do |f|
    f.ip :ip_address
  end
end


42
43
44
# File 'lib/data_cleaner/formats.rb', line 42

def self.helper(name, &block)
  helpers[name] = block
end