Class: JPie::Resource
- Inherits:
-
Object
- Object
- JPie::Resource
- Includes:
- ActiveSupport::Configurable, Attributable, Inferrable, Sortable
- Defined in:
- lib/jpie/resource.rb,
lib/jpie/resource/sortable.rb,
lib/jpie/resource/inferrable.rb,
lib/jpie/resource/attributable.rb
Defined Under Namespace
Modules: Attributable, Inferrable, Sortable
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Class Method Summary collapse
- .inherited(subclass) ⇒ Object
-
.scope(_context = {}) ⇒ Object
Default scope method that returns all records Override this in your resource classes to provide authorization scoping Example: def self.scope(context) current_user = context return model.none unless current_user.
-
.supported_includes ⇒ Object
Return supported include paths for validation Override this method to customize supported includes.
-
.supported_sort_fields ⇒ Object
Return supported sort fields for validation Override this method to customize supported sort fields.
Instance Method Summary collapse
- #attributes_hash ⇒ Object
-
#initialize(object, context = {}) ⇒ Resource
constructor
A new instance of Resource.
- #meta_hash ⇒ Object
Constructor Details
#initialize(object, context = {}) ⇒ Resource
Returns a new instance of Resource.
84 85 86 87 |
# File 'lib/jpie/resource.rb', line 84 def initialize(object, context = {}) @object = object @context = context end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name) ⇒ Object (private)
135 136 137 138 139 140 141 |
# File 'lib/jpie/resource.rb', line 135 def method_missing(method_name, *, &) if @object.respond_to?(method_name) @object.public_send(method_name, *, &) else super end end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
82 83 84 |
# File 'lib/jpie/resource.rb', line 82 def context @context end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
82 83 84 |
# File 'lib/jpie/resource.rb', line 82 def object @object end |
Class Method Details
.inherited(subclass) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/jpie/resource.rb', line 16 def inherited(subclass) super subclass._attributes = _attributes.dup subclass._relationships = _relationships.dup subclass. = .dup subclass._sortable_fields = _sortable_fields.dup end |
.scope(_context = {}) ⇒ Object
Default scope method that returns all records Override this in your resource classes to provide authorization scoping Example:
def self.scope(context)
current_user = context[:current_user]
return model.none unless current_user
if current_user.admin?
model.all
else
model.where(user: current_user)
end
end
37 38 39 |
# File 'lib/jpie/resource.rb', line 37 def scope(_context = {}) model.all end |
.supported_includes ⇒ Object
Return supported include paths for validation Override this method to customize supported includes
43 44 45 46 47 48 49 |
# File 'lib/jpie/resource.rb', line 43 def supported_includes # Return relationship names as supported includes by default _relationships.keys.map(&:to_s) # Convert to nested hash format for complex includes # For simple includes, return array format end |
.supported_sort_fields ⇒ Object
Return supported sort fields for validation Override this method to customize supported sort fields
53 54 55 56 57 |
# File 'lib/jpie/resource.rb', line 53 def supported_sort_fields base_fields = extract_base_sort_fields = (base_fields + ).uniq end |
Instance Method Details
#attributes_hash ⇒ Object
93 94 95 96 97 |
# File 'lib/jpie/resource.rb', line 93 def attributes_hash self.class._attributes.index_with do send(it) end end |
#meta_hash ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/jpie/resource.rb', line 99 def # Start with meta attributes from the macro = self.class..index_with do send(it) end # Check if the resource defines a custom meta method if respond_to?(:meta, true) && method(:meta).owner != JPie::Resource = # Validate that meta method returns a hash unless .is_a?(Hash) raise JPie::Errors::ResourceError.new( detail: "meta method must return a Hash, got #{.class}" ) end # Merge custom meta with base meta (custom meta takes precedence) .merge() else end end |