Class: Vario::Level

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/vario/level.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(setting, level, new_record = false) ⇒ Level

Returns a new instance of Level.



9
10
11
12
13
14
15
16
# File 'app/models/vario/level.rb', line 9

def initialize(setting, level, new_record = false)
  level = level.to_h.with_indifferent_access
  @setting = setting
  @id = level[:id] || SecureRandom.hex
  @value = level[:value]
  @new_record = new_record
  self.conditions = level[:conditions]
end

Instance Attribute Details

#conditionsObject

Returns the value of attribute conditions.



6
7
8
# File 'app/models/vario/level.rb', line 6

def conditions
  @conditions
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'app/models/vario/level.rb', line 7

def id
  @id
end

#settingObject

Returns the value of attribute setting.



7
8
9
# File 'app/models/vario/level.rb', line 7

def setting
  @setting
end

#valueObject

Returns the value of attribute value.



7
8
9
# File 'app/models/vario/level.rb', line 7

def value
  @value
end

Instance Method Details

#conditions_for(context) ⇒ Object



25
26
27
# File 'app/models/vario/level.rb', line 25

def conditions_for(context)
  conditions.select { |condition| context.key?(condition.key.to_sym) }
end

#conditions_hashObject



37
38
39
# File 'app/models/vario/level.rb', line 37

def conditions_hash
  set_conditions.map { |c| [c.key.to_sym, c.value] }.to_h
end

#conditions_not_for(context) ⇒ Object



29
30
31
# File 'app/models/vario/level.rb', line 29

def conditions_not_for(context)
  conditions.reject { |condition| context.key?(condition.key.to_sym) }
end

#default?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'app/models/vario/level.rb', line 86

def default?
  set_conditions.size.zero?
end

#human_valueObject



68
69
70
71
72
# File 'app/models/vario/level.rb', line 68

def human_value
  hv = setting.human_value(value)
  return unless hv
  setting.type == :array ? hv.join(', ') : hv.to_s
end

#move(positions) ⇒ Object



50
51
52
53
54
55
56
# File 'app/models/vario/level.rb', line 50

def move(positions)
  if positions.positive?
    move_up(positions)
  else
    move_down(positions.abs)
  end
end

#move_down(positions = 1) ⇒ Object



63
64
65
66
# File 'app/models/vario/level.rb', line 63

def move_down(positions = 1)
  positions.times { |time| setting.move_level_down(self) }
  setting.save!
end

#move_up(positions = 1) ⇒ Object



58
59
60
61
# File 'app/models/vario/level.rb', line 58

def move_up(positions = 1)
  positions.times { |time| setting.move_level_up(self) }
  setting.save!
end

#persisted?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'app/models/vario/level.rb', line 90

def persisted?
  !@new_record
end

#set_conditionsObject



33
34
35
# File 'app/models/vario/level.rb', line 33

def set_conditions
  conditions.reject { |condition| condition.value.blank? }
end

#to_hObject



74
75
76
77
78
79
80
# File 'app/models/vario/level.rb', line 74

def to_h
  {
    id: id,
    conditions: conditions_hash,
    value: value
  }
end

#value_present?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'app/models/vario/level.rb', line 82

def value_present?
  value === false || value.present?
end

#with_context_values?(context) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
# File 'app/models/vario/level.rb', line 41

def with_context_values?(context)
  context.each do |key, value|
    condition = conditions.find { |condition| condition.key == key.to_s }
    return false if condition.value != value
  end

  true
end