Class: JPie::Resource

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#contextObject (readonly)

Returns the value of attribute context.



82
83
84
# File 'lib/jpie/resource.rb', line 82

def context
  @context
end

#objectObject (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._meta_attributes = _meta_attributes.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_includesObject

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_fieldsObject

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
  timestamp_fields = extract_timestamp_fields
  (base_fields + timestamp_fields).uniq
end

Instance Method Details

#attributes_hashObject



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_hashObject



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 meta_hash
  # Start with meta attributes from the macro
  base_meta = self.class._meta_attributes.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
    custom_meta = meta

    # Validate that meta method returns a hash
    unless custom_meta.is_a?(Hash)
      raise JPie::Errors::ResourceError.new(
        detail: "meta method must return a Hash, got #{custom_meta.class}"
      )
    end

    # Merge custom meta with base meta (custom meta takes precedence)
    base_meta.merge(custom_meta)
  else
    base_meta
  end
end