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

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)


43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/valkyrie/resource.rb', line 43

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|
    instance_variable_set("@#{name}", self.class.schema[name].call(value))
  end
  type = type.meta(ordered: true) if name == :member_ids
  super(name, type)
end

.enable_optimistic_lockingObject



77
78
79
# File 'lib/valkyrie/resource.rb', line 77

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



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

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

.human_readable_type=(val) ⇒ Object



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

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)


63
64
65
# File 'lib/valkyrie/resource.rb', line 63

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

.optimistic_locking_enabled?Boolean

Returns:

  • (Boolean)


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

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

.reserved_attributesObject



57
58
59
# File 'lib/valkyrie/resource.rb', line 57

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

Instance Method Details

#attributesHash

Returns Hash of attributes.

Returns:

  • (Hash)

    Hash of attributes



90
91
92
# File 'lib/valkyrie/resource.rb', line 90

def attributes
  to_h
end

#column_for_attribute(name) ⇒ Symbol

Note:

Added for ActiveModel compatibility.

Parameters:

  • name (Symbol)

Returns:

  • (Symbol)


103
104
105
# File 'lib/valkyrie/resource.rb', line 103

def column_for_attribute(name)
  name
end

#has_attribute?(name) ⇒ Boolean

Parameters:

  • name (Symbol)

    Attribute name

Returns:

  • (Boolean)


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

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

#human_readable_typeString

Provide a human readable name for the resource

Returns:

  • (String)


133
134
135
# File 'lib/valkyrie/resource.rb', line 133

def human_readable_type
  self.class.human_readable_type
end

#optimistic_locking_enabled?Boolean

Returns:

  • (Boolean)


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

def optimistic_locking_enabled?
  self.class.optimistic_locking_enabled?
end

#persisted?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/valkyrie/resource.rb', line 108

def persisted?
  @new_record == false
end

#to_keyObject



112
113
114
# File 'lib/valkyrie/resource.rb', line 112

def to_key
  [id]
end

#to_modelObject

Note:

Added for ActiveModel compatibility



121
122
123
# File 'lib/valkyrie/resource.rb', line 121

def to_model
  self
end

#to_paramObject



116
117
118
# File 'lib/valkyrie/resource.rb', line 116

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

#to_sString

Returns:

  • (String)


126
127
128
# File 'lib/valkyrie/resource.rb', line 126

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