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.



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

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



22
23
24
# File 'app/lib/actions/helpers/humanizer.rb', line 22

def self.default_parts
  self.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.



31
32
33
34
# File 'app/lib/actions/helpers/humanizer.rb', line 31

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

.resource(name) ⇒ Object



18
19
20
# File 'app/lib/actions/helpers/humanizer.rb', line 18

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

.resource_classes_orderObject



14
15
16
# File 'app/lib/actions/helpers/humanizer.rb', line 14

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

Instance Method Details

#humanize_resource(name, data) ⇒ Object



49
50
51
52
53
54
# File 'app/lib/actions/helpers/humanizer.rb', line 49

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



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

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

#input(*parts) ⇒ Object



36
37
38
39
40
41
42
43
# File 'app/lib/actions/helpers/humanizer.rb', line 36

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