Class: Rys::Feature

Inherits:
Object
  • Object
show all
Extended by:
MonitorMixin
Defined in:
lib/rys/feature.rb

Direct Known Subclasses

RootFeature

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, parent) ⇒ Feature

Returns a new instance of Feature


54
55
56
57
58
59
60
61
62
# File 'lib/rys/feature.rb', line 54

def initialize(key, parent)
  @key = key
  @parent = parent
  @block_condition = nil
  @options = {}

  @full_key = parent.root? ? key : "#{parent.full_key}.#{key}"
  @children = {}
end

Instance Attribute Details

#block_conditionObject

Returns the value of attribute block_condition


11
12
13
# File 'lib/rys/feature.rb', line 11

def block_condition
  @block_condition
end

#childrenObject (readonly)

Returns the value of attribute children


10
11
12
# File 'lib/rys/feature.rb', line 10

def children
  @children
end

#full_keyObject (readonly)

Returns the value of attribute full_key


10
11
12
# File 'lib/rys/feature.rb', line 10

def full_key
  @full_key
end

#keyObject (readonly)

Returns the value of attribute key


10
11
12
# File 'lib/rys/feature.rb', line 10

def key
  @key
end

#optionsObject

Returns the value of attribute options


11
12
13
# File 'lib/rys/feature.rb', line 11

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent


10
11
12
# File 'lib/rys/feature.rb', line 10

def parent
  @parent
end

Class Method Details

.active?(*values) ⇒ Boolean

Returns:

  • (Boolean)

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rys/feature.rb', line 26

def self.active?(*values)
  values.each do |value|
    result = case value
             when Symbol, String
               feature = all_features[value.to_s]
               feature ? feature.active? : true
             when Proc
               value.call
             else
               true
             end

    # If one feature is inactive -> whole statement is inactive
    if !result
      return false
    end
  end

  true
end

.add(key, **options, &block_condition) ⇒ Object


17
18
19
20
21
22
23
24
# File 'lib/rys/feature.rb', line 17

def self.add(key, **options, &block_condition)
  synchronize {
    feature = root.fetch_or_create(key)
    feature.block_condition = block_condition
    feature.options = options
    feature
  }
end

.on(*keys, &block) ⇒ Object

TODO: Measure time


48
49
50
51
52
# File 'lib/rys/feature.rb', line 48

def self.on(*keys, &block)
  if active?(*keys)
    yield
  end
end

.rootObject


13
14
15
# File 'lib/rys/feature.rb', line 13

def self.root
  Rys::RootFeature.instance
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)

92
93
94
95
96
97
98
# File 'lib/rys/feature.rb', line 92

def active?
  if block_condition
    block_condition.call && parent.active?
  else
    RysFeatureRecord.active?(full_key) && parent.active?
  end
end

#categoryObject


100
101
102
# File 'lib/rys/feature.rb', line 100

def category
  options[:category]
end

#descriptionObject


108
109
110
# File 'lib/rys/feature.rb', line 108

def description
  translate_it :description, options[:description]
end

#fetch_or_create(key) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rys/feature.rb', line 68

def fetch_or_create(key)
  if key.blank?
    return self
  end

  if key.is_a?(Array)
    keys = key
  else
    keys = key.to_s.split('.')
  end

  keys = keys.dup
  key = keys.shift.to_s

  if !children.has_key?(key)
    new_feature = Rys::Feature.new(key, self)

    children[key] = new_feature
    all_features[new_feature.full_key] = new_feature
  end

  children[key].fetch_or_create(keys)
end

#root?Boolean

Returns:

  • (Boolean)

64
65
66
# File 'lib/rys/feature.rb', line 64

def root?
  false
end

#status_changed(active) ⇒ Object


112
113
114
# File 'lib/rys/feature.rb', line 112

def status_changed(active)
  options[:status_changed]&.(active)
end

#titleObject


104
105
106
# File 'lib/rys/feature.rb', line 104

def title
  translate_it :title, options[:title]
end