Class: Shaf::Generator::Serializer
- Inherits:
-
Base
- Object
- Base
- Shaf::Generator::Serializer
show all
- Defined in:
- lib/shaf/generator/serializer.rb
Instance Attribute Summary
Attributes inherited from Base
#args, #options
Instance Method Summary
collapse
Methods inherited from Base
identified_by, identifier, #identifier, inherited, #initialize, options, usage
Instance Method Details
#attribute_names ⇒ Object
62
63
64
|
# File 'lib/shaf/generator/serializer.rb', line 62
def attribute_names
attributes.map { |arg| arg.split(':').first }
end
|
#attributes ⇒ Object
58
59
60
|
# File 'lib/shaf/generator/serializer.rb', line 58
def attributes
args[1..-1]
end
|
#attributes_with_doc ⇒ Object
66
67
68
|
# File 'lib/shaf/generator/serializer.rb', line 66
def attributes_with_doc
attribute_names.map { |attr| ["attribute :#{attr}"] }
end
|
#call ⇒ Object
7
8
9
10
11
12
|
# File 'lib/shaf/generator/serializer.rb', line 7
def call
create_serializer
create_serializer_spec if options[:specs]
create_policy
create_profile
end
|
#collection_link ⇒ Object
97
98
99
100
101
102
103
|
# File 'lib/shaf/generator/serializer.rb', line 97
def collection_link
link(
rel: "collection",
uri_helper: "#{collection_name}_uri",
kwargs: {embed_depth: 0}
)
end
|
#collection_with_doc ⇒ Object
146
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/shaf/generator/serializer.rb', line 146
def collection_with_doc
<<~EOS.split("\n")
collection of: '#{plural_name}' do
curie(:doc) { doc_curie_uri('#{resource_name}') }
link :self, #{collection_name}_uri
link :up, root_uri
#{create_link.join("\n ")}
end
EOS
end
|
#create_link ⇒ Object
127
128
129
130
131
132
|
# File 'lib/shaf/generator/serializer.rb', line 127
def create_link
link(
rel: "create-form",
uri_helper: "new_#{resource_name}_uri"
)
end
|
#create_policy ⇒ Object
177
178
179
180
|
# File 'lib/shaf/generator/serializer.rb', line 177
def create_policy
policy_args = ["policy", name_arg, *attribute_names]
Generator::Factory.create(*policy_args, **options).call
end
|
#create_profile ⇒ Object
182
183
184
185
|
# File 'lib/shaf/generator/serializer.rb', line 182
def create_profile
profile_args = ["profile", name_arg, *attributes]
Generator::Factory.create(*profile_args, **options).call
end
|
#create_serializer ⇒ Object
46
47
48
49
50
|
# File 'lib/shaf/generator/serializer.rb', line 46
def create_serializer
content = render(template, opts)
content = wrap_in_module(content, module_name)
write_output(target, content)
end
|
#create_serializer_spec ⇒ Object
52
53
54
55
56
|
# File 'lib/shaf/generator/serializer.rb', line 52
def create_serializer_spec
content = render(spec_template, opts)
content = wrap_in_module(content, module_name, search: "describe #{serializer_class_name}")
write_output(spec_target, content)
end
|
#delete_link ⇒ Object
119
120
121
122
123
124
125
|
# File 'lib/shaf/generator/serializer.rb', line 119
def delete_link
link(
rel: "delete",
uri_helper: "#{resource_name}_uri(resource)",
kwargs: {curie: :doc}
)
end
|
#edit_link ⇒ Object
112
113
114
115
116
117
|
# File 'lib/shaf/generator/serializer.rb', line 112
def edit_link
link(
rel: "edit-form",
uri_helper: "edit_#{resource_name}_uri(resource)"
)
end
|
#link(rel:, uri_helper:, kwargs: {}) ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/shaf/generator/serializer.rb', line 134
def link(rel:, uri_helper:, kwargs: {})
kwargs_str = kwargs.inject('') do |s, (k,v)|
"#{s}, #{k}: #{Utils.symbol_or_quoted_string(v)}"
end
<<~EOS.split("\n")
link #{Utils.symbol_string(rel)}#{kwargs_str} do
#{uri_helper}
end
EOS
end
|
#link_relations ⇒ Object
70
71
72
|
# File 'lib/shaf/generator/serializer.rb', line 70
def link_relations
%w(collection self edit-form doc:delete)
end
|
#links_with_doc ⇒ Object
88
89
90
91
92
93
94
95
|
# File 'lib/shaf/generator/serializer.rb', line 88
def links_with_doc
[
collection_link,
self_link,
edit_link,
delete_link,
]
end
|
#opts ⇒ Object
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/shaf/generator/serializer.rb', line 159
def opts
{
name: name,
resource_name: resource_name,
collection_name: collection_name,
class_name: serializer_class_name,
model_class_name: model_class_name,
policy_class_name: policy_class_name,
policy_file: policy_file,
attribute_names: attribute_names,
link_relations: link_relations,
profile_with_doc: profile_with_doc,
attributes_with_doc: attributes_with_doc,
links_with_doc: links_with_doc,
collection_with_doc: collection_with_doc
}
end
|
#policy_class_name ⇒ Object
18
19
20
|
# File 'lib/shaf/generator/serializer.rb', line 18
def policy_class_name
"#{model_class_name}Policy"
end
|
#policy_file ⇒ Object
42
43
44
|
# File 'lib/shaf/generator/serializer.rb', line 42
def policy_file
File.join(['policies', namespace, "#{name}_policy"].compact)
end
|
#profile_with_doc ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/shaf/generator/serializer.rb', line 74
def profile_with_doc
doc = <<~DOC
# Adds a link to the '#{resource_name}' profile and a curie. By default the
# curie prefix is 'doc', use the `curie_prefix` keyword argument to
# change this.
# Note: the target of the profile link and the curie will be set to
# `profile_uri('#{resource_name}')` resp. `doc_curie_uri('#{resource_name}')`. To
# create links for external profiles or curies, delete the next line
# and use `::link` and/or `::curie` instead.
DOC
doc.split("\n") << %Q(profile #{Utils.symbol_string(resource_name)})
end
|
#self_link ⇒ Object
105
106
107
108
109
110
|
# File 'lib/shaf/generator/serializer.rb', line 105
def self_link
link(
rel: "self",
uri_helper: "#{resource_name}_uri(resource)"
)
end
|
#serializer_class_name ⇒ Object
14
15
16
|
# File 'lib/shaf/generator/serializer.rb', line 14
def serializer_class_name
"#{model_class_name}Serializer"
end
|
#spec_target ⇒ Object
38
39
40
|
# File 'lib/shaf/generator/serializer.rb', line 38
def spec_target
target(directory: 'spec/serializers', name: "#{name}_serializer_spec.rb")
end
|
#spec_template ⇒ Object
26
27
28
|
# File 'lib/shaf/generator/serializer.rb', line 26
def spec_template
'spec/serializer_spec.rb'
end
|
#target_dir ⇒ Object
30
31
32
|
# File 'lib/shaf/generator/serializer.rb', line 30
def target_dir
'api/serializers'
end
|
#target_name ⇒ Object
34
35
36
|
# File 'lib/shaf/generator/serializer.rb', line 34
def target_name
"#{name}_serializer.rb"
end
|
#template ⇒ Object
22
23
24
|
# File 'lib/shaf/generator/serializer.rb', line 22
def template
'api/serializer.rb'
end
|