Skip to content

Commit

Permalink
Add polymorphism to MetricSnapshot class
Browse files Browse the repository at this point in the history
Removing if's in parent method and providing implementation
in proper subclasses

Signed-off-by: Pedro Ivo Andrade <[email protected]>
Signed-off-by: Vitor Barbosa <[email protected]>
  • Loading branch information
vitorbaraujo committed Jun 27, 2017
1 parent 3962d52 commit c2ac56f
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 93 deletions.
160 changes: 81 additions & 79 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -36,63 +36,63 @@ GEM
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
addressable (2.4.0)
arel (6.0.3)
ast (2.2.0)
addressable (2.5.1)
public_suffix (~> 2.0, >= 2.0.2)
arel (6.0.4)
ast (2.3.0)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
builder (3.2.2)
byebug (6.0.2)
capistrano (3.4.0)
builder (3.2.3)
byebug (9.0.6)
capistrano (3.4.1)
i18n
rake (>= 10.0.0)
sshkit (~> 1.3)
capistrano-bundler (1.1.4)
capistrano-bundler (1.2.0)
capistrano (~> 3.1)
sshkit (~> 1.2)
capistrano-rails (1.1.3)
capistrano-rails (1.3.0)
capistrano (~> 3.1)
capistrano-bundler (~> 1.1)
capistrano-rvm (0.1.2)
capistrano (~> 3.0)
sshkit (~> 1.2)
capybara (2.8.1)
capybara (2.14.3)
addressable
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
cliver (0.3.2)
codeclimate-test-reporter (0.4.7)
simplecov (>= 0.7.1, < 1.0.0)
coffee-rails (4.1.0)
codeclimate-test-reporter (1.0.8)
simplecov (<= 0.13)
coffee-rails (4.1.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
railties (>= 4.0.0, < 5.1.x)
coffee-script (2.4.1)
coffee-script-source
execjs
coffee-script-source (1.9.1.1)
colorize (0.7.7)
concurrent-ruby (1.0.2)
coffee-script-source (1.12.2)
concurrent-ruby (1.0.5)
cucumber (1.3.20)
builder (>= 2.1.2)
diff-lcs (>= 1.1.3)
gherkin (~> 2.12)
multi_json (>= 1.7.5, < 2.0)
multi_test (>= 0.1.2)
cucumber-rails (1.4.4)
cucumber-rails (1.5.0)
capybara (>= 1.1.2, < 3)
cucumber (>= 1.3.8, < 3)
mime-types (>= 1.16, < 4)
cucumber (>= 1.3.8, < 4)
mime-types (>= 1.17, < 4)
nokogiri (~> 1.5)
railties (>= 3, < 5.1)
database_cleaner (1.5.0)
debug_inspector (0.0.2)
diff-lcs (1.2.5)
railties (>= 4, < 5.2)
database_cleaner (1.6.1)
debug_inspector (0.0.3)
diff-lcs (1.3)
docile (1.1.5)
erubis (2.7.0)
execjs (2.6.0)
execjs (2.7.0)
factory_girl (4.5.0)
activesupport (>= 3.0.0)
factory_girl_rails (4.5.0)
Expand All @@ -102,46 +102,46 @@ GEM
thor (~> 0.19.1)
gherkin (2.12.2)
multi_json (~> 1.3)
globalid (0.3.7)
activesupport (>= 4.1.0)
i18n (0.7.0)
jbuilder (2.3.1)
activesupport (>= 3.0.0, < 5)
multi_json (~> 1.2)
json (1.8.3)
libv8 (3.16.14.11)
globalid (0.4.0)
activesupport (>= 4.2.0)
i18n (0.8.4)
jbuilder (2.7.0)
activesupport (>= 4.2.0)
multi_json (>= 1.2)
json (1.8.6)
libv8 (3.16.14.19)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mail (2.6.4)
mail (2.6.6)
mime-types (>= 1.16, < 4)
metaclass (0.0.4)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
minitest (5.9.0)
mini_portile2 (2.2.0)
minitest (5.10.2)
mocha (1.1.0)
metaclass (~> 0.0.1)
multi_json (1.12.1)
multi_test (0.1.2)
net-scp (1.2.1)
net-ssh (>= 2.6.5)
net-ssh (2.9.2)
nokogiri (1.6.8)
mini_portile2 (~> 2.1.0)
pkg-config (~> 1.1.7)
parser (2.3.0.6)
net-ssh (4.1.0)
nokogiri (1.8.0)
mini_portile2 (~> 2.2.0)
parallel (1.11.2)
parser (2.4.0.0)
ast (~> 2.2)
pg (0.18.2)
pkg-config (1.1.7)
pg (0.21.0)
poltergeist (1.6.0)
capybara (~> 2.1)
cliver (~> 0.3.1)
multi_json (~> 1.0)
websocket-driver (>= 0.2.0)
powerpack (0.1.1)
puma (3.6.0)
rack (1.6.4)
public_suffix (2.0.5)
puma (3.9.1)
rack (1.6.8)
rack-test (0.6.3)
rack (>= 1.0)
rails (4.2.4)
Expand All @@ -157,9 +157,9 @@ GEM
sprockets-rails
rails-deprecated_sanitizer (1.0.3)
activesupport (>= 4.2.0.alpha)
rails-dom-testing (1.0.7)
rails-dom-testing (1.0.8)
activesupport (>= 4.2.0.beta, < 5.0)
nokogiri (~> 1.6.0)
nokogiri (~> 1.6)
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
Expand All @@ -168,9 +168,10 @@ GEM
activesupport (= 4.2.4)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rainbow (2.1.0)
rake (11.2.2)
rdoc (4.2.0)
rainbow (2.2.2)
rake
rake (12.0.0)
rdoc (4.3.0)
ref (2.0.0)
rspec-core (3.3.2)
rspec-support (~> 3.3.0)
Expand All @@ -189,65 +190,66 @@ GEM
rspec-mocks (~> 3.3.0)
rspec-support (~> 3.3.0)
rspec-support (3.3.0)
rubocop (0.38.0)
parser (>= 2.3.0.6, < 3.0)
rubocop (0.49.1)
parallel (~> 1.10)
parser (>= 2.3.3.1, < 3.0)
powerpack (~> 0.1)
rainbow (>= 1.99.1, < 3.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.7.5)
sass (3.4.18)
sass-rails (5.0.3)
railties (>= 4.0.0, < 5.0)
ruby-progressbar (1.8.1)
sass (3.4.24)
sass-rails (5.0.6)
railties (>= 4.0.0, < 6)
sass (~> 3.1)
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (~> 1.1)
sdoc (0.4.1)
tilt (>= 1.1, < 3)
sdoc (0.4.2)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
shoulda-matchers (2.8.0)
activesupport (>= 3.0.0)
simplecov (0.10.0)
simplecov (0.13.0)
docile (~> 1.1.0)
json (~> 1.8)
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.0)
spring (1.3.6)
sprockets (3.7.0)
simplecov-html (0.10.1)
spring (2.0.2)
activesupport (>= 4.2)
sprockets (3.7.1)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.2.0)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
sshkit (1.7.1)
colorize (>= 0.7.0)
sshkit (1.13.1)
net-scp (>= 1.1.2)
net-ssh (>= 2.8.0)
therubyracer (0.12.2)
libv8 (~> 3.16.14.0)
therubyracer (0.12.3)
libv8 (~> 3.16.14.15)
ref
thor (0.19.1)
thread_safe (0.3.5)
tilt (1.4.1)
turbolinks (2.5.3)
coffee-rails
tzinfo (1.2.2)
thor (0.19.4)
thread_safe (0.3.6)
tilt (2.0.7)
turbolinks (5.0.1)
turbolinks-source (~> 5)
turbolinks-source (5.0.3)
tzinfo (1.2.3)
thread_safe (~> 0.1)
uglifier (2.7.2)
execjs (>= 0.3.0)
json (>= 1.8.0)
unicode-display_width (1.0.1)
web-console (2.2.1)
uglifier (3.2.0)
execjs (>= 0.3.0, < 3)
unicode-display_width (1.3.0)
web-console (2.3.0)
activemodel (>= 4.0)
binding_of_caller (>= 0.7.2)
railties (>= 4.0)
sprockets-rails (>= 2.0, < 4.0)
websocket-driver (0.6.2)
websocket-driver (0.6.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.2)
xpath (2.0.0)
xpath (2.1.0)
nokogiri (~> 1.3)

PLATFORMS
Expand Down
8 changes: 8 additions & 0 deletions app/models/compound_metric_snapshot.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
class CompoundMetricSnapshot < MetricSnapshot
def as_json(options = {})
# Type is considered by ActiveRecord as an implementation detail and so it is ignored by as_json
# Here we set it, since it is important in our case
json = super(options)
json['type'] = 'CompoundMetricSnapshot'
json.delete('metric_collector_name')
json
end
end
8 changes: 8 additions & 0 deletions app/models/hotspot_metric_snapshot.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
class HotspotMetricSnapshot < NativeMetricSnapshot
def as_json(options = {})
# Type is considered by ActiveRecord as an implementation detail and so it is ignored by as_json
# Here we set it, since it is important in our case
json = super(options)
json['type'] = 'HotspotMetricSnapshot'
json.delete('script')
json
end
end
16 changes: 2 additions & 14 deletions app/models/metric_snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,8 @@ class MetricSnapshot < ActiveRecord::Base
validates :script, presence: true, if: "type == 'CompoundMetricSnapshot'"

def as_json(options = {})
json = super(options)
# Type is considered by ActiveRecord as an implementation detail and so it is ignored by as_json
# Here we set it, since it is important in our case
if self.is_a?(HotspotMetricSnapshot)
json['type'] = 'HotspotMetricSnapshot'
json.delete('script')
elsif self.is_a?(NativeMetricSnapshot)
json['type'] = 'NativeMetricSnapshot'
json.delete('script')
elsif self.is_a?(CompoundMetricSnapshot)
json['type'] = 'CompoundMetricSnapshot'
json.delete('metric_collector_name')
end
json
super(options)
# This method should be implemented in child classes
end

def scope=(value)
Expand Down
8 changes: 8 additions & 0 deletions app/models/native_metric_snapshot.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
class NativeMetricSnapshot < MetricSnapshot
def as_json(options = {})
# Type is considered by ActiveRecord as an implementation detail and so it is ignored by as_json
# Here we set it, since it is important in our case
json = super(options)
json['type'] = 'NativeMetricSnapshot'
json.delete('script')
json
end
end

0 comments on commit c2ac56f

Please sign in to comment.