module Cloudflair::Entity
Public Class Methods
included(other_klass)
click to toggle source
# File lib/cloudflair/entity.rb, line 9 def self.included(other_klass) other_klass.extend ClassMethods end
Public Instance Methods
data=(data)
click to toggle source
:internal: Used to pre-populate an entity
# File lib/cloudflair/entity.rb, line 170 def data=(data) @data = data end
delete()
click to toggle source
# File lib/cloudflair/entity.rb, line 108 def delete raise Cloudflair::CloudflairError, "Can't delete unless deletable=true" unless deletable return self if @deleted @data = response connection.delete path @deleted = true revert self end
method_missing(name_as_symbol, *args, &block)
click to toggle source
Calls superclass method
# File lib/cloudflair/entity.rb, line 130 def method_missing(name_as_symbol, *args, &block) name = normalize_accessor name_as_symbol return data if name_as_symbol == :_raw_data! if name.end_with?('=') if patchable_fields.include?(name[0..-2]) dirty_data[name[0..-2]] = args[0] return end super end # allow access to the unmodified data using 'zone.always_string!' or 'zone._name!' return data[name[0..2]] if name.end_with?('!') && data.key?(name[0..-2]) return objectify(name) if object_fields.include? name return arrayify(name, array_object_fields[name]) if array_object_fields.key?(name) return dirty_data[name] if dirty_data.key?(name) return data[name] if data.is_a?(Hash) && data.key?(name) super end
patch()
click to toggle source
# File lib/cloudflair/entity.rb, line 100 def patch return self if dirty_data.empty? @data = response connection.patch path, dirty_data revert self end
Also aliased as: save
reload()
click to toggle source
# File lib/cloudflair/entity.rb, line 94 def reload @data = get revert self end
Also aliased as: get!
respond_to_missing?(name_as_symbol, *args)
click to toggle source
Calls superclass method
# File lib/cloudflair/entity.rb, line 154 def respond_to_missing?(name_as_symbol, *args) name = normalize_accessor name_as_symbol return true if name_as_symbol == :_raw_data! return true if name.end_with?('=') && patchable_fields.include?(name[0..-2]) return true if name.end_with?('!') && data.key?(name[0..-2]) return true if object_fields.include? name return true if array_object_fields.key?(name) return true if dirty_data.key?(name) return true if data.is_a?(Hash) && data.key?(name) super end
revert()
click to toggle source
# File lib/cloudflair/entity.rb, line 90 def revert dirty_data.clear end
update(updated_fields)
click to toggle source
# File lib/cloudflair/entity.rb, line 118 def update(updated_fields) checked_updated_fields = {} updated_fields.each do |key, values| s_key = normalize_accessor key checked_updated_fields[s_key] = values if patchable_fields.include? s_key end dirty_data.merge! checked_updated_fields patch end
Private Instance Methods
array_object_fields()
click to toggle source
# File lib/cloudflair/entity.rb, line 207 def array_object_fields self.class.array_object_fields end
arrayify(name, klass_or_proc = nil)
click to toggle source
# File lib/cloudflair/entity.rb, line 215 def arrayify(name, klass_or_proc = nil) data[name].map do |data| if klass_or_proc.nil? hash_to_object data elsif klass_or_proc.is_a? Proc klass_or_proc.call data else klass_or_proc.new data end end end
data()
click to toggle source
# File lib/cloudflair/entity.rb, line 187 def data @data ||= get end
deletable()
click to toggle source
# File lib/cloudflair/entity.rb, line 199 def deletable self.class.deletable end
dirty_data()
click to toggle source
# File lib/cloudflair/entity.rb, line 243 def dirty_data @dirty_data ||= {} end
get()
click to toggle source
# File lib/cloudflair/entity.rb, line 191 def get response connection.get path end
normalize_accessor(symbol_or_string)
click to toggle source
# File lib/cloudflair/entity.rb, line 179 def normalize_accessor(symbol_or_string) always_string = symbol_or_string.to_s # allows access to remote data who's name conflicts with pre-defined methods # e.g. write 'zone._zone_id' instead of 'zone.zone_id' to access the remote value of 'zone_id' always_string.start_with?('_') ? always_string[1..-1] : always_string end
object_fields()
click to toggle source
# File lib/cloudflair/entity.rb, line 203 def object_fields self.class.object_fields end
objectify(name)
click to toggle source
# File lib/cloudflair/entity.rb, line 211 def objectify(name) hash_to_object data[name] end
patchable_fields()
click to toggle source
# File lib/cloudflair/entity.rb, line 195 def patchable_fields self.class.patchable_fields end
path()
click to toggle source
# File lib/cloudflair/entity.rb, line 227 def path return @path if @path @path = replace_path_variables_in self.class.path end
replace_path_variables_in(path)
click to toggle source
# File lib/cloudflair/entity.rb, line 233 def replace_path_variables_in(path) interpreted_path = path.clone path.scan(/:([a-zA-Z_][a-zA-Z0-9_]+[!?=]?)/) do |match, *| interpreted_path = interpreted_path.gsub ":#{match}", send(match).to_s end interpreted_path end