Class: FuryDumper::Dumpers::RelationItems

Inherits:
Object
  • Object
show all
Defined in:
lib/fury_dumper/dumpers/relation_items.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items: []) ⇒ RelationItems

Returns a new instance of RelationItems.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
# File 'lib/fury_dumper/dumpers/relation_items.rb', line 22

def initialize(items: [])
  raise ArgumentError unless items.is_a?(Array)
  raise ArgumentError unless items.all? { |item| item.is_a?(RelationItem) }

  @items = items
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



20
21
22
# File 'lib/fury_dumper/dumpers/relation_items.rb', line 20

def items
  @items
end

Class Method Details

.new_with_items(items: []) ⇒ Object



33
34
35
# File 'lib/fury_dumper/dumpers/relation_items.rb', line 33

def self.new_with_items(items: [])
  new(items: items)
end

.new_with_key_value(item_key: 'id', item_values: []) ⇒ Object



29
30
31
# File 'lib/fury_dumper/dumpers/relation_items.rb', line 29

def self.new_with_key_value(item_key: 'id', item_values: [])
  new(items: [RelationItem.new(key: item_key, values_for_key: item_values.compact)])
end

Instance Method Details

#complex_itemsObject



49
50
51
# File 'lib/fury_dumper/dumpers/relation_items.rb', line 49

def complex_items
  items.select(&:complex)
end

#copyObject



61
62
63
# File 'lib/fury_dumper/dumpers/relation_items.rb', line 61

def copy
  self.class.new(items: copy_items)
end

#copy_itemsObject



65
66
67
# File 'lib/fury_dumper/dumpers/relation_items.rb', line 65

def copy_items
  items.map(&:copy)
end

#copy_with_new_values(key, new_values) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/fury_dumper/dumpers/relation_items.rb', line 69

def copy_with_new_values(key, new_values)
  new_items = copy_items
  new_items.each do |item|
    item.values_for_key = new_values.compact.dup if item.key == key
  end

  self.class.new_with_items(items: new_items)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
# File 'lib/fury_dumper/dumpers/relation_items.rb', line 37

def eql?(other)
  raise ArgumentError unless other.is_a?(RelationItems)

  other.items.reject(&:additional).all? do |other_item|
    items.reject(&:additional).any? { |item| item.eql?(other_item) }
  end
end

#equality_itemsObject



45
46
47
# File 'lib/fury_dumper/dumpers/relation_items.rb', line 45

def equality_items
  items.reject(&:complex)
end

#keysObject



53
54
55
# File 'lib/fury_dumper/dumpers/relation_items.rb', line 53

def keys
  items.map(&:key).sort
end

#values(key) ⇒ Object



57
58
59
# File 'lib/fury_dumper/dumpers/relation_items.rb', line 57

def values(key)
  items.select { |item| item.key == key }.values_for_key
end