Class: Amsi::Model::Base::Attribute
- Inherits:
-
Object
- Object
- Amsi::Model::Base::Attribute
- Defined in:
- lib/amsi/model/base/attribute.rb
Overview
Encapsulate a model attribute
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#accessor_name ⇒ String
The name of the method on the model used to access this attribute.
-
#initialize(prefix:, type:, name:) ⇒ Attribute
constructor
A new instance of Attribute.
-
#matches?(attr) ⇒ true|false
There is some magic happening here to support more convenient attribute names.
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/amsi/model/base/attribute.rb', line 6 def name @name end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
6 7 8 |
# File 'lib/amsi/model/base/attribute.rb', line 6 def prefix @prefix end |
#type ⇒ Object (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_name ⇒ String
Returns 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)
40 41 42 |
# File 'lib/amsi/model/base/attribute.rb', line 40 def matches?(attr) possible_names.include?(attr.to_s.downcase) end |