Class: Shaf::Generator::Serializer

Inherits:
Base
  • Object
show all
Defined in:
lib/shaf/generator/serializer.rb

Instance Attribute Summary

Attributes inherited from Base

#args

Instance Method Summary collapse

Methods inherited from Base

identifier, inherited, #initialize, #read_template, #render, #template_dir, usage, #write_output

Constructor Details

This class inherits a constructor from Shaf::Generator::Base

Instance Method Details

#attributesObject



59
60
61
# File 'lib/shaf/generator/serializer.rb', line 59

def attributes
  args[1..-1].map { |attr| ":#{attr}" }
end

#attributes_with_docObject



63
64
65
66
67
68
69
70
# File 'lib/shaf/generator/serializer.rb', line 63

def attributes_with_doc
  attributes.map do |attr|
    [
      "# FIXME: Write documentation for attribute #{attr}",
      "attribute #{attr}"
    ]
  end
end

#callObject



7
8
9
10
11
12
13
14
15
# File 'lib/shaf/generator/serializer.rb', line 7

def call
  if name.empty?
    raise "Please provide a model name when using the serializer generator!"
  end

  create_serializer
  create_serializer_spec
  create_policy
end


107
108
109
110
111
112
113
114
115
116
# File 'lib/shaf/generator/serializer.rb', line 107

def collection_link
  link(
    rel: "doc:up",
    desc: "Link to the collection of all #{plural_name}. " \
    "Send a POST request to this uri to create a new #{name}",
    method: "GET or POST",
    uri: "/#{plural_name}",
    uri_helper: "#{plural_name}_uri"
  )
end

#collection_with_docObject



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/shaf/generator/serializer.rb', line 217

def collection_with_doc
  <<~EOS.split("\n")
    collection of: '#{plural_name}' do
      link :self, #{plural_name}_uri
      link :up, root_uri
      curie(:doc) { doc_curie_uri('#{name}') }
    
      embed :'doc:create-form' do
        #{model_class_name}.create_form.tap do |form|
          form.self_link = new_#{name}_uri
          form.href = #{plural_name}_uri
        end
      end
    end
  EOS
end

#create_policyObject



252
253
254
255
# File 'lib/shaf/generator/serializer.rb', line 252

def create_policy
  policy_args = ["policy", name, *args[1..-1]]
  Generator::Factory.create(*policy_args).call
end

#create_serializerObject



49
50
51
52
# File 'lib/shaf/generator/serializer.rb', line 49

def create_serializer
  content = render(template, opts)
  write_output(target, content)
end

#create_serializer_specObject



54
55
56
57
# File 'lib/shaf/generator/serializer.rb', line 54

def create_serializer_spec
  content = render(spec_template, opts)
  write_output(spec_target, content)
end

#curies_with_docObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/shaf/generator/serializer.rb', line 76

def curies_with_doc
  [
    <<~EOS.split("\n")
      # Auto generated doc:  
      # Link to the documentation for a given relation of the #{name} resource.
      # This link is templated, which means that {rel} must be replaced by the
      # appropriate relation name.  
      # Method: GET  
      # Example:
      # ```
      # curl -H "Accept: application/json" \\
      #      /doc/#{name}/rels/edit
      #```
      curie :doc do
        doc_curie_uri('#{name}')
      end
    EOS
  ]
end


155
156
157
158
159
160
161
162
163
# File 'lib/shaf/generator/serializer.rb', line 155

def delete_link
  link(
    rel: "doc:delete",
    desc: "Link to delete this #{name}",
    method: "DELETE",
    uri: "/#{plural_name}/5",
    uri_helper: "#{name}_uri(resource)"
  )
end


136
137
138
139
140
141
142
143
# File 'lib/shaf/generator/serializer.rb', line 136

def edit_link
  link(
    rel: "doc:edit-form",
    desc: "Link to a form to edit this resource",
    uri: "/#{plural_name}/5/edit",
    uri_helper: "edit_#{name}_uri(resource)"
  )
end

#embedsObject



198
199
200
# File 'lib/shaf/generator/serializer.rb', line 198

def embeds
  [:'doc:edit-form']
end

#embeds_with_docObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/shaf/generator/serializer.rb', line 202

def embeds_with_doc
  [
    <<~EOS.split("\n")
      # Auto generated doc:  
      # A form to edit this #{name}
      embed :'doc:edit-form' do
        resource.edit_form.tap do |form|
          form.self_link = edit_#{name}_uri(resource)
          form.href = #{name}_uri(resource)
        end
      end
    EOS
  ]
end

#example(method, uri) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/shaf/generator/serializer.rb', line 177

def example(method, uri)
  method_args = ""
  case method
  when "POST"
    method_args = "\n#      -d@payload \\"
  when "PUT"
    method_args = "\n#      -X PUT -d@payload \\"
  when "DELETE"
    method_args = "\n#      -X DELETE \\"
  end

  <<~EOS.chomp
    # Example:
    # ```
    # curl -H "Accept: application/json" \\
    #      -H "Authorization: abcdef" \\#{method_args}
    #      #{uri}
    #```
  EOS
end


165
166
167
168
169
170
171
172
173
174
175
# File 'lib/shaf/generator/serializer.rb', line 165

def link(rel:, method: "GET", desc:, uri:, uri_helper:)
  <<~EOS.split("\n")
    # Auto generated doc:  
    # #{desc}.  
    # Method: #{method}  
    #{example(method, uri)}
    link :'#{rel}' do
      #{uri_helper}
    end
  EOS
end


72
73
74
# File 'lib/shaf/generator/serializer.rb', line 72

def links
  %w(doc:up self doc:create-form doc:edit-form doc:edit doc:delete)
end


96
97
98
99
100
101
102
103
104
105
# File 'lib/shaf/generator/serializer.rb', line 96

def links_with_doc
  [
    collection_link,
    self_link,
    new_link,
    edit_link,
    update_link,
    delete_link,
  ]
end

#model_class_nameObject



25
26
27
# File 'lib/shaf/generator/serializer.rb', line 25

def model_class_name
  "::#{name.capitalize}"
end

#nameObject



17
18
19
# File 'lib/shaf/generator/serializer.rb', line 17

def name
  args.first || ""
end


127
128
129
130
131
132
133
134
# File 'lib/shaf/generator/serializer.rb', line 127

def new_link
  link(
    rel: "doc:create-form",
    desc: "Link to a form to create a new #{name}",
    uri: "/#{plural_name}/form",
    uri_helper: "new_#{name}_uri"
  )
end

#optsObject



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/shaf/generator/serializer.rb', line 234

def opts
  {
    name: name,
    class_name: "#{name.capitalize}Serializer",
    model_class_name: model_class_name,
    policy_class_name: policy_class_name,
    policy_name: "#{name}_policy",
    attributes: attributes,
    links: links,
    embeds: embeds,
    attributes_with_doc: attributes_with_doc,
    curies_with_doc: curies_with_doc,
    links_with_doc: links_with_doc,
    embeds_with_doc: embeds_with_doc,
    collection_with_doc: collection_with_doc,
  }
end

#plural_nameObject



21
22
23
# File 'lib/shaf/generator/serializer.rb', line 21

def plural_name
  Utils.pluralize(name)
end

#policy_class_nameObject



29
30
31
# File 'lib/shaf/generator/serializer.rb', line 29

def policy_class_name
  "::#{name.capitalize}Policy"
end


118
119
120
121
122
123
124
125
# File 'lib/shaf/generator/serializer.rb', line 118

def self_link
  link(
    rel: "self",
    desc: "Link to this #{name}",
    uri: "/#{plural_name}/5",
    uri_helper: "#{name}_uri(resource)"
  )
end

#spec_targetObject



45
46
47
# File 'lib/shaf/generator/serializer.rb', line 45

def spec_target
  "spec/serializers/#{name}_serializer_spec.rb"
end

#spec_templateObject



37
38
39
# File 'lib/shaf/generator/serializer.rb', line 37

def spec_template
  'spec/serializer_spec.rb'
end

#targetObject



41
42
43
# File 'lib/shaf/generator/serializer.rb', line 41

def target
  "api/serializers/#{name}_serializer.rb"
end

#templateObject



33
34
35
# File 'lib/shaf/generator/serializer.rb', line 33

def template
  'api/serializer.rb'
end


145
146
147
148
149
150
151
152
153
# File 'lib/shaf/generator/serializer.rb', line 145

def update_link
  link(
    rel: "doc:edit",
    desc: "Link to update this #{name}",
    method: "PUT",
    uri: "/#{plural_name}/5",
    uri_helper: "#{name}_uri(resource)"
  )
end