Module: Structish::Validations::ClassMethods
- Defined in:
- lib/structish/validations.rb
Instance Method Summary collapse
- #assign(key) ⇒ Object
- #attribute_hash(key, klass = nil, kwargs = {}, block) ⇒ Object
- #attribute_keys ⇒ Object
- #attributes ⇒ Object
- #compact(do_compact) ⇒ Object
- #compact? ⇒ Boolean
- #delegate(function, object) ⇒ Object
- #delegations ⇒ Object
- #global_validations ⇒ Object
- #optional_attributes ⇒ Object
- #required_attributes ⇒ Object
- #restrict? ⇒ Boolean
- #restrict_attributes ⇒ Object
- #structish? ⇒ Boolean
- #validate(key, klass = nil, kwargs = {}, &block) ⇒ Object
- #validate_all(klass = nil, kwargs = {}, &block) ⇒ Object
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_keys ⇒ Object
194 195 196 |
# File 'lib/structish/validations.rb', line 194 def attribute_keys attributes.map { |attribute| attribute[:key] } end |
#attributes ⇒ Object
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
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 |
#delegations ⇒ Object
250 251 252 |
# File 'lib/structish/validations.rb', line 250 def delegations @delegations ||= [] end |
#global_validations ⇒ Object
234 235 236 |
# File 'lib/structish/validations.rb', line 234 def global_validations @global_validations ||= [] end |
#optional_attributes ⇒ Object
242 243 244 |
# File 'lib/structish/validations.rb', line 242 def optional_attributes @optional_attributes ||= [] end |
#required_attributes ⇒ Object
238 239 240 |
# File 'lib/structish/validations.rb', line 238 def required_attributes @required_attributes ||= [] end |
#restrict? ⇒ Boolean
254 255 256 |
# File 'lib/structish/validations.rb', line 254 def restrict? @restrict_attributes end |
#restrict_attributes ⇒ Object
210 211 212 |
# File 'lib/structish/validations.rb', line 210 def restrict_attributes @restrict_attributes = true end |
#structish? ⇒ Boolean
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 |