Class: Valkyrie::Resource
- Inherits:
-
Dry::Struct
- Object
- Dry::Struct
- Valkyrie::Resource
- 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.
Direct Known Subclasses
Defined Under Namespace
Modules: AccessControls Classes: DeprecatedHashWrite
Class Method Summary collapse
-
.attribute(name, type = Valkyrie::Types::Set.optional, internal: false) ⇒ Object
Define an attribute.
- .enable_optimistic_locking ⇒ Object
-
.fields ⇒ Array<Symbol>
Array of fields defined for this class.
- .human_readable_type ⇒ Object
- .human_readable_type=(val) ⇒ Object
-
.inherited(subclass) ⇒ Object
Overridden to provide default attributes.
- .model_name ⇒ ActiveModel::Name
- .new(attributes = default_attributes) ⇒ Object
- .optimistic_locking_enabled? ⇒ Boolean
- .reserved_attributes ⇒ Object
Instance Method Summary collapse
-
#[](name) ⇒ Object
Return an attribute’s value.
-
#attributes ⇒ Hash
Hash of attributes.
- #column_for_attribute(name) ⇒ Symbol
- #has_attribute?(name) ⇒ Boolean
-
#human_readable_type ⇒ String
Provide a human readable name for the resource.
- #optimistic_locking_enabled? ⇒ Boolean
- #persisted? ⇒ Boolean
-
#set_value(key, value) ⇒ Object
Set an attribute’s value.
- #to_key ⇒ Object
- #to_model ⇒ Object
- #to_param ⇒ Object
- #to_s ⇒ String
Class Method Details
.attribute(name, type = Valkyrie::Types::Set.optional, internal: false) ⇒ Object
Remove ability to override built in attributes.
Overridden from Dry::Struct to make the default type Types::Set
Define an attribute. Attributes are used to describe resources.
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.(ordered: true) if name == :member_ids super(name, type) end |
.enable_optimistic_locking ⇒ Object
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 |
.fields ⇒ Array<Symbol>
Returns 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_type ⇒ Object
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
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_name ⇒ ActiveModel::Name
Added for ActiveModel compatibility.
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
92 93 94 |
# File 'lib/valkyrie/resource.rb', line 92 def self.optimistic_locking_enabled? schema.key?(Valkyrie::Persistence::Attributes::OPTIMISTIC_LOCK) end |
.reserved_attributes ⇒ Object
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.
197 198 199 200 201 |
# File 'lib/valkyrie/resource.rb', line 197 def [](name) super(name.to_sym) rescue NoMethodError nil end |
#attributes ⇒ Hash
Returns 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
Added for ActiveModel compatibility.
160 161 162 |
# File 'lib/valkyrie/resource.rb', line 160 def column_for_attribute(name) name end |
#has_attribute?(name) ⇒ Boolean
153 154 155 |
# File 'lib/valkyrie/resource.rb', line 153 def has_attribute?(name) respond_to?(name) end |
#human_readable_type ⇒ String
Provide a human readable name for the resource
190 191 192 |
# File 'lib/valkyrie/resource.rb', line 190 def human_readable_type self.class.human_readable_type end |
#optimistic_locking_enabled? ⇒ Boolean
96 97 98 |
# File 'lib/valkyrie/resource.rb', line 96 def optimistic_locking_enabled? self.class.optimistic_locking_enabled? end |
#persisted? ⇒ 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.
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_key ⇒ Object
169 170 171 |
# File 'lib/valkyrie/resource.rb', line 169 def to_key [id] end |
#to_model ⇒ Object
Added for ActiveModel compatibility
178 179 180 |
# File 'lib/valkyrie/resource.rb', line 178 def to_model self end |
#to_param ⇒ Object
173 174 175 |
# File 'lib/valkyrie/resource.rb', line 173 def to_param to_key.map(&:to_s).join('-') end |
#to_s ⇒ String
183 184 185 |
# File 'lib/valkyrie/resource.rb', line 183 def to_s "#{self.class}: #{id}" end |