File: null.out.rb

package info (click to toggle)
ruby-algebrick 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 276 kB
  • sloc: ruby: 1,614; makefile: 3
file content (40 lines) | stat: -rw-r--r-- 1,120 bytes parent folder | download | duplicates (3)
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
extend Algebrick::Matching                         # => main

def deliver_email(email)
  true
end 

Contact = Algebrick.type do |contact|
  variants Null = atom,
           contact
  fields username: String, email: String
end
    # => Contact(Null | Contact(username: String, email: String))

module Contact
  def username
    match self,
          Null >> 'no name',
          Contact >-> { self[:username] }
  end
  def email
    match self,
          Null >> 'no email',
          Contact >-> { self[:email] }
  end
  def deliver_personalized_email
    match self,
          Null >> true,
          Contact >-> { deliver_email(self.email) }
  end
end 

peter  = Contact['peter', 'example@dot.com']       # => Contact[username: peter, email: example@dot.com]
john   = Contact[username: 'peter', email: 'example@dot.com']
    # => Contact[username: peter, email: example@dot.com]
nobody = Null                                      # => Null

[peter, john, nobody].map &:email
    # => ["example@dot.com", "example@dot.com", "no email"]
[peter, john, nobody].map &:deliver_personalized_email
    # => [true, true, true]