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_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_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
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
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