Class: Shaf::Generator::Serializer

Inherits:
Base
  • Object
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

Constructor Details

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

Instance Method Details

#attribute_namesObject



62
63
64
# File 'lib/shaf/generator/serializer.rb', line 62

def attribute_names
  attributes.map { |arg| arg.split(':').first }
end

#attributesObject



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

def attributes
  args[1..-1]
end

#attributes_with_docObject



66
67
68
# File 'lib/shaf/generator/serializer.rb', line 66

def attributes_with_doc
  attribute_names.map { |attr| ["attribute :#{attr}"] }
end

#callObject



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


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_docObject



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


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_policyObject



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_profileObject



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_serializerObject



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_specObject



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


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


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


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


70
71
72
# File 'lib/shaf/generator/serializer.rb', line 70

def link_relations
  %w(collection self edit-form doc:delete)
end


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

#optsObject



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_nameObject



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

def policy_class_name
  "#{model_class_name}Policy"
end

#policy_fileObject



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

def policy_file
  File.join(['policies', namespace, "#{name}_policy"].compact)
end

#profile_with_docObject



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


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_nameObject



14
15
16
# File 'lib/shaf/generator/serializer.rb', line 14

def serializer_class_name
  "#{model_class_name}Serializer"
end

#spec_targetObject



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_templateObject



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

def spec_template
  'spec/serializer_spec.rb'
end

#target_dirObject



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

def target_dir
  'api/serializers'
end

#target_nameObject



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

def target_name
  "#{name}_serializer.rb"
end

#templateObject



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

def template
  'api/serializer.rb'
end