Module: LinkedRails::Serializer::ClassMethods

Defined in:
lib/linked_rails/serializer.rb

Instance Method Summary collapse

Instance Method Details

#anonymous_object?(object) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/linked_rails/serializer.rb', line 32

def anonymous_object?(object)
  object.iri.anonymous?
end

#default_enum_opts(attr) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/linked_rails/serializer.rb', line 60

def default_enum_opts(attr)
  enum_opts = serializable_class.try(:defined_enums).try(:[], attr.to_s)
  return [] if enum_opts.blank?

  HashWithIndifferentAccess[
    enum_opts&.map { |k, _v| [k.to_sym, {}] }
  ]
end

#enum(attr, opts = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/linked_rails/serializer.rb', line 36

def enum(attr, opts = nil)
  self._enums ||= HashWithIndifferentAccess.new
  opts[:type] ||= Vocab.ontola[:FormOption]
  opts[:options] ||= default_enum_opts(attr)
  self._enums[attr] = enum_values(attr, opts)

  attribute(attr, if: opts[:if], predicate: opts[:predicate]) do |object, params|
    block_given? ? yield(object, params) : enum_value(attr, object)
  end
end

#enum_options(key) ⇒ Object



47
48
49
# File 'lib/linked_rails/serializer.rb', line 47

def enum_options(key)
  _enums && _enums[key]
end

#enum_value(key, object) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/linked_rails/serializer.rb', line 51

def enum_value(key, object)
  options = enum_options(key)
  return if options.blank?

  raw_value = object.send(key)

  options[raw_value].try(:iri) if raw_value.present?
end

#has_many(key, **opts) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/linked_rails/serializer.rb', line 79

def has_many(key, **opts)
  opts[:id_method_name] = :iri

  return super if block_given?

  super do |object|
    object.send(key)
  end
end

#has_one(key, **opts) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/linked_rails/serializer.rb', line 69

def has_one(key, **opts)
  opts[:id_method_name] = :iri

  return super if block_given?

  super do |object|
    object.send(key)
  end
end

#named_object?(object) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/linked_rails/serializer.rb', line 89

def named_object?(object)
  !object.iri.anonymous?
end

#never(_object, _params) ⇒ Object



93
94
95
# File 'lib/linked_rails/serializer.rb', line 93

def never(_object, _params)
  false
end

#secret_attribute(key, **opts) ⇒ Object



97
98
99
100
# File 'lib/linked_rails/serializer.rb', line 97

def secret_attribute(key, **opts)
  opts[:if] = method(:never)
  attribute(key, **opts)
end

#serializable_classObject



102
103
104
# File 'lib/linked_rails/serializer.rb', line 102

def serializable_class
  @serializable_class ||= name.gsub('Serializer', '').safe_constantize
end

#serialize_image(obj) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/linked_rails/serializer.rb', line 106

def serialize_image(obj)
  if obj.is_a?(String) || obj.is_a?(Symbol)
    RDF::URI(obj.to_s.gsub(/^fa-/, 'http://fontawesome.io/icon/'))
  else
    obj.presence
  end
end

#with_collection(name, **opts) ⇒ Object

rubocop:disable Metrics/AbcSize



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/linked_rails/serializer.rb', line 114

def with_collection(name, **opts) # rubocop:disable Metrics/AbcSize
  collection_name = "#{name.to_s.singularize}_collection"
  opts[:association] ||= name
  opts[:polymorphic] ||= true
  opts[:if] ||= method(:named_object?)

  collection_opts = {}
  collection_opts[:page_size] = opts.delete(:page_size) if opts.key?(:page_size)
  collection_opts[:display] = opts.delete(:display) if opts.key?(:display)

  has_one collection_name, **opts do |object, params|
    object.send(collection_name, **collection_opts.merge(user_context: params[:scope]))
  end
end