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 :id, Valkyrie::Types::ID.optional
  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) ⇒ Object

Note:

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

Define an attribute.

Parameters:

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


42
43
44
45
46
47
# File 'lib/valkyrie/resource.rb', line 42

def self.attribute(name, type = Valkyrie::Types::Set.optional)
  define_method("#{name}=") do |value|
    instance_variable_set("@#{name}", self.class.schema[name].call(value))
  end
  super
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



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

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

.human_readable_type=(val) ⇒ Object



61
62
63
# File 'lib/valkyrie/resource.rb', line 61

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.



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

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

.model_nameActiveModel::Name

Note:

Added for ActiveModel compatibility.

Returns:

  • (ActiveModel::Name)


51
52
53
# File 'lib/valkyrie/resource.rb', line 51

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

Instance Method Details

#attributesHash

Returns Hash of attributes.

Returns:

  • (Hash)

    Hash of attributes



66
67
68
# File 'lib/valkyrie/resource.rb', line 66

def attributes
  to_h
end

#column_for_attribute(name) ⇒ Symbol

Note:

Added for ActiveModel compatibility.

Parameters:

  • name (Symbol)

Returns:

  • (Symbol)


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

def column_for_attribute(name)
  name
end

#has_attribute?(name) ⇒ Boolean

Parameters:

  • name (Symbol)

    Attribute name

Returns:

  • (Boolean)


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

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

#human_readable_typeString

Provide a human readable name for the resource

Returns:

  • (String)


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

def human_readable_type
  self.class.human_readable_type
end

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  @new_record == false
end

#to_keyObject



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

def to_key
  [id]
end

#to_modelObject

Note:

Added for ActiveModel compatibility



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

def to_model
  self
end

#to_paramObject



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

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

#to_sString

Returns:

  • (String)


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

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