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
|
# File 'lib/hyraft/rule/circuit/engine_command.rb', line 39
def self.generate_content(plural_name, singular_name, class_name, concrete_class_name, attributes)
attr_readers = attributes.map { |a| " attr_reader :#{a}" }.join("\n")
init_params = attributes.map { |a| "#{a}:" }.join(", ")
init_assignments = attributes.map { |a| " @#{a} = #{a}" }.join("\n")
to_h_merge = attributes.map { |a| "#{a}: @#{a}" }.join(", ")
create_params = attributes.join(", ")
create_args = attributes.join(", ")
update_params = attributes.map { |a| "#{a}:" }.join(", ")
update_args = attributes.map { |a| "#{a}: #{a}" }.join(", ")
<<~RUBY
# frozen_string_literal: true
pathway 'circuit/ports/#{plural_name}_port'
# ============================================================================
# DOMAIN LAYER
# ============================================================================
class #{singular_name.capitalize} < Hyraft::Engine
#{attr_readers.empty? ? " # Add attributes here" : attr_readers}
def initialize(id: nil, #{init_params}, created_at: nil, updated_at: nil)
super(id: id, created_at: created_at, updated_at: updated_at)
#{init_assignments}
end
def to_h
super.merge(#{to_h_merge})
end
end
# ============================================================================
# CONCRETE IMPLEMENTATION
# ============================================================================
class #{concrete_class_name} < #{class_name}InputPort
def initialize(gateway)
@gateway = gateway
end
def create(#{create_params})
@gateway.save(#{singular_name.capitalize}.new(#{update_args})).to_h
end
def get(id)
@gateway.find(id).to_h
end
def list
@gateway.all.map(&:to_h)
end
def update(id, #{create_params})
article = @gateway.find(id)
updated = #{singular_name.capitalize}.new(id: id, #{update_args}, created_at: article.created_at)
@gateway.save(updated).to_h
end
def delete(id)
@gateway.delete(id)
{deleted: true}
end
end
RUBY
end
|