Class: Eco::API::Organization::PresetsFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/eco/api/organization/presets_factory.rb

Constant Summary collapse

ABILITIES =
File.join(__dir__, 'presets_values.json')
INTEGRITY =
File.join(__dir__, 'presets_integrity.json')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enviro: nil, policy_groups: nil) ⇒ PresetsFactory

Returns a new instance of PresetsFactory.



26
27
28
29
30
31
32
# File 'lib/eco/api/organization/presets_factory.rb', line 26

def initialize(enviro: nil, policy_groups: nil)
  msg = "Expecting Environment object. Given: #{enviro.class}"
  fatal(msg) if enviro && !enviro.is_a?(Eco::API::Common::Session::Environment)

  @enviro        = enviro
  @policy_groups = policy_groups
end

Class Method Details

.abilitiesObject



21
22
23
# File 'lib/eco/api/organization/presets_factory.rb', line 21

def abilities
  @abilities ||= abilities_model.keys
end

.abilities_modelObject



13
14
15
# File 'lib/eco/api/organization/presets_factory.rb', line 13

def abilities_model
  @abilities_model ||= JSON.load(File.open(ABILITIES))
end

.all_abilities(hash = {}) ⇒ Object



9
10
11
# File 'lib/eco/api/organization/presets_factory.rb', line 9

def all_abilities(hash = {})
  Hash[abilities.each_with_object(nil).to_a].merge(hash)
end

.integrity_modelObject



17
18
19
# File 'lib/eco/api/organization/presets_factory.rb', line 17

def integrity_model
  @integrity_model ||= JSON.load(File.open(INTEGRITY))
end

Instance Method Details

#keysArray<String>

Returns all the abilities.

Returns:

  • (Array<String>)

    all the abilities



35
36
37
# File 'lib/eco/api/organization/presets_factory.rb', line 35

def keys
  self.class.abilities
end

#valid?(preset) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/eco/api/organization/presets_factory.rb', line 39

def valid?(preset)
  validate(preset).empty?
end

#validate(preset) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/eco/api/organization/presets_factory.rb', line 43

def validate(preset)
  [].tap do |errors|
    if (err = preset_errors(preset))
      errors << "{ '#{key}' preset -> #{err}}"
    end

    if (err = preset_integrity(preset))
      errors << "{ '#{key}' preset -> #{err}}"
    end
  end
end