module RealPage module Model class Base # Encapsulate a model attribute class Attribute attr_reader :prefix, :type, :name def initialize(prefix:, type:, name:) @prefix = prefix @type = type @name = name.to_s end # @return [String] the name of the method on the model used to access # this attribute def accessor_name type == :boolean ? "#{name}?" : name end # There is some magic happening here to support more convenient # attribute names. Since Model::Base.new can take parameters from a # parsed XML document or from a human, we need to be able to look up # the attribute based on either. The general mapping looks like: # # XML node | attribute name # ---------------+-------------------- # SomeThing | some_thing # something | some_thing # foobarid | bar_id (if in Model::Foo) # somethingbit | some_thing (if defined as a :boolean) # somethingflag | some_thing (if defined as a :boolean) # # @param attr [String|Symbol] the name of the attribute or the XML node # name from the RealPage response, e.g. :made_ready_date or # "MadeReadyDate" # @return [true|false] true iff the value passed in is for this # Attribute def matches?(attr) possible_names.include?(attr.to_s.downcase) end private def possible_names squished_name = name.gsub(/_/, '') prefixed_name = "#{prefix}#{squished_name}" names = [name, squished_name, prefixed_name] if type == :boolean names.concat(["#{squished_name}flag", "#{squished_name}bit"]) end names end end end end end