Module: Inform::Linkable
- Included in:
- Object, System::Object
- Defined in:
- lib/runtime/link.rb
Overview
The Linkable module
Instance Method Summary collapse
- #_set_object(link_name, obj = nil) ⇒ Object
-
#link(link_name, obj = nil) ⇒ Object
rubocop: disable Metrics/AbcSize rubocop: disable Metrics/MethodLength.
-
#linked?(link_name) ⇒ Boolean
(also: #_key?)
rubocop: enable Metrics/AbcSize rubocop: enable Metrics/MethodLength.
- #links ⇒ Object
- #linksfrom(link_name = nil) ⇒ Object
- #linkto(link_name) ⇒ Object (also: #_get_object)
- #unlink(link_name) ⇒ Object (also: #_unset_object)
Instance Method Details
#_set_object(link_name, obj = nil) ⇒ Object
151 152 153 |
# File 'lib/runtime/link.rb', line 151 def _set_object(link_name, obj = nil) link(link_name, obj).to end |
#link(link_name, obj = nil) ⇒ Object
rubocop: disable Metrics/AbcSize rubocop: disable Metrics/MethodLength
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/runtime/link.rb', line 106 def link(link_name, obj = nil) link = Inform::Link.first(name: link_name.to_s, from_id: self.id) link&.to&.refresh return link if obj.nil? if link.nil? clauses = { name: link_name.to_s, from_id: self.id, to_id: obj.id } link = Inform::Link.find_or_create(**clauses) do |l| l.from = self l.to = obj end else link.to = obj link.save end link end |
#linked?(link_name) ⇒ Boolean Also known as: _key?
rubocop: enable Metrics/AbcSize rubocop: enable Metrics/MethodLength
125 126 127 |
# File 'lib/runtime/link.rb', line 125 def linked?(link_name) Inform::Link.filter(name: link_name.to_s, from_id: self.id).count > 0 end |
#links ⇒ Object
98 99 100 101 102 |
# File 'lib/runtime/link.rb', line 98 def links super rescue StandardError => _e Array::Empty end |
#linksfrom(link_name = nil) ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/runtime/link.rb', line 141 def linksfrom(link_name = nil) if link_name.nil? Inform::Link.filter(to_id: self.id).map(&:from) else Inform::Link.filter(name: link_name.to_s, to_id: self.id).map(&:from) end end |
#linkto(link_name) ⇒ Object Also known as: _get_object
137 138 139 |
# File 'lib/runtime/link.rb', line 137 def linkto(link_name) Inform::Link.first(name: link_name.to_s, from_id: self.id)&.to&.refresh end |
#unlink(link_name) ⇒ Object Also known as: _unset_object
129 130 131 132 133 134 135 |
# File 'lib/runtime/link.rb', line 129 def unlink(link_name) Inform::Link.first(name: link_name.to_s, from_id: self.id).destroy&.to rescue Sequel::NoExistingObject => e log.warn 'Error: ' + e. log.warn 'No such link: ' + link_name nil end |