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.



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

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

Class Method Details

.abilitiesObject



23
24
25
# File 'lib/eco/api/organization/presets_factory.rb', line 23

def abilities
  @abilities ||= abilities_model.keys
end

.abilities_modelObject



15
16
17
# File 'lib/eco/api/organization/presets_factory.rb', line 15

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

.all_abilities(hash = {}) ⇒ Object



11
12
13
# File 'lib/eco/api/organization/presets_factory.rb', line 11

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

.integrity_modelObject



19
20
21
# File 'lib/eco/api/organization/presets_factory.rb', line 19

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



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

def keys
  self.class.abilities
end

#valid?(preset) ⇒ Boolean

Returns:

  • (Boolean)


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

def valid?(preset)
  validate(perset).length == 0
end

#validate(preset) ⇒ Object



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

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