class Shaf::Generator::Serializer
Public Instance Methods
attribute_names()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 63 def attribute_names attributes.map { |arg| arg.split(':').first } end
attributes()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 59 def attributes args[1..-1] end
attributes_with_doc()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 67 def attributes_with_doc attribute_names.map { |attr| ["attribute :#{attr}"] } end
call()
click to toggle source
# 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()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 98 def collection_link link( rel: "collection", uri_helper: "#{plural_name}_uri", kwargs: {embed_depth: 0} ) end
collection_with_doc()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 147 def collection_with_doc <<~EOS.split("\n") collection of: '#{plural_name}' do curie(:doc) { doc_curie_uri('#{name}') } link :self, #{plural_name}_uri link :up, root_uri #{create_link.join("\n ")} end EOS end
create_link()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 128 def create_link link( rel: "create-form", uri_helper: "new_#{name}_uri" ) end
create_policy()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 176 def create_policy policy_args = ["policy", name, *attribute_names] Generator::Factory.create(*policy_args, **options).call end
create_profile()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 181 def create_profile profile_args = ["profile", name, *attributes] Generator::Factory.create(*profile_args, **options).call end
create_serializer()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 49 def create_serializer content = render(template, opts) write_output(target, content) end
create_serializer_spec()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 54 def create_serializer_spec content = render(spec_template, opts) write_output(spec_target, content) end
delete_link()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 120 def delete_link link( rel: "delete", uri_helper: "#{name}_uri(resource)", kwargs: {curie: :doc} ) end
edit_link()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 113 def edit_link link( rel: "edit-form", uri_helper: "edit_#{name}_uri(resource)" ) end
link(rel:, uri_helper:, kwargs: {})
click to toggle source
# File lib/shaf/generator/serializer.rb, line 135 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()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 71 def link_relations %w(collection self edit-form doc:delete) end
links_with_doc()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 89 def links_with_doc [ collection_link, self_link, edit_link, delete_link, ] end
model_class_name()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 25 def model_class_name Utils.model_name(name) end
name()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 14 def name n = args.first || "" return n unless n.empty? raise Command::ArgumentError, "Please provide a model name when using the serializer generator!" end
opts()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 160 def opts { name: name, class_name: "#{model_class_name}Serializer", model_class_name: model_class_name, policy_class_name: policy_class_name, policy_name: "#{name}_policy", 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
plural_name()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 21 def plural_name Utils.pluralize(name) end
policy_class_name()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 29 def policy_class_name "#{model_class_name}Policy" end
profile_with_doc()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 75 def profile_with_doc doc = <<~DOC # Adds a link to the '#{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('#{name}')` resp. `doc_curie_uri('#{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(name)}) end
self_link()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 106 def self_link link( rel: "self", uri_helper: "#{name}_uri(resource)" ) end
spec_target()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 45 def spec_target "spec/serializers/#{name}_serializer_spec.rb" end
spec_template()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 37 def spec_template 'spec/serializer_spec.rb' end
target()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 41 def target "api/serializers/#{name}_serializer.rb" end
template()
click to toggle source
# File lib/shaf/generator/serializer.rb, line 33 def template 'api/serializer.rb' end