Class: Valkyrie::Resource
- Inherits:
-
Dry::Struct
- Object
- Dry::Struct
- Valkyrie::Resource
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.
Defined Under Namespace
Modules: AccessControls
Classes: ReservedAttributeError
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.allow_nonexistent_keys ⇒ Object
Allows a Valkyrie::Resource to be instantiated without providing every available key, and makes sure the defaults are set up if no value is given.
21
22
23
|
# File 'lib/valkyrie/resource.rb', line 21
def self.allow_nonexistent_keys
transform_types(&:omittable)
end
|
.attribute(name, type = Valkyrie::Types::Set.optional, internal: false) ⇒ Object
Note:
Overridden from Dry::Struct to make the default type Types::Set
Define an attribute. Attributes are used to describe resources.
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/valkyrie/resource.rb', line 47
def self.attribute(name, type = Valkyrie::Types::Set.optional, internal: false)
raise ReservedAttributeError, "#{name} is a reserved attribute and defined by Valkyrie::Resource, do not redefine it." if reserved_attributes.include?(name.to_sym) &&
attribute_names.include?(name.to_sym) &&
!internal
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_locking ⇒ Object
.fields ⇒ Array<Symbol>
Returns Array of fields defined for this class.
38
39
40
|
# File 'lib/valkyrie/resource.rb', line 38
def self.fields
attribute_names.without(:new_record)
end
|
.human_readable_type ⇒ Object
70
71
72
|
# File 'lib/valkyrie/resource.rb', line 70
def self.human_readable_type
@_human_readable_type ||= name.demodulize.titleize
end
|
.human_readable_type=(val) ⇒ Object
74
75
76
|
# File 'lib/valkyrie/resource.rb', line 74
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.
27
28
29
30
31
32
33
34
35
|
# File 'lib/valkyrie/resource.rb', line 27
def self.inherited(subclass)
super(subclass)
subclass.allow_nonexistent_keys
subclass.attribute :id, Valkyrie::Types::ID.optional, internal: true
subclass.attribute :internal_resource, Valkyrie::Types::Any.default(subclass.to_s.freeze), 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_name ⇒ ActiveModel::Name
Note:
Added for ActiveModel compatibility.
64
65
66
|
# File 'lib/valkyrie/resource.rb', line 64
def self.model_name
@model_name ||= ::ActiveModel::Name.new(self)
end
|
.optimistic_locking_enabled? ⇒ Boolean
.reserved_attributes ⇒ Object
58
59
60
|
# File 'lib/valkyrie/resource.rb', line 58
def self.reserved_attributes
[:id, :internal_resource, :created_at, :updated_at, :new_record]
end
|
Instance Method Details
#[](name) ⇒ Object
Return an attribute’s value.
148
149
150
151
152
|
# File 'lib/valkyrie/resource.rb', line 148
def [](name)
super(name.to_sym)
rescue Dry::Struct::MissingAttributeError
nil
end
|
#__attributes__ ⇒ Object
94
95
96
|
# File 'lib/valkyrie/resource.rb', line 94
def __attributes__
Hash[@attributes].freeze
end
|
#attributes ⇒ Object
90
91
92
|
# File 'lib/valkyrie/resource.rb', line 90
def attributes
Hash[self.class.attribute_names.map { |x| [x, nil] }].merge(super).freeze
end
|
#column_for_attribute(name) ⇒ Symbol
Note:
Added for ActiveModel compatibility.
111
112
113
|
# File 'lib/valkyrie/resource.rb', line 111
def column_for_attribute(name)
name
end
|
#dup ⇒ Object
98
99
100
|
# File 'lib/valkyrie/resource.rb', line 98
def dup
new({})
end
|
#has_attribute?(name) ⇒ Boolean
104
105
106
|
# File 'lib/valkyrie/resource.rb', line 104
def has_attribute?(name)
respond_to?(name)
end
|
#human_readable_type ⇒ String
Provide a human readable name for the resource
141
142
143
|
# File 'lib/valkyrie/resource.rb', line 141
def human_readable_type
self.class.human_readable_type
end
|
#optimistic_locking_enabled? ⇒ Boolean
86
87
88
|
# File 'lib/valkyrie/resource.rb', line 86
def optimistic_locking_enabled?
self.class.optimistic_locking_enabled?
end
|
#ordered_attribute?(key) ⇒ Boolean
Returns if an attribute is set as ordered.
163
164
165
|
# File 'lib/valkyrie/resource.rb', line 163
def ordered_attribute?(key)
self.class.schema.key(key).type.meta.try(:[], :ordered)
end
|
#persisted? ⇒ Boolean
116
117
118
|
# File 'lib/valkyrie/resource.rb', line 116
def persisted?
new_record == false
end
|
#set_value(key, value) ⇒ Object
Set an attribute’s value.
158
159
160
|
# File 'lib/valkyrie/resource.rb', line 158
def set_value(key, value)
@attributes[key.to_sym] = self.class.schema.key(key.to_sym).type.call(value)
end
|
#to_key ⇒ Object
120
121
122
|
# File 'lib/valkyrie/resource.rb', line 120
def to_key
[id]
end
|
#to_model ⇒ Object
Note:
Added for ActiveModel compatibility
129
130
131
|
# File 'lib/valkyrie/resource.rb', line 129
def to_model
self
end
|
#to_param ⇒ Object
124
125
126
|
# File 'lib/valkyrie/resource.rb', line 124
def to_param
to_key.map(&:to_s).join('-')
end
|
#to_s ⇒ String
134
135
136
|
# File 'lib/valkyrie/resource.rb', line 134
def to_s
"#{self.class}: #{id}"
end
|