Class: Ree::Contracts::ArgContracts::Splat

Inherits:
Object
  • Object
show all
Includes:
Truncatable
Defined in:
lib/ree/contracts/arg_contracts/splat.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Truncatable

#truncate

Constructor Details

#initialize(contracts) ⇒ Splat

Returns a new instance of Splat.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ree/contracts/arg_contracts/splat.rb', line 39

def initialize(contracts)
  @contracts = contracts

  @first_splat_of = contracts.first.is_a?(ArgContracts::SplatOf) ? contracts.first : nil
  @last_splat_of = contracts.size > 1 && contracts.last.is_a?(ArgContracts::SplatOf) ? contracts.last : nil

  @middle_splat_of = if contracts.size > 2
    list = contracts[1..-2]
    idx = list.index { _1.is_a?(ArgContracts::SplatOf) }
    list[idx] if idx
  end

  @middle_splat_of_index = @contracts.index { _1 == @middle_splat_of } if @middle_splat_of
  @validators = contracts.map { Validators.fetch_for(_1) }
end

Class Method Details

.[](*contracts) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ree/contracts/arg_contracts/splat.rb', line 10

def self.[](*contracts)
  if contracts.empty?
    raise BadContractError, 'Splat contract should accept at least one contract'
  end

  forbidden_class_contracts = Ree::Contracts::Validators::FORBIDDEN_CONTRACTS
  forbidden = Set.new(forbidden_class_contracts.to_a - [ArgContracts::SplatOf])
  
  contracts.each_with_index do |contract, index|
    contract_name = if forbidden_class_contracts.include?(contract)
      contract.to_s
    elsif forbidden.include?(contract.class)
      contract.class.to_s
    end

    if contract_name
      raise BadContractError, "#{contract_name} contract is not allowed to use inside Splat contract"
    end
  end

  splat_of_count = contracts.count { _1.is_a?(ArgContracts::SplatOf) }

  if splat_of_count != 1
    raise BadContractError, "Splat contract should include one SplatOf contract"
  end

  new(contracts)
end

Instance Method Details

#message(value, name, lvl = 1) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/ree/contracts/arg_contracts/splat.rb', line 104

def message(value, name, lvl = 1)
  unless value.is_a?(Array)
    return "expected #{to_s}, got #{value.class} => #{truncate(value.inspect)}"
  end

  errors = []
  sps = "  " * lvl
  
  if @validators.size - 1 > value.size
    return "expected at least #{@validators.size - 1} #{pluralize(@validators.size, 'value', 'values')} for #{to_s}, got #{value.size} #{pluralize(value.size, 'value', 'values')} => #{truncate(value.inspect)}"
  end
  
  if @first_splat_of
    rest_validators = @validators[1..-1]
    rest_validator_count = rest_validators.size

    rest_values = if value.size >= @validators.size
      value[(value.size - rest_validator_count)..-1]
    else
      value
    end

    if rest_validator_count > 0
      rest_validators.each_with_index do |validator, idx|
        val = rest_values[idx]
        next if validator.call(val)
        
        msg = validator.message(val, "#{name}[#{idx + 1}]", lvl + 1)
        errors << "\n\t#{sps} - #{name}[#{idx + 1}]: #{msg}"

        if errors.size > 3
          errors << "\n\t#{sps} - ..."
          break
        end
      end
    end

    if rest_values.size != value.size
      validator = @validators.first
      idx = value.size - rest_validator_count - 1
      val = value[0..idx]

      if !validator.call(val)
        msg = validator.message(val, "#{name}[0..#{idx}]", lvl + 1)
        errors << "\n\t#{sps} - #{name}[0..#{idx}]: #{msg}"
      end
    end
  elsif @last_splat_of
    rest_validators = @validators[0..-2]
    rest_validator_count = rest_validators.size

    rest_values = if value.size >= @validators.size
      value[0..-2]
    else
      value
    end

    if rest_validator_count > 0
      rest_validators.each_with_index do |validator, idx|
        val = rest_values[idx]
        next if validator.call(val)

        msg = validator.message(val, "#{name}[#{idx}]", lvl + 1)
        errors << "\n\t#{sps} - #{name}[#{idx}]: #{msg}"

        if errors.size > 3
          errors << "\n\t#{sps} - ..."
          break
        end
      end
    end

    if rest_values.size != value.size
      validator = @validators.last
      idx = rest_validator_count
      val = value[idx..-1]

      if !validator.call(val)
        msg = validator.message(val, "#{name}[#{idx}..#{value.size - 1}]", lvl + 1)
        errors << "\n\t#{sps} - #{name}[#{idx}..#{value.size - 1}]: #{msg}"
      end
    end
  elsif @middle_splat_of
    left_contract_count = @middle_splat_of_index
    right_contract_count = @validators.size - @middle_splat_of_index - 1

    value[0..left_contract_count - 1].each_with_index do |value, idx|
      validator = @validators[idx]
      next if validator.call(value)

      msg = validator.message(value, "#{name}[#{idx}]", lvl + 1)
      errors << "\n\t#{sps} - #{name}[#{idx}]: #{msg}"

      if errors.size > 3
        errors << "\n\t#{sps} - ..."
        break
      end
    end

    splat_values = if left_contract_count + right_contract_count == value.size
      []
    else
      value[@middle_splat_of_index..value.size - right_contract_count - 1]
    end
    
    if !splat_values.empty?
      validator = @validators[@middle_splat_of_index]

      if !validator.call(splat_values)
        splat_name = "#{name}[#{@middle_splat_of_index}..#{value.size - right_contract_count - 1}]"
        msg = validator.message(splat_values, "#{splat_name}", lvl + 1)
        errors << "\n\t#{sps} - #{splat_name}: #{msg}"
      end
    end

    is_valid = value[value.size - right_contract_count..-1].each_with_index.all? do |value, idx|
      @validators[@middle_splat_of_index + idx + 1].call(value)
    end

    pos = value.size - right_contract_count
    
    value[pos..-1].each_with_index do |value, idx|
      validator = @validators[@middle_splat_of_index + idx + 1]
      next if validator.call(value)

      msg = validator.message(value, "#{name}[#{pos + idx}]", lvl + 1)
      errors << "\n\t#{sps} - #{name}[#{pos + idx}]: #{msg}"

      if errors.size > 3
        errors << "\n\t#{sps} - ..."
        break
      end
    end
  end

  errors.join
end

#to_sObject



100
101
102
# File 'lib/ree/contracts/arg_contracts/splat.rb', line 100

def to_s
  "Splat[#{@validators.map(&:to_s).join(", ")}]"
end

#valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


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
91
92
93
94
95
96
97
98
# File 'lib/ree/contracts/arg_contracts/splat.rb', line 55

def valid?(value)
  return false if !value.is_a?(Array)
  return false if @validators.size - 1 > value.size
  
  if @first_splat_of
    return valid_with_first?(@validators, value)
  end

  if @last_splat_of
    return valid_with_last?(@validators, value)
  end

  if @middle_splat_of
    # before splat part
    left_contract_count = @middle_splat_of_index
    right_contract_count = @validators.size - @middle_splat_of_index - 1

    is_valid = value[0..left_contract_count - 1].each_with_index.all? do |value, idx|
      @validators[idx].call(value)
    end

    return false if !is_valid

    # splat part
    splat_values = if left_contract_count + right_contract_count == value.size
      []
    else
      value[@middle_splat_of_index..value.size - right_contract_count - 1]
    end
    
    if !splat_values.empty?
      return false if !@validators[@middle_splat_of_index].call(splat_values)
    end

    # after splat part
    is_valid = value[value.size - right_contract_count..-1].each_with_index.all? do |value, idx|
      @validators[@middle_splat_of_index + idx + 1].call(value)
    end

    return false if !is_valid
  end

  return true
end