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.
Defined Under Namespace
Modules: AccessControls
Class Method Summary collapse
-
.attribute(name, type = Valkyrie::Types::Set.optional) ⇒ Object
Define an attribute.
-
.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
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.
- #persisted? ⇒ Boolean
- #to_key ⇒ Object
- #to_model ⇒ Object
- #to_param ⇒ Object
- #to_s ⇒ String
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.
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 |
.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
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_name ⇒ ActiveModel::Name
Note:
Added for ActiveModel compatibility.
51 52 53 |
# File 'lib/valkyrie/resource.rb', line 51 def self.model_name @model_name ||= ::ActiveModel::Name.new(self) end |
Instance Method Details
#attributes ⇒ Hash
Returns 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.
79 80 81 |
# File 'lib/valkyrie/resource.rb', line 79 def column_for_attribute(name) name end |
#has_attribute?(name) ⇒ Boolean
72 73 74 |
# File 'lib/valkyrie/resource.rb', line 72 def has_attribute?(name) respond_to?(name) end |
#human_readable_type ⇒ String
Provide a human readable name for the resource
109 110 111 |
# File 'lib/valkyrie/resource.rb', line 109 def human_readable_type self.class.human_readable_type end |
#persisted? ⇒ Boolean
84 85 86 |
# File 'lib/valkyrie/resource.rb', line 84 def persisted? @new_record == false end |
#to_key ⇒ Object
88 89 90 |
# File 'lib/valkyrie/resource.rb', line 88 def to_key [id] end |
#to_model ⇒ Object
Note:
Added for ActiveModel compatibility
97 98 99 |
# File 'lib/valkyrie/resource.rb', line 97 def to_model self end |
#to_param ⇒ Object
92 93 94 |
# File 'lib/valkyrie/resource.rb', line 92 def to_param to_key.map(&:to_s).join('-') end |
#to_s ⇒ String
102 103 104 |
# File 'lib/valkyrie/resource.rb', line 102 def to_s "#{self.class}: #{id}" end |