Class: Biovision::Components::BaseComponent

Inherits:
Object
  • Object
show all
Defined in:
app/services/biovision/components/base_component.rb

Overview

Base biovision component

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component, user = nil) ⇒ BaseComponent

Returns a new instance of BaseComponent.

Parameters:



11
12
13
14
15
16
17
# File 'app/services/biovision/components/base_component.rb', line 11

def initialize(component, user = nil)
  @component = component
  @slug = component&.slug || 'base'
  self.user = user

  @name = I18n.t("biovision.components.#{@slug}.name", default: @slug)
end

Instance Attribute Details

#componentObject (readonly)

Returns the value of attribute component.



7
8
9
# File 'app/services/biovision/components/base_component.rb', line 7

def component
  @component
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'app/services/biovision/components/base_component.rb', line 7

def name
  @name
end

#slugObject (readonly)

Returns the value of attribute slug.



7
8
9
# File 'app/services/biovision/components/base_component.rb', line 7

def slug
  @slug
end

#userObject

Returns the value of attribute user.



7
8
9
# File 'app/services/biovision/components/base_component.rb', line 7

def user
  @user
end

Class Method Details

.allow?(user, options = {}) ⇒ Boolean

Parameters:

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

Returns:

  • (Boolean)


45
46
47
48
49
50
51
# File 'app/services/biovision/components/base_component.rb', line 45

def self.allow?(user, options = {})
  return false if user.nil?

  privilege = options[:privilege] || default_privilege_name

  UserPrivilege.user_has_privilege?(user, privilege)
end

.default_privilege_nameObject



39
40
41
# File 'app/services/biovision/components/base_component.rb', line 39

def self.default_privilege_name
  self.class.to_s.demodulize.underscore.gsub('component', 'manager')
end

.handler(input, user = nil) ⇒ BaseComponent

Receive component-specific handler by component slug

Parameters:

Returns:



24
25
26
27
28
29
30
31
# File 'app/services/biovision/components/base_component.rb', line 24

def self.handler(input, user = nil)
  if input.is_a?(BiovisionComponent)
    handler_class(input.slug).new(input, user)
  else
    entity = BiovisionComponent.find_by!(slug: input)
    handler_class(input).new(entity, user)
  end
end

.handler_class(slug) ⇒ Object

Parameters:

  • slug (String)


34
35
36
37
# File 'app/services/biovision/components/base_component.rb', line 34

def self.handler_class(slug)
  handler_name = "biovision/components/#{slug}_component".classify
  handler_name.safe_constantize || BaseComponent
end

Instance Method Details

#[](key) ⇒ String

Receive parameter value or nil

Returns value of component’s parameter of nil when it’s not found

Parameters:

  • key (String)

Returns:

  • (String)


109
110
111
# File 'app/services/biovision/components/base_component.rb', line 109

def [](key)
  @component.get(key)
end

#[]=(key, value) ⇒ Object

Set parameter

Parameters:

  • key (String)
  • value (String)


117
118
119
# File 'app/services/biovision/components/base_component.rb', line 117

def []=(key, value)
  @component[key] = value unless key.blank?
end

#allow?(options = {}) ⇒ Boolean

Parameters:

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

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
# File 'app/services/biovision/components/base_component.rb', line 70

def allow?(options = {})
  return false if user.nil?
  return true if user.super_user? || @role&.administrator?

  if options.key?(:action)
    @role.data[options[:action].to_s]
  else
    !@role.nil?
  end
end

#receive(key, default = '') ⇒ String

Receive parameter value with default

Returns value of component’s parameter or default value when it’s not found

Parameters:

  • key (String)
  • default (String) (defaults to: '')

Returns:

  • (String)


99
100
101
# File 'app/services/biovision/components/base_component.rb', line 99

def receive(key, default = '')
  @component.get(key, default)
end

#register_metric(name, quantity = 1) ⇒ Object

Parameters:

  • name (String)
  • quantity (Integer) (defaults to: 1)


123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/services/biovision/components/base_component.rb', line 123

def register_metric(name, quantity = 1)
  metric = Metric.find_by(name: name)
  if metric.nil?
    attributes = {
      biovision_component: @component,
      name: name,
      incremental: !name.end_with?('.hit')
    }
    metric = Metric.create(attributes)
  end

  metric << quantity
end

#settingsObject



87
88
89
# File 'app/services/biovision/components/base_component.rb', line 87

def settings
  @component.settings
end

#settings=(data) ⇒ Object

Parameters:

  • data (Hash)


82
83
84
85
# File 'app/services/biovision/components/base_component.rb', line 82

def settings=(data)
  @component.settings.merge!(normalize_settings(data))
  @component.save!
end

#use_parameters?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'app/services/biovision/components/base_component.rb', line 65

def use_parameters?
  true
end