Class: Archimate::Diff::ArchimateArrayReference
Instance Attribute Summary collapse
#archimate_node
Instance Method Summary
collapse
#==, for_node, #lookup_parent_in_model, #parent, #recurse_lookup_in_model
Constructor Details
Returns a new instance of ArchimateArrayReference.
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/archimate/diff/archimate_array_reference.rb', line 11
def initialize(array, array_index)
unless array.is_a?(Array)
raise(
TypeError,
"array argument must be an Array, was #{array.class}"
)
end
unless array_index.is_a?(Integer)
raise(
TypeError,
"array_index argument must be a Integer, was #{array_index.class} #{array_index.inspect}"
)
end
unless array_index >= 0 && array_index < array.size
raise(
ArgumentError,
"array_index argument a valid index for array #{array_index.inspect}"
)
end
super(array)
@array_index = array_index
end
|
Instance Attribute Details
#array_index ⇒ Object
Returns the value of attribute array_index.
9
10
11
|
# File 'lib/archimate/diff/archimate_array_reference.rb', line 9
def array_index
@array_index
end
|
Instance Method Details
#change(to_model, from_value) ⇒ Object
91
92
93
94
95
96
97
98
99
|
# File 'lib/archimate/diff/archimate_array_reference.rb', line 91
def change(to_model, from_value)
ary_in_model = lookup_parent_in_model(to_model)
idx = ary_in_model.smart_find(from_value)
if idx.nil?
$stderr.puts "Couldn't find value #{from_value.inspect} in path #{path} to change in to_model, adding to end of list"
idx = ary_in_model.size
end
ary_in_model[idx] = value
end
|
#delete(to_model) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/archimate/diff/archimate_array_reference.rb', line 77
def delete(to_model)
ary_in_model = lookup_parent_in_model(to_model)
if ary_in_model.nil?
$stderr.puts "lookup parent in model failed for path #{path}"
return nil
end
idx = ary_in_model.smart_find(value)
if idx
ary_in_model.delete_at(idx)
else
$stderr.puts "Couldn't find item #{value.inspect} in path #{path} to delete in to_model"
end
end
|
#find_insert_index_in_ary(ary) ⇒ Object
For inserts - we can’t be sure of what is available (without an expensive sort) So lookup the first previous value that exists in to_model and insert it after that value instead of a fixed index.
63
64
65
66
67
68
69
|
# File 'lib/archimate/diff/archimate_array_reference.rb', line 63
def find_insert_index_in_ary(ary)
return -1 if array_index.zero?
my_idx = (array_index - 1).downto(0).find(-1) do |idx|
ary.smart_include?(archimate_node[idx])
end
ary.smart_find(archimate_node[my_idx])
end
|
#insert(to_model) ⇒ Object
71
72
73
74
75
|
# File 'lib/archimate/diff/archimate_array_reference.rb', line 71
def insert(to_model)
ary_in_model = lookup_parent_in_model(to_model)
insert_idx = find_insert_index_in_ary(ary_in_model) + 1
ary_in_model.insert(insert_idx, value)
end
|
#lookup_in_model(model) ⇒ Object
42
43
44
45
46
|
# File 'lib/archimate/diff/archimate_array_reference.rb', line 42
def lookup_in_model(model)
result = lookup_parent_in_model(model)
raise TypeError, "result was #{result.class} expected Array" unless result.is_a?(Array)
result[array_index]
end
|
#move(to_model, _from_ref) ⇒ Object
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/archimate/diff/archimate_array_reference.rb', line 101
def move(to_model, _from_ref)
ary_in_model = lookup_parent_in_model(to_model)
insert_idx = parent.previous_item_index(ary_in_model, value) + 1
current_idx = ary_in_model.smart_find(value)
deleted_value = ary_in_model.delete_at(current_idx)
ary_in_model.insert(
insert_idx,
deleted_value
)
end
|
#path(options = {}) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/archimate/diff/archimate_array_reference.rb', line 48
def path(options = {})
@array_ref_path ||= [
super,
case value
when DataModel::Referenceable
value.id
else
array_index
end
].map(&:to_s).reject(&:empty?).join("/")
end
|
#to_s ⇒ Object
38
39
40
|
# File 'lib/archimate/diff/archimate_array_reference.rb', line 38
def to_s
value.to_s
end
|
#value ⇒ Object
34
35
36
|
# File 'lib/archimate/diff/archimate_array_reference.rb', line 34
def value
archimate_node[array_index]
end
|