Module: SkullIsland::Helpers::Resource
- Included in:
- Resource
- Defined in:
- lib/skull_island/helpers/resource.rb
Overview
Simple helper methods for Resources
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #datetime_from_params(params, actual_key) ⇒ Object
-
#delayed_set(property, data, key) ⇒ Object
rubocop:disable Style/GuardClause.
- #destroy ⇒ Object
-
#digest ⇒ Object
rubocop:enable Style/GuardClause.
- #digest_properties ⇒ Object
-
#find_by_digest ⇒ Object
Tests for an existing version of this resource based on its properties rather than its ‘id`.
- #fresh? ⇒ Boolean
- #host_regex ⇒ Object
- #id ⇒ Object
- #id_property ⇒ Object
- #immutable? ⇒ Boolean
-
#import_update_or_skip(verbose: false, test: false, index:) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity.
-
#lookup(type, value) ⇒ Object
rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity.
-
#model_name ⇒ Object
ActiveRecord ActiveModel::Name compatibility method.
- #new? ⇒ Boolean
-
#persisted? ⇒ Boolean
ActiveRecord ActiveModel::Model compatibility method.
- #postprocess_created_at(value) ⇒ Object
- #postprocess_updated_at(value) ⇒ Object
- #properties ⇒ Object
- #prune_for_save(data) ⇒ Object
- #reload ⇒ Object
- #required_properties ⇒ Object
- #save ⇒ Object
- #save_uri ⇒ Object
- #tainted? ⇒ Boolean
-
#to_param ⇒ Object
ActiveRecord ActiveModel::Conversion compatibility method.
- #to_s ⇒ Object
-
#update(params) ⇒ Object
ActiveRecord ActiveModel compatibility method.
Instance Method Details
#<=>(other) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/skull_island/helpers/resource.rb', line 224 def <=>(other) if id < other.id -1 elsif id > other.id 1 elsif id == other.id 0 else raise Exceptions::InvalidArguments end end |
#datetime_from_params(params, actual_key) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/skull_island/helpers/resource.rb', line 7 def datetime_from_params(params, actual_key) DateTime.new( params["#{actual_key}(1i)"].to_i, params["#{actual_key}(2i)"].to_i, params["#{actual_key}(3i)"].to_i, params["#{actual_key}(4i)"].to_i, params["#{actual_key}(5i)"].to_i ) end |
#delayed_set(property, data, key) ⇒ Object
rubocop:disable Style/GuardClause
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/skull_island/helpers/resource.rb', line 18 def delayed_set(property, data, key) # rubocop:disable Security/Eval if data[key] value = data[key].is_a?(String) ? eval(Erubi::Engine.new(data[key]).src) : data[key] send( "#{property}=".to_sym, value.is_a?(String) && value.start_with?('{"') ? eval(value) : value ) end # rubocop:enable Security/Eval end |
#destroy ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/skull_island/helpers/resource.rb', line 153 def destroy raise Exceptions::ImmutableModification if immutable? unless new? @api_client.delete(relative_uri.to_s) @api_client.invalidate_cache_for(relative_uri.to_s) @lazy = false @tainted = true @entity.delete('id') end true end |
#digest ⇒ Object
rubocop:enable Style/GuardClause
31 32 33 34 35 |
# File 'lib/skull_island/helpers/resource.rb', line 31 def digest Digest::MD5.hexdigest( digest_properties.sort.map { |prop| "#{prop}=#{send(prop.to_sym)}" }.compact.join(':') ) end |
#digest_properties ⇒ Object
37 38 39 |
# File 'lib/skull_island/helpers/resource.rb', line 37 def digest_properties properties.keys.reject { |k| i[created_at updated_at].include? k } end |
#find_by_digest ⇒ Object
Tests for an existing version of this resource based on its properties rather than its ‘id`
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/skull_island/helpers/resource.rb', line 42 def find_by_digest result = self.class.where(:digest, digest) # matching digest means the equivalent resource if result.size == 1 entity_data = @api_client.cache(result.first.relative_uri.to_s) do |client| client.get(result.first.relative_uri.to_s) end @entity = entity_data @lazy = false @tainted = false true else false end end |
#fresh? ⇒ Boolean
57 58 59 |
# File 'lib/skull_island/helpers/resource.rb', line 57 def fresh? !tainted? end |
#host_regex ⇒ Object
61 62 63 |
# File 'lib/skull_island/helpers/resource.rb', line 61 def host_regex /^(([\w]|[\w][\w\-]*[\w])\.)*([\w]|[\w][\w\-]*[\w])$/ end |
#id ⇒ Object
69 70 71 |
# File 'lib/skull_island/helpers/resource.rb', line 69 def id @entity[id_property.to_s] end |
#id_property ⇒ Object
65 66 67 |
# File 'lib/skull_island/helpers/resource.rb', line 65 def id_property self.class.properties.select { |_, opts| opts[:id_property] }.keys.first || 'id' end |
#immutable? ⇒ Boolean
73 74 75 |
# File 'lib/skull_island/helpers/resource.rb', line 73 def immutable? self.class.immutable? end |
#import_update_or_skip(verbose: false, test: false, index:) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/skull_island/helpers/resource.rb', line 79 def import_update_or_skip(verbose: false, test: false, index:) if find_by_digest puts "[INFO] Skipping #{self.class} index #{index} (#{id})" if verbose elsif test puts "[INFO] Would have saved #{self.class} index #{index}" elsif modified_existing? puts "[INFO] Modified #{self.class} index #{index} (#{id})" if verbose elsif save puts "[INFO] Created #{self.class} index #{index} (#{id})" if verbose else puts "[ERR] Failed to save #{self.class} index #{index}" end end |
#lookup(type, value) ⇒ Object
rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/skull_island/helpers/resource.rb', line 95 def lookup(type, value) case type when :consumer { 'id' => Resources::Consumer.find(:username, value).id } when :route { 'id' => Resources::Route.find(:name, value).id } when :service { 'id' => Resources::Service.find(:name, value).id } when :upstream { 'id' => Resources::Upstream.find(:name, value).id } else raise Exceptions::InvalidArguments, "#{type} is not a valid lookup type" end end |
#model_name ⇒ Object
ActiveRecord ActiveModel::Name compatibility method
111 112 113 |
# File 'lib/skull_island/helpers/resource.rb', line 111 def model_name self.class end |
#new? ⇒ Boolean
115 116 117 |
# File 'lib/skull_island/helpers/resource.rb', line 115 def new? !@entity.key?(id_property.to_s) end |
#persisted? ⇒ Boolean
ActiveRecord ActiveModel::Model compatibility method
120 121 122 |
# File 'lib/skull_island/helpers/resource.rb', line 120 def persisted? !new? end |
#postprocess_created_at(value) ⇒ Object
124 125 126 |
# File 'lib/skull_island/helpers/resource.rb', line 124 def postprocess_created_at(value) Time.at(value).utc.to_datetime end |
#postprocess_updated_at(value) ⇒ Object
128 129 130 |
# File 'lib/skull_island/helpers/resource.rb', line 128 def postprocess_updated_at(value) Time.at(value).utc.to_datetime end |
#properties ⇒ Object
132 133 134 |
# File 'lib/skull_island/helpers/resource.rb', line 132 def properties self.class.properties end |
#prune_for_save(data) ⇒ Object
236 237 238 239 240 241 242 243 |
# File 'lib/skull_island/helpers/resource.rb', line 236 def prune_for_save(data) data.reject do |k, v| k.to_sym == id_property || !properties[k.to_sym] || properties[k.to_sym][:read_only] || v.nil? end end |
#reload ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/skull_island/helpers/resource.rb', line 166 def reload if new? # Can't reload a new resource false else @api_client.invalidate_cache_for(relative_uri.to_s) entity_data = @api_client.cache(relative_uri.to_s) do |client| client.get(relative_uri.to_s) end @entity = entity_data @lazy = false @tainted = false true end end |
#required_properties ⇒ Object
136 137 138 |
# File 'lib/skull_island/helpers/resource.rb', line 136 def required_properties properties.select { |_key, value| value[:required] } end |
#save ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/skull_island/helpers/resource.rb', line 182 def save saveable_data = prune_for_save(@entity) validate_required_properties(saveable_data) if new? @entity = @api_client.post(save_uri.to_s, saveable_data) @lazy = true else @api_client.invalidate_cache_for(relative_uri.to_s) @entity = @api_client.patch(relative_uri, saveable_data) end @api_client.invalidate_cache_for(self.class.relative_uri.to_s) # clear any collection class @tainted = false true end |
#save_uri ⇒ Object
198 199 200 |
# File 'lib/skull_island/helpers/resource.rb', line 198 def save_uri self.class.relative_uri end |
#tainted? ⇒ Boolean
140 141 142 |
# File 'lib/skull_island/helpers/resource.rb', line 140 def tainted? @tainted ? true : false end |
#to_param ⇒ Object
ActiveRecord ActiveModel::Conversion compatibility method
145 146 147 |
# File 'lib/skull_island/helpers/resource.rb', line 145 def to_param new? ? nil : id.to_s end |
#to_s ⇒ Object
149 150 151 |
# File 'lib/skull_island/helpers/resource.rb', line 149 def to_s to_param.to_s end |
#update(params) ⇒ Object
ActiveRecord ActiveModel compatibility method
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/skull_island/helpers/resource.rb', line 203 def update(params) new_params = {} # need to convert multi-part datetime params params.each do |key, value| if /([^(]+)\(1i/.match?(key) actual_key = key.match(/([^(]+)\(/)[1] new_params[actual_key] = datetime_from_params(params, actual_key) else new_params[key] = value end end new_params.each do |key, value| setter_key = "#{key}=".to_sym raise Exceptions::InvalidProperty unless respond_to?(setter_key) send(setter_key, value) end save end |