-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path01-06-nil.rb
More file actions
109 lines (83 loc) · 1.67 KB
/
01-06-nil.rb
File metadata and controls
109 lines (83 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
class Person
attr_reader :name
def initialize(name)
@name = name
end
def self.find(id)
people = { 1 => new("Jil"), 2 => new("Bob") }
people[id]
end
end
class Subscription
def self.create_for_person(person)
create!(person: person, person_name: person.name)
end
def self.create!(*args)
"Yay!"
end
end
class SubscriptionController
def create(person_id)
person = Person.find(person_id)
Subscription.create_for_person(person)
end
end
puts SubscriptionController.new.create(1)
# Example 2
# class Person
# attr_reader :subscription
# def subscribe!
# @subscription = Subscription.new
# end
# end
# class Subscription
# end
# person = Person.new
# person.subscribe!
# puts person.subscription
# Example 3
# class Person
# end
# class Subscription
# attr_reader :person
# end
# Example 4
# class Person
# def subscribe!
# @subscription = Subscription.new
# end
# def subscription
# @subscription or raise NoSubscriptionError
# end
# end
# class NoSubscriptionError < Exception; end
# class Subscription
# end
# person = Person.new
# if false
# person.subscribe!
# end
# puts person.subscription
# Example 5
# class Person
# def subscribe
# Subscriber.new(Subscription.new)
# end
# end
# class Subscriber
# attr_reader :subscription
# def initialize(subscription)
# @subscription = subscription
# end
# end
# class Subscription
# end
# person = Person.new
# if true
# subscriber = person.subscribe
# end
# puts subscriber.subscription
# use your language & libraries intelligently
# invert the object relationship
# guard against nils in a manual attribute reader
# Introduce new domain concepts