class HALPresenter::CurieCollection
Attributes
collection[R]
Public Class Methods
extract_from!(hash, resolve_collisions: true)
click to toggle source
# File lib/hal_presenter/curie_collection.rb, line 65 def self.extract_from!(hash, resolve_collisions: true) new.tap do |curies| curies.add_curies(hash[:_links]&.delete(:curies)) curies.send(:add_references, hash[:_links], :links) curies.send(:add_references, hash[:_embedded], :embedded) curies.resolve_collisions! if resolve_collisions end end
new()
click to toggle source
# File lib/hal_presenter/curie_collection.rb, line 74 def initialize @collection = [] end
Public Instance Methods
add_curies(curies)
click to toggle source
# File lib/hal_presenter/curie_collection.rb, line 78 def add_curies(curies) return unless curies curies.each do |curie| next if find(curie[:name]) collection << CurieWithReferences.new(curie) end end
each() { |c| ... }
click to toggle source
# File lib/hal_presenter/curie_collection.rb, line 111 def each return collection.each unless block_given? collection.each { |c| yield c } end
empty?()
click to toggle source
# File lib/hal_presenter/curie_collection.rb, line 107 def empty? collection.empty? end
generate_curie_name(base)
click to toggle source
# File lib/hal_presenter/curie_collection.rb, line 87 def generate_curie_name(base) name = "#{base}0" name = name.next while find(name.to_sym) name.to_sym end
resolve_collisions!()
click to toggle source
# File lib/hal_presenter/curie_collection.rb, line 93 def resolve_collisions! collection.reverse_each do |curie| next if collection.none? { |c| c.name == curie.name && c.href != curie.href } new_name = generate_curie_name(curie.name) curie.rename new_name end self end
to_a()
click to toggle source
# File lib/hal_presenter/curie_collection.rb, line 103 def to_a collection.map(&:to_h) end
Private Instance Methods
add_references(reference, type)
click to toggle source
# File lib/hal_presenter/curie_collection.rb, line 142 def add_references(reference, type) return unless reference reference.each do |rel, values| curie_name = curie_from(rel) curie = find(curie_name) curie&.add_reference(rel, reference, type) values = [values] if values.is_a? Hash values.each do |value| nested = self.class.extract_from!(value, resolve_collisions: false) concat nested.collection end end end
concat(other)
click to toggle source
# File lib/hal_presenter/curie_collection.rb, line 132 def concat(other) other.each do |curie| if existing = find(curie.name, curie.href) existing << curie else collection << curie end end end
curie_from(rel)
click to toggle source
# File lib/hal_presenter/curie_collection.rb, line 127 def curie_from(rel) parts = rel.to_s.split(':') parts.first if parts.size > 1 end
find(name, href = nil)
click to toggle source
# File lib/hal_presenter/curie_collection.rb, line 118 def find(name, href = nil) return unless name collection.find do |c| next unless c.name.to_sym == name.to_sym href.nil? || c.href == href end end