Class: BugBunny::Resource

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::API, ActiveModel::Attributes, ActiveModel::Dirty, ActiveModel::Validations
Defined in:
lib/bug_bunny/resource.rb

Defined Under Namespace

Classes: ExchangeScope

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Resource

Returns a new instance of Resource.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bug_bunny/resource.rb', line 49

def initialize(attributes = {})
  attributes.each do |key, value|
    attribute_name = key.to_sym
    type = guess_type(value)

    next if self.class.attribute_names.include?(attribute_name.to_s)

    self.class.attribute attribute_name, type if type.present?
  end

  super(attributes)

  @previously_persisted = persisted
  clear_changes_information if persisted?
end

Class Attribute Details

.resource_nameObject



12
13
14
# File 'lib/bug_bunny/resource.rb', line 12

def resource_name
  @resource_name ||= name.demodulize.underscore
end

.resource_pathObject

Returns the value of attribute resource_path.



9
10
11
# File 'lib/bug_bunny/resource.rb', line 9

def resource_path
  @resource_path
end

Class Method Details

.create_actionObject



210
211
212
# File 'lib/bug_bunny/resource.rb', line 210

def self.create_action
  :create
end

.current_exchangeObject



162
163
164
# File 'lib/bug_bunny/resource.rb', line 162

def self.current_exchange
  Thread.current[:bugbunny_current_exchange]
end

.current_routing_keyObject



166
167
168
# File 'lib/bug_bunny/resource.rb', line 166

def self.current_routing_key
  Thread.current[:bugbunny_current_routing_key]
end

.destroy_actionObject



218
219
220
# File 'lib/bug_bunny/resource.rb', line 218

def self.destroy_action
  :destroy
end

.execute(name, *args, **kwargs, &block) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/bug_bunny/resource.rb', line 147

def self.execute(name, *args, **kwargs, &block)
  original_exchange = Thread.current[:bugbunny_current_exchange]
  Thread.current[:bugbunny_current_exchange] = kwargs[:exchange]
  original_routing_key = Thread.current[:bugbunny_current_routing_key]
  Thread.current[:bugbunny_current_routing_key] = kwargs[:routing_key]
  begin
    kwargs.delete(:exchange)
    kwargs.delete(:routing_key)
    send(name, *args, **kwargs, &block)
  ensure
    Thread.current[:bugbunny_current_exchange] = original_exchange
    Thread.current[:bugbunny_current_routing_key] = original_routing_key
  end
end

.for_exchange(exchange_name, routing_key = nil) ⇒ Object

Raises:

  • (ArgumentError)


141
142
143
144
145
# File 'lib/bug_bunny/resource.rb', line 141

def self.for_exchange(exchange_name, routing_key = nil)
  raise ArgumentError, 'Exchange name must be specified.' if exchange_name.blank?

  ExchangeScope.new(self, exchange_name, routing_key)
end

.index_actionObject



202
203
204
# File 'lib/bug_bunny/resource.rb', line 202

def self.index_action
  :index
end

.inherited(subclass) ⇒ Object



16
17
18
19
20
# File 'lib/bug_bunny/resource.rb', line 16

def inherited(subclass)
  super

  subclass.resource_path = resource_path if resource_path.present?
end

.publisherObject



222
223
224
225
226
227
228
# File 'lib/bug_bunny/resource.rb', line 222

def self.publisher
  @publisher ||= if resource_path.end_with?('/')
                   [resource_path, resource_name].join('').camelize.constantize
                 else
                   [resource_path, resource_name].join('/').camelize.constantize
                 end
end

.show_actionObject



206
207
208
# File 'lib/bug_bunny/resource.rb', line 206

def self.show_action
  :show
end

.update_actionObject



214
215
216
# File 'lib/bug_bunny/resource.rb', line 214

def self.update_action
  :update
end

Instance Method Details

#assign_attributes(attrs) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bug_bunny/resource.rb', line 65

def assign_attributes(attrs)
  return if attrs.blank?

  attrs.each do |key, val|
    setter_method = "#{key.to_s.underscore}="

    next unless respond_to?(setter_method)

    send(setter_method, val)
  end
end

#changes_to_sendObject



97
98
99
100
101
# File 'lib/bug_bunny/resource.rb', line 97

def changes_to_send
  attrs = {}
  changes.each { |attribute, values| attrs[attribute] = values[1] }
  attrs
end

#current_exchangeObject



133
134
135
# File 'lib/bug_bunny/resource.rb', line 133

def current_exchange
  self.class.current_exchange
end

#current_routing_keyObject



137
138
139
# File 'lib/bug_bunny/resource.rb', line 137

def current_routing_key
  self.class.current_routing_key
end

#destroyObject



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/bug_bunny/resource.rb', line 120

def destroy
  return self unless persisted?

  # Llamada al PUBLISHER sin el argumento 'box'
  self.class.publisher.send(destroy_action.to_sym, exchange: current_exchange, routing_key: current_routing_key, id: id)

  self.persisted = false
  true
rescue BugBunny::ResponseError::UnprocessableEntity => e
  load_remote_rabbit_errors(e.message)
  false
end

#guess_type(value) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/bug_bunny/resource.rb', line 77

def guess_type(value)
  case value
  when Integer then :integer
  when Float then :float
  when Date then :date
  when Time, DateTime then :datetime
  when TrueClass, FalseClass then :boolean
  when String then :string
  end
end

#load_remote_rabbit_errors(remote_errors) ⇒ Object



230
231
232
233
234
235
236
# File 'lib/bug_bunny/resource.rb', line 230

def load_remote_rabbit_errors(remote_errors)
  JSON.parse(remote_errors).each do |attribute, errors|
    errors.each do |error|
      self.errors.add(attribute, error['error'], **error.except('error').symbolize_keys)
    end
  end
end

#persisted?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/bug_bunny/resource.rb', line 88

def persisted?
  persisted
end

#saveObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/bug_bunny/resource.rb', line 103

def save
  action = persisted? ? self.class.update_action.to_sym : self.class.create_action.to_sym

  return self if persisted? && changes.empty?

  obj = self.class.publisher.send(action, exchange: current_exchange, routing_key: current_routing_key, message: changes_to_send)

  assign_attributes(obj) # refresco el objeto
  self.persisted = true
  @previously_persisted = true
  clear_changes_information
  true
rescue BugBunny::ResponseError::UnprocessableEntity => e
  load_remote_rabbit_errors(e.message)
  false
end

#update(attrs_changes) ⇒ Object



92
93
94
95
# File 'lib/bug_bunny/resource.rb', line 92

def update(attrs_changes)
  assign_attributes(attrs_changes)
  save
end