Class: Actions::Helpers::Humanizer

Inherits:
Object
  • Object
show all
Defined in:
app/lib/actions/helpers/humanizer.rb

Defined Under Namespace

Classes: ActivationKeyResource, ContentViewResource, ContentViewVersionResource, OrganizationResource, ProductResource, RepositoryResource, Resource, SystemResource, UserResource

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action) ⇒ Humanizer

Returns a new instance of Humanizer.



4
5
6
7
8
9
10
# File 'app/lib/actions/helpers/humanizer.rb', line 4

def initialize(action)
  @action = action
  @input  = action.respond_to?(:task_input) ? action.task_input : action.input
  @input ||= {}
  @output = action.respond_to?(:task_output) ? action.task_output : action.output
  @output ||= {}
end

Class Method Details

.default_partsObject



20
21
22
# File 'app/lib/actions/helpers/humanizer.rb', line 20

def self.default_parts
  resource_classes_order.map { |klass| klass.new.name }
end

.register_resource(resource_class) ⇒ Object

Registers the resource class to the humanizer. Usually, this happens when the resource class is defined. The order of resources in the humanized input is determined by the registration order. The ‘register_resource` can be run more times for the same class, effectively moving the resource to the end of the humanized form.



29
30
31
32
# File 'app/lib/actions/helpers/humanizer.rb', line 29

def self.register_resource(resource_class)
  resource_classes_order.delete_if { |klass| klass.name == resource_class.name }
  resource_classes_order << resource_class
end

.resource(name) ⇒ Object



16
17
18
# File 'app/lib/actions/helpers/humanizer.rb', line 16

def self.resource(name)
  resource_classes_order.map(&:new).find { |resource| resource.name == name }
end

.resource_classes_orderObject



12
13
14
# File 'app/lib/actions/helpers/humanizer.rb', line 12

def self.resource_classes_order
  @resource_classes_order ||= []
end

Instance Method Details

#humanize_resource(name, data) ⇒ Object



45
46
47
48
49
50
# File 'app/lib/actions/helpers/humanizer.rb', line 45

def humanize_resource(name, data)
  if (resource = self.class.resource(name))
    { text: "#{resource.humanized_name} '#{resource.humanized_value(data)}'",
      link: resource.link(data) }
  end
end

#included_parts(parts, data) ⇒ Object



41
42
43
# File 'app/lib/actions/helpers/humanizer.rb', line 41

def included_parts(parts, data)
  parts.select { |part| data.key?(part) }
end

#input(*parts) ⇒ Object



34
35
36
37
38
39
# File 'app/lib/actions/helpers/humanizer.rb', line 34

def input(*parts)
  parts = self.class.default_parts if parts.empty?
  included_parts(parts, @input).map do |part|
    [part, humanize_resource(part, @input)]
  end
end