Class: OodAppkit::ClusterDecorator

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/ood_appkit/cluster_decorator.rb

Overview

A decorator that adds a presentation layer to the OodCluster::Cluster object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cluster:, id:, title: "", url: "", validators: {}, **_) ⇒ ClusterDecorator

Returns a new instance of ClusterDecorator.

Parameters:

  • cluster (OodCluster::Cluster)

    cluster object

  • id (#to_sym)

    id used in clusters hash of OodAppkit

  • title (#to_s) (defaults to: "")

    title of cluster

  • url (#to_s) (defaults to: "")

    url of cluster

  • validators (Hash{#to_sym=>Array<Validator>) (defaults to: {})

    ] hash of validators



24
25
26
27
28
29
30
# File 'lib/ood_appkit/cluster_decorator.rb', line 24

def initialize(cluster:, id:, title: "", url: "", validators: {}, **_)
  super(cluster)
  @id         = id.to_sym
  @title      = title.to_s
  @url        = url.to_s
  @validators = validators.each_with_object({}) { |(k, v), h| h[k.to_sym] = v }
end

Instance Attribute Details

#idSymbol (readonly)

The identifier used as the key in the OodAppkit clusters hash (used for reverse searching)

Returns:

  • (Symbol)

    the clusters hash key for this object



9
10
11
# File 'lib/ood_appkit/cluster_decorator.rb', line 9

def id
  @id
end

#titleString (readonly)

The title used to describe this cluster to users

Returns:

  • (String)

    the cluster title



13
14
15
# File 'lib/ood_appkit/cluster_decorator.rb', line 13

def title
  @title
end

#urlString (readonly)

The URL for this cluster that users can use to view more information

Returns:

  • (String)

    the cluster url



17
18
19
# File 'lib/ood_appkit/cluster_decorator.rb', line 17

def url
  @url
end

Instance Method Details

#==(other) ⇒ Boolean

The comparison operator

Parameters:

  • other (#to_sym)

    object to compare against

Returns:

  • (Boolean)

    whether objects are equivalent



42
43
44
# File 'lib/ood_appkit/cluster_decorator.rb', line 42

def ==(other)
  id == other.to_sym
end

#valid?(method = :cluster) ⇒ Boolean

Whether the given method is valid (i.e., passes all supplied validators)

Parameters:

  • method (#to_sym) (defaults to: :cluster)

    method to check if valid

Returns:

  • (Boolean)

    whether this method is valid



35
36
37
# File 'lib/ood_appkit/cluster_decorator.rb', line 35

def valid?(method = :cluster)
  @validators.fetch(method.to_sym, []).all? { |v| v.success? }
end