Class: FakedCSV::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/faked_csv/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Config

Returns a new instance of Config.



5
6
7
# File 'lib/faked_csv/config.rb', line 5

def initialize(config)
    @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/faked_csv/config.rb', line 3

def config
  @config
end

#fieldsObject (readonly)

Returns the value of attribute fields.



3
4
5
# File 'lib/faked_csv/config.rb', line 3

def fields
  @fields
end

#row_countObject (readonly)

Returns the value of attribute row_count.



3
4
5
# File 'lib/faked_csv/config.rb', line 3

def row_count
  @row_count
end

Instance Method Details

#_min_max(range) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/faked_csv/config.rb', line 96

def _min_max(range)
    unless range.kind_of?(Array) && range.size == 2
        raise "invalid range. should be like: [0, 100]"
    end
    if range[0] >= range[1]
        raise "invalid range. 1st is >= 2nd"
    end
    return range[0], range[1]
end

#_supported_typesObject



92
93
94
# File 'lib/faked_csv/config.rb', line 92

def _supported_types
    ['rand:int', 'rand:float', 'rand:char', 'fixed', 'faker:<class>:<method>'].join ", "
end

#_validate_rotate(rotate) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/faked_csv/config.rb', line 106

def _validate_rotate(rotate)
    if rotate.to_s.include? "rows/"
        div = rotate.split("/")[1].to_i
        r = @row_count / div
    else
        r = rotate.to_i
    end
    return r > @row_count ? @row_count : r
end

#headersObject



14
15
16
# File 'lib/faked_csv/config.rb', line 14

def headers
    fields.map{|f| f[:name]}
end

#parseObject

prepare the json config and generate the fields



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/faked_csv/config.rb', line 19

def parse
    if @config["rows"].nil? || @config["rows"].to_i < 0
        @row_count = 100 # default value
    else
        @row_count = @config["rows"].to_i
    end

    @fields = []
    if @config["fields"].nil? || @config["fields"].empty?
        raise "need 'fields' in the config file and at least 1 field in it"
    end

    @config["fields"].each do |cfg|
        field = {}

        if cfg["name"].nil?
            raise "field needs a name"
        end
        field[:name] = cfg["name"].to_s

        if cfg["type"].nil? || cfg["type"].empty?
            raise "field needs a type"
        end
        field[:type] = cfg["type"].to_s

        unless cfg["inject"].nil? || cfg["inject"].empty? || !cfg["inject"].kind_of?(Array)
            field[:inject] = cfg["inject"].uniq # get rid of duplicates
        end

        unless cfg["rotate"].nil?
            field[:rotate] = _validate_rotate cfg["rotate"]
        end

        case field[:type]
        when /inc:int/i
            field[:type] = :inc_int
            field[:start] = cfg["start"].nil? ? 1 : cfg["start"].to_i
            field[:step] = cfg["step"].nil? ? 1 : cfg["step"].to_i
        when /rand:int/i
            field[:type] = :rand_int
            if cfg["range"].nil?
                # no range specified? use the default range: [0, 100]
                field[:min], field[:max] = 0, 100
            else
                field[:min], field[:max] = _min_max cfg["range"]
            end
        when /rand:float/i
            field[:type] = :rand_float
            if cfg["range"].nil?
                # no range specified? use the default range: [0, 1]
                field[:min], field[:max] = 0, 1
            else
                field[:min], field[:max] = _min_max cfg["range"]
            end
            field[:precision] = cfg["precision"].nil? ? 1 : cfg["precision"].to_i
        when /rand:char/i
            field[:type] = :rand_char
            field[:length] = cfg["length"].nil? ? 10 : cfg["length"]
            field[:format] = cfg["format"]
        when /fixed/i
            field[:type] = :fixed
            raise "need values for fixed type" if cfg["values"].nil?
            field[:values] = cfg["values"]
        when /faker:\S+/i
            field[:type] = cfg["type"]
        else
            raise "unsupported type: #{field[:type]}. supported types: #{_supported_types}"
        end

        fields << field
    end
end