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

#roleObject (readonly)

Returns the value of attribute role.



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

def role
  @role
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, privilege_name = '') ⇒ Boolean

Parameters:

  • user (User)
  • privilege_name (String) (defaults to: '')

Returns:

  • (Boolean)


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

def self.allow?(user, privilege_name = '')
  return false if user.nil?
  return true if user.super_user?

  slug = self.class.to_s.demodulize.underscore.gsub('component', '')
  component = BiovisionComponent.find_by(slug: slug)

  self.class.new(component, user).allow?(privilege_name)
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

.privilege_namesObject

Privilege names for using in biovision_component_user.data



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

def self.privilege_names
  []
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)


123
124
125
# File 'app/services/biovision/components/base_component.rb', line 123

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

#[]=(key, value) ⇒ Object

Set parameter

Parameters:

  • key (String)
  • value (String)


131
132
133
# File 'app/services/biovision/components/base_component.rb', line 131

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

#administrator?Boolean

Returns:

  • (Boolean)


72
73
74
75
76
# File 'app/services/biovision/components/base_component.rb', line 72

def administrator?
  return false if user.nil?

  user.super_user? || @role&.administrator?
end

#allow?(*privilege_names) ⇒ Boolean

Parameters:

  • privilege_names (String|Array)

Returns:

  • (Boolean)


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

def allow?(*privilege_names)
  return false if user.nil?
  return true if administrator? || privilege_names.blank?
  return false if @role.nil?

  privilege_names.flatten.each { |slug| return true if privilege?(slug) }
  false
end

#privilege?(privilege_name) ⇒ Boolean

Parameters:

  • privilege_name (String)

Returns:

  • (Boolean)


89
90
91
92
93
# File 'app/services/biovision/components/base_component.rb', line 89

def privilege?(privilege_name)
  return false if @role.nil?

  @role.data[privilege_name.to_s]
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)


113
114
115
# File 'app/services/biovision/components/base_component.rb', line 113

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

#register_metric(name, quantity = 1) ⇒ Object

Parameters:

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


137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/services/biovision/components/base_component.rb', line 137

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



101
102
103
# File 'app/services/biovision/components/base_component.rb', line 101

def settings
  @component.settings
end

#settings=(data) ⇒ Object

Parameters:

  • data (Hash)


96
97
98
99
# File 'app/services/biovision/components/base_component.rb', line 96

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

#update_privileges(user, data = nil) ⇒ Object

Parameters:

  • user (User)
  • data (Hash) (defaults to: nil)


153
154
155
156
157
158
159
160
161
162
163
# File 'app/services/biovision/components/base_component.rb', line 153

def update_privileges(user, data = nil)
  criteria = {
    user: user,
    biovision_component: @component
  }
  link = BiovisionComponentUser.find_or_create_by(criteria)

  link&.update(data: data) unless data.nil?

  link
end

#use_parameters?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'app/services/biovision/components/base_component.rb', line 68

def use_parameters?
  true
end