Monkey patching Virtus' Attribute
class certainly is an option.
However, reaching into the internals of a library makes you vulnerable to refactorings in the private part of that libraries' source code.
Instead, you could use a helper module that encapsulates this feature. Here is a suggestion how:
require 'virtus'
# Put this helper module somewhere on your load path (e.g. your project's lib directory)
module ApiThing
def self.included(base)
base.include Virtus.model
base.extend ApiThing::ClassMethods
end
module ClassMethods
@@identifiers = {}
def api_attribute(attr_name, *virtus_args, identifier:, **virtus_options)
attribute attr_name, *virtus_args, **virtus_options
@@identifiers[attr_name.to_sym] = identifier
end
def identifier_for(attr_name)
@@identifiers.fetch(attr_name.to_sym){ raise ArgumentError, "unknown API attribute #{attr_name.inspect}" }
end
end
def identifier_for(attr_name)
self.class.identifier_for(attr_name)
end
end
# And include it in your API classes
class Balls
include ApiThing
api_attribute :color, String, identifier: 'SOME__fancy_identifier'
api_attribute :number, Integer, identifier: 'SOME__other_identifier'
api_attribute :size, BigDecimal, identifier: 'THAT__third_identifier'
end
# The attributes will be registered with Virtus – as usual
puts Balls.attribute_set[:color].type #=> Axiom::Types::String
puts Balls.attribute_set[:number].type #=> Axiom::Types::Integer
puts Balls.attribute_set[:size].type #=> Axiom::Types::Decimal
# You can use the handy Virtus constructor that takes a hash – as usual
b = Balls.new(color: 'red', number: 2, size: 42)
# You can access the attribute values – as usual
puts b.color #=> "red"
puts b.number #=> 2
puts b.size #=> 0.42e2
puts b.durability #=> undefined method `durability' [...]
# You can ask the instance about identifiers
puts b.identifier_for :color #=> "SOME__fancy_identifier"
puts b.identifier_for :durability #=> unknown API attribute :durability (ArgumentError)
# And you can ask the class about identifiers
puts Balls.identifier_for :color #=> "SOME__fancy_identifier"
puts Balls.identifier_for :durability #=> unknown API attribute :durability (ArgumentError)
You don't need Virtus in order to implement your API identifiers. A similar helper module could just register attr_accessor
s instead of Virtus attributes.
However, Virtus has other handy features like the hash constructors and attribute coersion. If you don't mind living without these features or finding replacements, ditching Virtus should not be a problem.
HTH! :)