Class: Darstellung::Attribute
- Inherits:
-
Object
- Object
- Darstellung::Attribute
- Defined in:
- lib/darstellung/attribute.rb
Overview
An attribute is any field that can be represented. This class provides extra behavior around when and how these fields get represented.
Instance Attribute Summary collapse
- #block ⇒ Object readonly
- #block The block to call to get the value.(Theblocktocalltogetthevalue.) ⇒ Object readonly
- #name ⇒ Object readonly
- #name The name of the attribute.(Thenameoftheattribute.) ⇒ Object readonly
- #options ⇒ Object readonly
- #options The attribute options.(Theattributeoptions.) ⇒ Object readonly
Instance Method Summary collapse
-
#displayable?(version) ⇒ true, false
Determines if the attribute is displayable in the representation given the provided version.
-
#initialize(name, options = {}, &block) ⇒ Attribute
constructor
Initialize the new attribute.
-
#value(resource) ⇒ Object
Get the value for the attribute from the provided resource.
Constructor Details
#initialize(name, options = {}, &block) ⇒ Attribute
Initialize the new attribute.
51 52 53 |
# File 'lib/darstellung/attribute.rb', line 51 def initialize(name, = {}, &block) @name, @options, @block = name, , block end |
Instance Attribute Details
#block ⇒ Object (readonly)
13 14 15 |
# File 'lib/darstellung/attribute.rb', line 13 def block @block end |
#block The block to call to get the value.(Theblocktocalltogetthevalue.) ⇒ Object (readonly)
13 |
# File 'lib/darstellung/attribute.rb', line 13 attr_reader :name, :options, :block |
#name ⇒ Object (readonly)
13 14 15 |
# File 'lib/darstellung/attribute.rb', line 13 def name @name end |
#name The name of the attribute.(Thenameoftheattribute.) ⇒ Object (readonly)
13 |
# File 'lib/darstellung/attribute.rb', line 13 attr_reader :name, :options, :block |
#options ⇒ Object (readonly)
13 14 15 |
# File 'lib/darstellung/attribute.rb', line 13 def @options end |
#options The attribute options.(Theattributeoptions.) ⇒ Object (readonly)
13 |
# File 'lib/darstellung/attribute.rb', line 13 attr_reader :name, :options, :block |
Instance Method Details
#displayable?(version) ⇒ true, false
Note:
This method assumes that API versions are following the Semantic Versioning Specificaion, and does its comparison of version strings with this in mind.
Determines if the attribute is displayable in the representation given the provided version.
32 33 34 35 |
# File 'lib/darstellung/attribute.rb', line 32 def displayable?(version) return true unless version from <= version && version <= to(version) end |
#value(resource) ⇒ Object
Get the value for the attribute from the provided resource.
65 66 67 |
# File 'lib/darstellung/attribute.rb', line 65 def value(resource) block ? block.call(resource) : resource.__send__(name) end |