Class: Amsi::Model::Base::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/amsi/model/base/attribute.rb

Overview

Encapsulate a model attribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix:, type:, name:) ⇒ Attribute

Returns a new instance of Attribute.



8
9
10
11
12
# File 'lib/amsi/model/base/attribute.rb', line 8

def initialize(prefix:, type:, name:)
  @prefix = prefix
  @type = type
  @name = name.to_s
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/amsi/model/base/attribute.rb', line 6

def name
  @name
end

#prefixObject (readonly)

Returns the value of attribute prefix.



6
7
8
# File 'lib/amsi/model/base/attribute.rb', line 6

def prefix
  @prefix
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/amsi/model/base/attribute.rb', line 6

def type
  @type
end

Instance Method Details

#accessor_nameString

Returns the name of the method on the model used to access this attribute.

Returns:

  • (String)

    the name of the method on the model used to access this attribute



16
17
18
# File 'lib/amsi/model/base/attribute.rb', line 16

def accessor_name
  type == :boolean ? "#{name}?" : name
end

#matches?(attr) ⇒ true|false

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
SomeThingID   | some_thing_id
somethingID   | some_thing_id
foobarid      | bar_id (if in Model::Foo)
somethingbit  | some_thing (if defined as a :boolean)
somethingflag | some_thing (if defined as a :boolean)

Parameters:

  • attr (String|Symbol)

    the name of the attribute or the XML node name from the AMSI response, e.g. :made_ready_date or “MadeReadyDate”

Returns:

  • (true|false)

    true iff the value passed in is for this Attribute



40
41
42
# File 'lib/amsi/model/base/attribute.rb', line 40

def matches?(attr)
  possible_names.include?(attr.to_s.downcase)
end