Module: Structish::Validations::ClassMethods

Defined in:
lib/structish/validations.rb

Instance Method Summary collapse

Instance Method Details

#assign(key) ⇒ Object



206
207
208
# File 'lib/structish/validations.rb', line 206

def assign(key)
  [:other_attribute, key]
end

#attribute_hash(key, klass = nil, kwargs = {}, block) ⇒ Object



226
227
228
229
230
231
232
# File 'lib/structish/validations.rb', line 226

def attribute_hash(key, klass = nil, kwargs = {}, block)
  {
    key: key,
    klass: klass,
    proc: block,
  }.merge(kwargs.except(:key, :klass, :proc))
end

#attribute_keysObject



194
195
196
# File 'lib/structish/validations.rb', line 194

def attribute_keys
  attributes.map { |attribute| attribute[:key] }
end

#attributesObject



246
247
248
# File 'lib/structish/validations.rb', line 246

def attributes
  required_attributes + optional_attributes
end

#compact(do_compact) ⇒ Object



186
187
188
# File 'lib/structish/validations.rb', line 186

def compact(do_compact)
  @compact = do_compact
end

#compact?Boolean

Returns:



190
191
192
# File 'lib/structish/validations.rb', line 190

def compact?
  @compact
end

#delegate(function, object) ⇒ Object



202
203
204
# File 'lib/structish/validations.rb', line 202

def delegate(function, object)
  delegations << [function, object]
end

#delegationsObject



250
251
252
# File 'lib/structish/validations.rb', line 250

def delegations
  @delegations ||= []
end

#global_validationsObject



234
235
236
# File 'lib/structish/validations.rb', line 234

def global_validations
  @global_validations ||= []
end

#optional_attributesObject



242
243
244
# File 'lib/structish/validations.rb', line 242

def optional_attributes
  @optional_attributes ||= []
end

#required_attributesObject



238
239
240
# File 'lib/structish/validations.rb', line 238

def required_attributes
  @required_attributes ||= []
end

#restrict?Boolean

Returns:



254
255
256
# File 'lib/structish/validations.rb', line 254

def restrict?
  @restrict_attributes
end

#restrict_attributesObject



210
211
212
# File 'lib/structish/validations.rb', line 210

def restrict_attributes
  @restrict_attributes = true
end

#structish?Boolean

Returns:



198
199
200
# File 'lib/structish/validations.rb', line 198

def structish?
  true
end

#validate(key, klass = nil, kwargs = {}, &block) ⇒ Object



214
215
216
217
218
219
220
# File 'lib/structish/validations.rb', line 214

def validate(key, klass = nil, kwargs = {}, &block)
  accessor_name = kwargs[:alias_to] ? kwargs[:alias_to] : key
  accessor = accessor_name.to_s if (accessor_name.is_a?(String) || accessor_name.is_a?(Symbol))
  attr_reader(accessor) if accessor
  attribute_array = kwargs[:optional] ? optional_attributes : required_attributes
  attribute_array << attribute_hash(key, klass, kwargs.merge(accessor: accessor), block)
end

#validate_all(klass = nil, kwargs = {}, &block) ⇒ Object



222
223
224
# File 'lib/structish/validations.rb', line 222

def validate_all(klass = nil, kwargs = {}, &block)
  global_validations << attribute_hash(nil, klass, kwargs, block)
end