Class: Valkyrie::Resource

Inherits:
Dry::Struct
  • Object
show all
Includes:
Draper::Decoratable
Defined in:
lib/valkyrie/resource.rb,
lib/valkyrie/resource/access_controls.rb

Overview

The base resource class for all Valkyrie metadata objects.

Examples:

Define a resource

class Book < Valkyrie::Resource
  attribute :member_ids, Valkyrie::Types::Array
  attribute :author
end

See Also:

Defined Under Namespace

Modules: AccessControls Classes: DeprecatedHashWrite

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attribute(name, type = Valkyrie::Types::Set.optional, internal: false) ⇒ Object

TODO:

Remove ability to override built in attributes.

Note:

Overridden from Dry::Struct to make the default type Types::Set

Define an attribute. Attributes are used to describe resources.

Parameters:

  • name (Symbol)
  • type (Dry::Types::Type) (defaults to: Valkyrie::Types::Set.optional)


54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/valkyrie/resource.rb', line 54

def self.attribute(name, type = Valkyrie::Types::Set.optional, internal: false)
  if reserved_attributes.include?(name.to_sym) && schema[name] && !internal
    warn "#{name} is a reserved attribute in Valkyrie::Resource and defined by it. You can remove your definition of `attribute :#{name}`. " \
         "For now your version will be used, but in the next major version the type will be overridden. " \
         "Called from #{Gem.location_of_caller.join(':')}"
    schema.delete(name)
  end
  define_method("#{name}=") do |value|
    set_value(name, value)
  end
  type = type.meta(ordered: true) if name == :member_ids
  super(name, type)
end

.enable_optimistic_lockingObject



88
89
90
# File 'lib/valkyrie/resource.rb', line 88

def self.enable_optimistic_locking
  attribute(Valkyrie::Persistence::Attributes::OPTIMISTIC_LOCK, Valkyrie::Types::Set.of(Valkyrie::Types::OptimisticLockToken))
end

.fieldsArray<Symbol>

Returns Array of fields defined for this class.

Returns:

  • (Array<Symbol>)

    Array of fields defined for this class.



33
34
35
# File 'lib/valkyrie/resource.rb', line 33

def self.fields
  schema.keys.without(:new_record)
end

.human_readable_typeObject



80
81
82
# File 'lib/valkyrie/resource.rb', line 80

def self.human_readable_type
  @_human_readable_type ||= name.demodulize.titleize
end

.human_readable_type=(val) ⇒ Object



84
85
86
# File 'lib/valkyrie/resource.rb', line 84

def self.human_readable_type=(val)
  @_human_readable_type = val
end

.inherited(subclass) ⇒ Object

Note:

The current theory is that we should use this sparingly.

Overridden to provide default attributes.



22
23
24
25
26
27
28
29
30
# File 'lib/valkyrie/resource.rb', line 22

def self.inherited(subclass)
  super(subclass)
  subclass.constructor_type :schema
  subclass.attribute :id, Valkyrie::Types::ID.optional, internal: true
  subclass.attribute :internal_resource, Valkyrie::Types::Any.default(subclass.to_s), internal: true
  subclass.attribute :created_at, Valkyrie::Types::DateTime.optional, internal: true
  subclass.attribute :updated_at, Valkyrie::Types::DateTime.optional, internal: true
  subclass.attribute :new_record, Types::Bool.default(true), internal: true
end

.model_nameActiveModel::Name

Note:

Added for ActiveModel compatibility.

Returns:

  • (ActiveModel::Name)


74
75
76
# File 'lib/valkyrie/resource.rb', line 74

def self.model_name
  @model_name ||= ::ActiveModel::Name.new(self)
end

.new(attributes = default_attributes) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/valkyrie/resource.rb', line 37

def self.new(attributes = default_attributes)
  if attributes.is_a?(Hash) && attributes.keys.map(&:class).uniq.include?(String)
    warn "[DEPRECATION] Instantiating a Valkyrie::Resource with strings as keys has " \
         "been deprecated and will be removed in the next major release. " \
         "Please use symbols instead." \
         "Called from #{Gem.location_of_caller.join(':')}"
    attributes = attributes.symbolize_keys
  end
  super
end

.optimistic_locking_enabled?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/valkyrie/resource.rb', line 92

def self.optimistic_locking_enabled?
  schema.key?(Valkyrie::Persistence::Attributes::OPTIMISTIC_LOCK)
end

.reserved_attributesObject



68
69
70
# File 'lib/valkyrie/resource.rb', line 68

def self.reserved_attributes
  [:id, :internal_resource, :created_at, :updated_at, :new_record]
end

Instance Method Details

#[](name) ⇒ Object

Return an attribute’s value.

Parameters:

  • name (#to_sym)

    the name of the attribute to read



197
198
199
200
201
# File 'lib/valkyrie/resource.rb', line 197

def [](name)
  super(name.to_sym)
rescue NoMethodError
  nil
end

#attributesHash

Returns Hash of attributes.

Returns:

  • (Hash)

    Hash of attributes



147
148
149
# File 'lib/valkyrie/resource.rb', line 147

def attributes
  DeprecatedHashWrite.new.merge(to_h).soft_freeze!
end

#column_for_attribute(name) ⇒ Symbol

Note:

Added for ActiveModel compatibility.

Parameters:

  • name (Symbol)

Returns:

  • (Symbol)


160
161
162
# File 'lib/valkyrie/resource.rb', line 160

def column_for_attribute(name)
  name
end

#has_attribute?(name) ⇒ Boolean

Parameters:

  • name (Symbol)

    Attribute name

Returns:

  • (Boolean)


153
154
155
# File 'lib/valkyrie/resource.rb', line 153

def has_attribute?(name)
  respond_to?(name)
end

#human_readable_typeString

Provide a human readable name for the resource

Returns:

  • (String)


190
191
192
# File 'lib/valkyrie/resource.rb', line 190

def human_readable_type
  self.class.human_readable_type
end

#optimistic_locking_enabled?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/valkyrie/resource.rb', line 96

def optimistic_locking_enabled?
  self.class.optimistic_locking_enabled?
end

#persisted?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/valkyrie/resource.rb', line 165

def persisted?
  new_record == false
end

#set_value(key, value) ⇒ Object

Set an attribute’s value.

Parameters:

  • key (#to_sym)

    the name of the attribute to set

  • value

    the value to set key to.



207
208
209
# File 'lib/valkyrie/resource.rb', line 207

def set_value(key, value)
  instance_variable_set(:"@#{key}", self.class.schema[key.to_sym].call(value))
end

#to_keyObject



169
170
171
# File 'lib/valkyrie/resource.rb', line 169

def to_key
  [id]
end

#to_modelObject

Note:

Added for ActiveModel compatibility



178
179
180
# File 'lib/valkyrie/resource.rb', line 178

def to_model
  self
end

#to_paramObject



173
174
175
# File 'lib/valkyrie/resource.rb', line 173

def to_param
  to_key.map(&:to_s).join('-')
end

#to_sString

Returns:

  • (String)


183
184
185
# File 'lib/valkyrie/resource.rb', line 183

def to_s
  "#{self.class}: #{id}"
end