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
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
- .optimistic_locking_enabled? ⇒ Boolean
- .reserved_attributes ⇒ Object
Instance Method Summary collapse
-
#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
- #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.
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.(ordered: true) if name == :member_ids super(name, type) end |
.enable_optimistic_locking ⇒ Object
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 |
.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
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
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.
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
81 82 83 |
# File 'lib/valkyrie/resource.rb', line 81 def self.optimistic_locking_enabled? schema.key?(Valkyrie::Persistence::Attributes::OPTIMISTIC_LOCK) end |
.reserved_attributes ⇒ Object
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
#attributes ⇒ Hash
Returns Hash of attributes.
90 91 92 |
# File 'lib/valkyrie/resource.rb', line 90 def attributes to_h end |
#column_for_attribute(name) ⇒ Symbol
Added for ActiveModel compatibility.
103 104 105 |
# File 'lib/valkyrie/resource.rb', line 103 def column_for_attribute(name) name end |
#has_attribute?(name) ⇒ Boolean
96 97 98 |
# File 'lib/valkyrie/resource.rb', line 96 def has_attribute?(name) respond_to?(name) end |
#human_readable_type ⇒ String
Provide a human readable name for the resource
133 134 135 |
# File 'lib/valkyrie/resource.rb', line 133 def human_readable_type self.class.human_readable_type end |
#optimistic_locking_enabled? ⇒ Boolean
85 86 87 |
# File 'lib/valkyrie/resource.rb', line 85 def optimistic_locking_enabled? self.class.optimistic_locking_enabled? end |
#persisted? ⇒ Boolean
108 109 110 |
# File 'lib/valkyrie/resource.rb', line 108 def persisted? @new_record == false end |
#to_key ⇒ Object
112 113 114 |
# File 'lib/valkyrie/resource.rb', line 112 def to_key [id] end |
#to_model ⇒ Object
Added for ActiveModel compatibility
121 122 123 |
# File 'lib/valkyrie/resource.rb', line 121 def to_model self end |
#to_param ⇒ Object
116 117 118 |
# File 'lib/valkyrie/resource.rb', line 116 def to_param to_key.map(&:to_s).join('-') end |
#to_s ⇒ String
126 127 128 |
# File 'lib/valkyrie/resource.rb', line 126 def to_s "#{self.class}: #{id}" end |