Class: Hyraft::Rule::AdapterRequest::WebAdapterCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/hyraft/rule/adapter_request/web_adapter_command.rb

Class Method Summary collapse

Class Method Details

.show_usageObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hyraft/rule/adapter_request/web_adapter_command.rb', line 33

def self.show_usage
  puts "Usage: hyraft-rule web-adapter [folder_name/]<AdapterName> [target_dir]"
  puts ""
  puts "Examples:"
  puts "  hyraft-rule web-adapter Articles"
  puts "  hyraft-rule web-adapter admin/Users"
  puts "  hyraft-rule web-adapter api-app/Products"
  puts "  hyraft-rule web-adapter mobile/Categories"
  puts ""
  puts "Default folder: web-app"
end

.start(args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hyraft/rule/adapter_request/web_adapter_command.rb', line 9

def self.start(args)
  input = args[0]
  return show_usage unless input

  if input.include?('/')
    folder_name, adapter_name = input.split('/', 2)
  else
    folder_name = "web-app"
    adapter_name = input
  end

  target_dir = args[1] || "."
  adapters_dir = File.join(target_dir, "adapter-intake", folder_name, "request")
  full_path = File.join(adapters_dir, "#{adapter_name.downcase}_web_adapter.rb")

  FileUtils.mkdir_p(adapters_dir)
  File.write(full_path, web_adapter_template(adapter_name, folder_name))

  puts "\e[94m✓ Created web adapter: #{full_path}\e[0m"
  puts "\e[38;5;214mApp folder name: #{folder_name}, Adapter: #{adapter_name}\e[0m"
end

.web_adapter_template(adapter_name, folder_name = "web-app") ⇒ Object



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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/hyraft/rule/adapter_request/web_adapter_command.rb', line 45

def self.web_adapter_template(adapter_name, folder_name = "web-app")
  singular_name = adapter_name.downcase.chomp('s')
  adapter_class_name = adapter_name.split('_').map(&:capitalize).join  # "articles" → "Articles"
  plural_name = singular_name + 's'
  circuit_name = "#{adapter_class_name}Circuit"
  gateway_name = "Sequel#{adapter_class_name}Gateway"

  <<~RUBY
require_root 'engine/circuit/#{plural_name}_circuit'
require_root 'adapter-exhaust/data-gateway/sequel_#{plural_name}_gateway'

class #{adapter_class_name}WebAdapter
  def initialize
    gateway = #{gateway_name}.new
    @#{plural_name} = #{circuit_name}.new(gateway)
  end

  # GET /#{plural_name}
  def index(request)
    #{plural_name} = @#{plural_name}.list || []
    
    {
      status: 200,
      locals: { 
#{plural_name}: #{plural_name}
      },
      display: 'pages/#{plural_name}/index.hyr'
    }
  end

  # GET /#{plural_name}/:id
  def show(request)
    id = request.params['route_params'].first
    #{singular_name} = @#{plural_name}.find(id)

    if #{singular_name}
      {
status: 200,
locals: { 
  #{singular_name}: #{singular_name}
},
display: 'pages/#{plural_name}/show.hyr'
      }
    else
      not_found_response(request)
    end
  end

  # GET /#{plural_name}/new - Show create form
  def new(request)
    {
      status: 200,
      locals: {},
      display: 'pages/#{plural_name}/new.hyr'
    }
  end

  # POST /#{plural_name} - Create #{singular_name}
  def create(request)
    data = request.params['data'] || {}
    
    # TODO: Add validation for required fields
    # Example:
    # if data['title'] && data['content']
    #   article = @articles.create(
    #     title: data['title'],
    #     content: data['content']
    #   )
    #   
    #   {
    #     status: 303,
    #     headers: { 'Location' => "/articles" },
    #     locals: {}
    #   }
    # else
    #   {
    #     status: 422,
    #     locals: { 
    #       error: "Title and content are required"
    #     },
    #     display: 'pages/articles/new.hyr'
    #   }
    # end
    
  end

  # GET /#{plural_name}/:id/edit - Show edit form
  def edit(request)
    id = request.params['route_params'].first
    #{singular_name} = @#{plural_name}.find(id)
    
    if #{singular_name}
      {
status: 200,
locals: { 
  #{singular_name}: #{singular_name}
},
display: 'pages/#{plural_name}/edit.hyr'
      }
    else
      not_found_response(request)
    end
  end

  # PUT /#{plural_name}/:id - Update #{singular_name}
  def update(request)
    id = request.params['route_params'].first
    data = request.params['data'] || {}
    
    # TODO: Implement update logic
    # Example:
    # updated_#{singular_name} = @#{plural_name}.update(
    #   id: id,
    #   title: data['title'],
    #   content: data['content']
    # )
    
    if updated_#{singular_name}
      {
status: 303,
headers: { 'Location' => "/#{plural_name}" },
locals: {}
      }
    else
      {
status: 422,
locals: { 
  #{singular_name}: @#{plural_name}.find(id),
  error: "Failed to update #{singular_name}" 
},
display: 'pages/#{plural_name}/edit.hyr'
      }
    end
  end

  # DELETE /#{plural_name}/:id - Delete #{singular_name}
  def delete(request)
    id = request.params['route_params'].first
    @#{plural_name}.delete(id)
    
    {
      status: 303,
      headers: { 'Location' => "/#{plural_name}" },
      locals: {}
    }
  end

  private

  def not_found_response(request)
    {
      status: 404,
      locals: { 
error: "#{singular_name.capitalize} not found",
back_url: '/#{plural_name}',  
back_text: 'Back to #{adapter_name}'  
      },
      display: 'pages/#{plural_name}/404.hyr'
    }
  end
end
  RUBY
end