Module: Cloudflair::Entity

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Communication

#connection, #hash_to_object, #response

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name_as_symbol, *args, &block) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# 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

Class Method Details

.included(other_klass) ⇒ Object



9
10
11
# File 'lib/cloudflair/entity.rb', line 9

def self.included(other_klass)
  other_klass.extend ClassMethods
end

Instance Method Details

#data=(data) ⇒ Object

:internal: Used to pre-populate an entity



170
171
172
# File 'lib/cloudflair/entity.rb', line 170

def data=(data)
  @data = data
end

#deleteObject



108
109
110
111
112
113
114
115
116
# 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

#patchObject Also known as: save



100
101
102
103
104
105
106
# 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

#reloadObject Also known as: get!



94
95
96
97
98
# File 'lib/cloudflair/entity.rb', line 94

def reload
  @data = get
  revert
  self
end

#respond_to_missing?(name_as_symbol, *args) ⇒ Boolean

Returns:

  • (Boolean)


154
155
156
157
158
159
160
161
162
163
164
165
166
# 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

#revertObject



90
91
92
# File 'lib/cloudflair/entity.rb', line 90

def revert
  dirty_data.clear
end

#update(updated_fields) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# 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