github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

shvets / design_patterns_in_ruby

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 165
    • 5
  • Source
  • Commits
  • Network (5)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Implementation of Design Patterns in Ruby — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

added act_as_observable to make dynamic code generation for observables Comment
shvets (author)
Sun Mar 22 08:28:29 -0700 2009
commit  698dcfab8a2d89909d49f8b6207beb7df4563bf1
tree    e63925a79d07d54eb8612e034e06f535a6768960
parent  b739593f4346a72e183c0b2ec95ea3ac5d6baace
design_patterns_in_ruby / enterprise / data-access-object.rb enterprise/data-access-object.rb
100644 193 lines (135 sloc) 3.123 kb
edit raw blame history
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# data-access-object.bsh
 
# 1.
 
class TransferObject
  def data(key)
  end
 
  def set_data(key, value)
  end
end
 
class TransferObjectDAO
  def set_data_source(dataSource)
  end
 
  def create
  end
 
  def update(transfer_object)
  end
 
  def delete(transfer_object)
  end
 
  def find(id)
  end
 
  def find_all
  end
end
 
class DataSource
  def generate_next_id
  end
 
  def execute_command(command, param)
  end
end
 
class TransferObjectDAOFactory
  def transfer_object_dao
  end
end
 
# 2.
 
class MyTransferObject < TransferObject
  def initialize
    @data = {}
  end
 
  def data(key)
    @data[key]
  end
 
  def set_data(key, value)
    @data[key] = value
  end
 
  def to_s
    @data.to_s
  end
end
 
class MyDataSource < DataSource
  def initialize
    @generated_id = 0
 
    @objects = []
  end
 
  def generate_next_id
    @generated_id = @generated_id + 1
  end
 
  def execute_command(command, param=nil)
    result = nil
 
    if(command == :insert)
      result = (@objects << param)
    elsif(command == :update)
      index = @objects.index(param)
      @objects.delete_at(index)
      @objects.insert(index, param)
 
      result = true
    elsif(command == :delete)
      result = (@objects.delete(param))
    elsif(command == :find)
      id = param
      
      @objects.each do |transfer_object|
        current_id = transfer_object.data("id")
 
        if(current_id == id)
          result = transfer_object
 
          break
        end
      end
    elsif(command == :find_all)
      result = @objects
    end
 
    result
  end
end
 
class MyTransferObjectDAO < TransferObjectDAO
 
  def data_source=(data_source)
    @data_source = data_source
  end
 
  def create
    transfer_object = MyTransferObject.new
 
    id = @data_source.generate_next_id
 
    transfer_object.set_data("id", id)
 
    @data_source.execute_command(:insert, transfer_object)
 
    id
  end
 
  def update(transfer_object)
    @data_source.execute_command(:update, transfer_object)
  end
  
  def delete(transfer_object)
    @data_source.execute_command(:delete, transfer_object)
  end
 
  def find(id)
    @data_source.execute_command(:find, id)
  end
 
  def find_all
    @data_source.execute_command(:find_all)
  end
end
 
class MyTransferObjectDAOFactory < TransferObjectDAOFactory
 
  def initialize(data_source)
    @data_source = data_source
  end
 
  def transfer_object_dao
    transfer_object_dao = MyTransferObjectDAO.new
    
    transfer_object_dao.data_source = @data_source
    
    transfer_object_dao
  end
end
 
 
# test - or business object, or client
 
dataSource = MyDataSource.new
factory = MyTransferObjectDAOFactory.new(dataSource)
 
dao = factory.transfer_object_dao
 
# create a new domain object
new_id = dao.create
 
# Find a domain object.
o1 = dao.find(new_id)
 
puts "o1: " + o1.to_s
 
# modify the values in the Transfer Object.
o1.set_data("address", "a1")
o1.set_data("email", "em1")
 
# update the domain object using the DAO
dao.update(o1)
 
puts "o1: " + o1.to_s
 
list = dao.find_all
 
puts "list before delete: " + list.join(', ')
 
# delete a domain object
dao.delete(o1)
 
puts "list after delete: " + list.join(', ')
 
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server