Class: Darstellung::Attribute

Inherits:
Object
  • Object
show all
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.

Since:

  • 0.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, &block) ⇒ Attribute

Initialize the new attribute.

Examples:

Initialize the new attribute.

Darstellung::Attribute.new(:name, version: "1.0.1")

Parameters:

  • name (Symbol)

    The name of the attribute.

  • options (Hash) (defaults to: {})

    The attribute options.

Options Hash (options):

  • :from (String)

    The version the attribute is available from.

  • :to (String)

    The version the attribute is available to.

Since:

  • 0.0.0



51
52
53
# File 'lib/darstellung/attribute.rb', line 51

def initialize(name, options = {}, &block)
  @name, @options, @block = name, options, block
end

Instance Attribute Details

#blockObject (readonly)

Since:

  • 0.0.0



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

#nameObject (readonly)

Since:

  • 0.0.0



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

#optionsObject (readonly)

Since:

  • 0.0.0



13
14
15
# File 'lib/darstellung/attribute.rb', line 13

def options
  @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.

Examples:

Is the attribute displayable?

attribute.displayable?("1.0.0")

Parameters:

  • version (String)

    The version number.

Returns:

  • (true, false)

    If the attribute is displayable.

See Also:

Since:

  • 0.0.0



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.

Examples:

Get the value for the attribute.

attribute.value(user)

Parameters:

  • resource (Object)

    The resource to execute on.

Returns:

  • (Object)

    The value of the resource.

Since:

  • 0.0.0



65
66
67
# File 'lib/darstellung/attribute.rb', line 65

def value(resource)
  block ? block.call(resource) : resource.__send__(name)
end