Module: Inform::Linkable
- Included in:
- Ephemeral::Object
- Defined in:
- lib/story_teller/inform/ephemeral/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/CyclomaticComplexity rubocop: disable Metrics/MethodLength rubocop: disable Metrics/PerceivedComplexity.
-
#linked?(link_name) ⇒ Boolean
(also: #_key?)
rubocop: enable Metrics/AbcSize rubocop: enable Metrics/CyclomaticComplexity rubocop: enable Metrics/MethodLength rubocop: enable Metrics/PerceivedComplexity.
- #links ⇒ Object
-
#linksfrom(link_name = nil) ⇒ Object
rubocop: disable Metrics/AbcSize.
-
#linkto(link_name) ⇒ Object
(also: #_get_object)
rubocop: enable Metrics/AbcSize.
-
#unlink(link_name) ⇒ Object
(also: #_unset_object)
rubocop: disable Metrics/AbcSize.
Instance Method Details
#_set_object(link_name, obj = nil) ⇒ Object
164 165 166 167 |
# File 'lib/story_teller/inform/ephemeral/link.rb', line 164 def _set_object(link_name, obj = nil) link = link(link_name, obj) link.to end |
#link(link_name, obj = nil) ⇒ Object
rubocop: disable Metrics/AbcSize rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/MethodLength rubocop: disable Metrics/PerceivedComplexity
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/story_teller/inform/ephemeral/link.rb', line 86 def link(link_name, obj = nil) link = if Inform.link_klass.respond_to?(:first) Inform.link_klass.first(name: link_name.to_s, from_id: self.id) else self.links.find { |l| l.name == link_name.to_s } end link&.to&.refresh return link if obj.nil? if link.nil? link = if Inform.link_klass.respond_to?(:find_or_create) clauses = { name: link_name.to_s, from_id: self.id, to_id: obj.id } Inform.link_klass.find_or_create(**clauses) do |l| l.from = self l.to = obj end else properties = { name: link_name.to_s, from: self, to: obj } Inform::Ephemeral::Link.new(properties).tap do |l| self.links << l end 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/CyclomaticComplexity rubocop: enable Metrics/MethodLength rubocop: enable Metrics/PerceivedComplexity
118 119 120 121 122 123 124 |
# File 'lib/story_teller/inform/ephemeral/link.rb', line 118 def linked?(link_name) if Inform.link_klass.respond_to?(:filter) Inform.link_klass.filter(name: link_name.to_s, from_id: self.id).count > 0 else self.links.any? { |link| link.name == link_name.to_s && link.from == self } end end |
#links ⇒ Object
76 77 78 79 80 |
# File 'lib/story_teller/inform/ephemeral/link.rb', line 76 def links super rescue StandardError => _e Array::Empty end |
#linksfrom(link_name = nil) ⇒ Object
rubocop: disable Metrics/AbcSize
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/story_teller/inform/ephemeral/link.rb', line 149 def linksfrom(link_name = nil) if Inform.link_klass.respond_to?(:filter) if link_name.nil? Inform.link_klass.filter(to_id: self.id) else Inform.link_klass.filter(name: link_name.to_s, to_id: self.id) end else self.links.select { |link| link.name == link_name.to_s && link.to == self } end.map(&:from) end |
#linkto(link_name) ⇒ Object Also known as: _get_object
rubocop: enable Metrics/AbcSize
140 141 142 143 144 145 146 |
# File 'lib/story_teller/inform/ephemeral/link.rb', line 140 def linkto(link_name) if Inform.link_klass.respond_to?(:first) Inform.link_klass.first(name: link_name.to_s, from_id: self.id)&.to&.refresh else self.links.find { |link| link.name == link_name.to_s && link.from == self }&.to end end |
#unlink(link_name) ⇒ Object Also known as: _unset_object
rubocop: disable Metrics/AbcSize
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/story_teller/inform/ephemeral/link.rb', line 127 def unlink(link_name) if Inform.link_klass.respond_to?(:first) Inform.link_klass.first(name: link_name.to_s, from_id: self.id).destroy&.to else self.links.delete { |link| link.name == link_name.to_s && link.from == self } end rescue Sequel::NoExistingObject => e log.warn "Error: #{e.}" log.warn "No such link: #{link_name}" nil end |