Class: Eco::API::Organization::PolicyGroups

Inherits:
Language::Models::Collection show all
Defined in:
lib/eco/api/organization/policy_groups.rb

Constant Summary

Constants inherited from Language::Models::Collection

Language::Models::Collection::BASIC_METHODS, Language::Models::Collection::EXTENDED_METHODS

Instance Method Summary collapse

Methods inherited from Language::Models::Collection

#<, #<<, #attr, #attr?, attr_collection, attr_presence, #attrs, attrs_create_method, #contains, #delete!, #each, #empty, #empty?, #exclude, #group_by, #length, #merge, #new, #newFrom, #present, #present_all?, #present_some?, #remove, #to_c, #to_h, #unique_attrs, #update

Constructor Details

#initialize(policy_groups = [], klass: Ecoportal::API::Internal::PolicyGroup) ⇒ PolicyGroups

Returns a new instance of PolicyGroups.



8
9
10
11
12
13
# File 'lib/eco/api/organization/policy_groups.rb', line 8

def initialize(policy_groups = [], klass: Ecoportal::API::Internal::PolicyGroup)
  @klass       = Ecoportal::API::Internal::PolicyGroup
  @caches_init = false
  super(policy_groups, klass: @klass)
  init_caches
end

Instance Method Details

#[](id_name) ⇒ Object



49
50
51
# File 'lib/eco/api/organization/policy_groups.rb', line 49

def [](id_name)
  @by_id[policy_group_id(id_name)]
end

#idsObject



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

def ids
  @by_id.keys
end

#namesObject



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

def names
  @by_name.keys
end

#policy_group(id_name) ⇒ Object



45
46
47
# File 'lib/eco/api/organization/policy_groups.rb', line 45

def policy_group(id_name)
  self[id_name]
end

#to_id(name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/eco/api/organization/policy_groups.rb', line 23

def to_id(name)
  case name
  when Enumerable
    name.map do |n|
      policy_group(n)&.id
    end.compact
  else
    policy_group(name)&.id
  end
end

#to_name(id) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/eco/api/organization/policy_groups.rb', line 34

def to_name(id)
  case id
  when Enumerable
    id.map do |n|
      policy_group(n)&.name
    end.compact
  else
    policy_group(id)&.name
  end
end

#user_pg_ids(initial: [], final: [], non_custom: (non_custom_not_used = true; []), preserve_custom: true) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/eco/api/organization/policy_groups.rb', line 53

def user_pg_ids(initial: [], final: [], non_custom: (non_custom_not_used = true; []), preserve_custom: true)
  non_custom = to_id([non_custom].flatten.compact)
  initial    = to_id([initial].flatten.compact)
  final      = to_id([final].flatten.compact)
  unless initial.is_a?(Array) && final.is_a?(Array) && non_custom.is_a?(Array)
    raise "Expected Array for :initial, :final and :custom"
  end

  if !non_custom_not_used && preserve_custom
    initial_custom = initial - non_custom
    final         += initial_custom
  end

  new_pg_ids = final - initial
  # keep same order as they where
  (initial & final) + new_pg_ids
end