Method: AttrJson::Model.attr_json
- Defined in:
- lib/attr_json/model.rb
.attr_json(name, type, **options) ⇒ Object
Type can be an instance of an ActiveModel::Type::Value subclass, or a symbol that will
be looked up in ActiveModel::Type.lookup
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 |
# File 'lib/attr_json/model.rb', line 213 def attr_json(name, type, **) .assert_valid_keys(*(AttributeDefinition::VALID_OPTIONS - [:container_attribute] + [:validate])) type = _attr_json_maybe_wrap_timezone_aware(type) self.attr_json_registry = attr_json_registry.with( AttributeDefinition.new(name.to_sym, type, .except(:validate)) ) # By default, automatically validate nested models if (type.kind_of?(AttrJson::Type::Model) || type.kind_of?(AttrJson::Type::PolymorphicModel)) && [:validate] != false # Post validations up with something based on ActiveRecord::Validations::AssociatedValidator self.validates_with ::AttrJson::Model::NestedModelValidator, attributes: [name.to_sym] end _attr_jsons_module.module_eval do define_method("#{name}=") do |value| _attr_json_write(AttrJson.efficient_to_s(name), value) end define_method("#{name}") do attributes[AttrJson.efficient_to_s(name)] end end end |