Class: Zm::Client::Base::BaseJsnsBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/zm/client/base/base_jsns_builder.rb

Overview

class for account object jsns builder

Direct Known Subclasses

AceJsnsBuilder, FolderGrantJsnsBuilder

Constant Summary collapse

A_ARRAY_PROC =
->(i) { i.last.is_a?(Array) ? i.last.map { |j| [i.first, j] } : [i] }
A_NODE_PROC =
->(n) { { n: n.first, _content: n.last } }

Instance Method Summary collapse

Constructor Details

#initialize(item) ⇒ BaseJsnsBuilder

Returns a new instance of BaseJsnsBuilder.



11
12
13
# File 'lib/zm/client/base/base_jsns_builder.rb', line 11

def initialize(item)
  @item = item
end

Instance Method Details

#arrow_name(name) ⇒ Object



58
59
60
61
62
# File 'lib/zm/client/base/base_jsns_builder.rb', line 58

def arrow_name(name)
  return name if name.to_s.start_with?('@')

  "@#{name}"
end

#instance_variables_arrayObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/zm/client/base/base_jsns_builder.rb', line 42

def instance_variables_array
  selected_attrs = @item.attrs_write.map { |a| arrow_name(a).to_sym }
  attrs_only_set = @item.instance_variables & selected_attrs

  arr = attrs_only_set.map do |name|
    n = name.to_s[1..]
    value = @item.instance_variable_get(name)
    [n, value]
  end

  multi_value = arr.select { |a| a.last.is_a?(Array) }
  arr.reject! { |a| a.last.is_a?(Array) || a.last.nil? }
  multi_value.each { |a| arr += a.last.map { |v| [a.first, v] } }
  arr
end

#to_deleteObject



38
39
40
# File 'lib/zm/client/base/base_jsns_builder.rb', line 38

def to_delete
  { id: @item.id }
end

#to_jsnsObject Also known as: to_create



15
16
17
18
19
20
# File 'lib/zm/client/base/base_jsns_builder.rb', line 15

def to_jsns
  {
    name: @item.name,
    a: instance_variables_array.map(&A_ARRAY_PROC).flatten(1).map(&A_NODE_PROC)
  }
end

#to_patch(hash) ⇒ Object



31
32
33
34
35
36
# File 'lib/zm/client/base/base_jsns_builder.rb', line 31

def to_patch(hash)
  {
    id: @item.id,
    a: hash.map(&A_ARRAY_PROC).flatten(1).map(&A_NODE_PROC)
  }
end

#to_updateObject



24
25
26
27
28
29
# File 'lib/zm/client/base/base_jsns_builder.rb', line 24

def to_update
  {
    id: @item.id,
    a: instance_variables_array.map(&A_ARRAY_PROC).flatten(1).map(&A_NODE_PROC)
  }
end