Skip to content

Commit 11188a6

Browse files
author
coding-chimp
committed
Dynamically create model attributes on initialization
1 parent 1679bbf commit 11188a6

8 files changed

Lines changed: 36 additions & 49 deletions

File tree

lib/onesignal/models/app.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ module OneSignal
44
class App < BaseModel
55
DATE_REGEX = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/
66

7-
attr_accessor :apns_certificates, :apns_env, :basic_auth_key, :chrome_key,
8-
:chrome_web_default_notification_icon, :chrome_web_gcm_sender_id,
9-
:chrome_web_origin, :chrome_web_sub_domain, :created_at, :gcm_key, :id,
10-
:messageable_players, :name, :players, :safari_apns_certificate,
11-
:safari_icon_128_128, :safari_icon_16_16, :safari_icon_256_256,
12-
:safari_icon_32_32, :safari_icon_64_64, :safari_push_id,
13-
:safari_site_origin, :site_name, :updated_at
14-
157
def created_at=(time)
168
parse_time('created_at', time)
179
end

lib/onesignal/models/base_model.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ def self.to_proc
55
end
66

77
def initialize(attributes = {})
8-
attributes.each { |key, value| send("#{key}=", value) }
8+
attributes.each do |key, value|
9+
create_attr(key)
10+
send("#{key}=", value)
11+
end
912
end
1013

1114
def inspect
@@ -15,5 +18,23 @@ def inspect
1518

1619
"<#{self.class.name} #{values}>"
1720
end
21+
22+
private
23+
24+
def create_attr(name)
25+
create_method("#{name}=".to_sym) do |value|
26+
instance_variable_set("@#{name}", value)
27+
end
28+
29+
create_method(name.to_sym) do
30+
instance_variable_get("@#{name}")
31+
end
32+
end
33+
34+
def create_method(name, &block)
35+
unless respond_to?(name)
36+
self.class.send(:define_method, name, &block)
37+
end
38+
end
1839
end
1940
end

lib/onesignal/models/notification.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
module OneSignal
22
class Notification < BaseModel
3-
attr_accessor :canceled, :contents, :converted, :data, :errored, :failed,
4-
:headings, :id, :include_player_ids, :included_segments, :isAdm,
5-
:isAndroid, :isChrome, :isChromeWeb, :isFirefox, :isIos, :isSafari,
6-
:isWP, :isWP_WNS, :queued_at, :remaining, :send_after, :successful, :url
7-
83
def queued_at=(time)
94
@queued_at = Time.at(time)
105
end

lib/onesignal/models/player.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
module OneSignal
22
class Player < BaseModel
3-
attr_accessor :ad_id, :amount_spent, :badge_count, :created_at,
4-
:device_model, :device_os, :device_type, :game_version, :id, :identifier,
5-
:invalid_identifier, :language, :last_active, :playtime, :session_count,
6-
:sdk, :tags, :timezone
7-
83
def created_at=(time)
94
@created_at = Time.at(time)
105
end

test/models/app_test.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,32 @@
33
class AppTest < Minitest::Test
44
def test_created_at_setter_parses_string_dates
55
time = '2014-04-01T04:20:02.003Z'
6-
app = OneSignal::App.new
76

8-
app.created_at = time
7+
app = OneSignal::App.new(created_at: time)
98

109
assert_equal Time.parse(time), app.created_at
1110
end
1211

1312
def test_created_at_setter_leaves_times_alone
1413
time = Time.now
15-
app = OneSignal::App.new
1614

17-
app.created_at = time
15+
app = OneSignal::App.new(created_at: time)
1816

1917
assert_equal time, app.created_at
2018
end
2119

2220
def test_updated_at_setter_parses_string_dates
2321
time = '2014-04-01T04:20:02.003Z'
24-
app = OneSignal::App.new
2522

26-
app.updated_at = time
23+
app = OneSignal::App.new(updated_at: time)
2724

2825
assert_equal Time.parse(time), app.updated_at
2926
end
3027

3128
def test_updated_at_setter_leaves_times_alone
3229
time = Time.now
33-
app = OneSignal::App.new
3430

35-
app.updated_at = time
31+
app = OneSignal::App.new(updated_at: time)
3632

3733
assert_equal time, app.updated_at
3834
end

test/models/base_model_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require_relative '../test_helper'
22

33
class DummyModel < OneSignal::BaseModel
4-
attr_accessor :id, :name, :created_at
54
end
65

76
class BaseModelTest < Minitest::Test

test/models/notification_test.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,32 @@
33
class NotificationTest < Minitest::Test
44
def test_queued_at_setter_parses_integers
55
time = 1415914655
6-
notification = OneSignal::Notification.new
76

8-
notification.queued_at = time
7+
notification = OneSignal::Notification.new(queued_at: time)
98

109
assert_equal Time.at(time), notification.queued_at
1110
end
1211

1312
def test_queued_at_setter_leaves_times_alone
1413
time = Time.now
15-
notification = OneSignal::Notification.new
1614

17-
notification.queued_at = time
15+
notification = OneSignal::Notification.new(queued_at: time)
1816

1917
assert_equal time, notification.queued_at
2018
end
2119

2220
def test_send_after_setter_parses_integers
2321
time = 1415914655
24-
notification = OneSignal::Notification.new
2522

26-
notification.send_after = time
23+
notification = OneSignal::Notification.new(send_after: time)
2724

2825
assert_equal Time.at(time), notification.send_after
2926
end
3027

3128
def test_send_after_setter_leaves_times_alone
3229
time = Time.now
33-
notification = OneSignal::Notification.new
3430

35-
notification.send_after = time
31+
notification = OneSignal::Notification.new(send_after: time)
3632

3733
assert_equal time, notification.send_after
3834
end

test/models/player_test.rb

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,46 @@
33
class PlayerTest < Minitest::Test
44
def test_created_at_setter_parses_integers
55
time = 1415914655
6-
player = OneSignal::Player.new
76

8-
player.created_at = time
7+
player = OneSignal::Player.new(created_at: time)
98

109
assert_equal Time.at(time), player.created_at
1110
end
1211

1312
def test_created_at_setter_leaves_times_alone
1413
time = Time.now
15-
player = OneSignal::Player.new
1614

17-
player.created_at = time
15+
player = OneSignal::Player.new(created_at: time)
1816

1917
assert_equal time, player.created_at
2018
end
2119

2220
def test_device_type_setter_parses_integers
23-
player = OneSignal::Player.new
24-
25-
player.device_type = 0
21+
player = OneSignal::Player.new(device_type: 0)
2622

2723
assert_equal 'ios', player.device_type
2824
end
2925

3026
def test_device_type_setter_leaves_times_alone
3127
type = 'ios'
32-
player = OneSignal::Player.new
3328

34-
player.device_type = type
29+
player = OneSignal::Player.new(device_type: type)
3530

3631
assert_equal type, player.device_type
3732
end
3833

3934
def test_last_active_setter_parses_integers
4035
time = 1415914655
41-
player = OneSignal::Player.new
4236

43-
player.last_active = time
37+
player = OneSignal::Player.new(last_active: time)
4438

4539
assert_equal Time.at(time), player.last_active
4640
end
4741

4842
def test_last_active_setter_leaves_times_alone
4943
time = Time.now
50-
player = OneSignal::Player.new
5144

52-
player.last_active = time
45+
player = OneSignal::Player.new(last_active: time)
5346

5447
assert_equal time, player.last_active
5548
end

0 commit comments

Comments
 (0)