Class: Tito::Resource

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::API, ActiveModel::Attributes, ActiveModel::Dirty
Defined in:
lib/tito/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_scope: nil, **attrs) ⇒ Resource

Returns a new instance of Resource.



94
95
96
97
98
99
100
101
102
# File 'lib/tito/resource.rb', line 94

def initialize(_scope: nil, **attrs)
  @_scope = _scope
  @_persisted = !!(attrs[:id] || attrs[:slug] || attrs["id"] || attrs["slug"])
  # Drop unknown attributes
  known = self.class.attribute_names_set
  allowed = attrs.select { |k, _| known.include?(k.to_s) }
  super(**allowed)
  clear_changes_information if @_persisted
end

Instance Attribute Details

#_scopeObject (readonly)

Returns the value of attribute _scope.



92
93
94
# File 'lib/tito/resource.rb', line 92

def _scope
  @_scope
end

Class Method Details

.account_scoped!Object



44
45
46
# File 'lib/tito/resource.rb', line 44

def 
  @_event_scoped = false
end

.action(name, method: :post, path:) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/tito/resource.rb', line 69

def action(name, method: :post, path:)
  define_method(name) do |**params|
    body = params.empty? ? {} : { self.class.resource_name => params }
    response = _scope.request(method, "#{_scope.member_path(identifier)}/#{path}", body: body)
    if response.is_a?(Hash) && response[self.class.resource_name]
      _apply_response(response)
    end
    self
  end
end

.attribute_names_setObject



89
# File 'lib/tito/resource.rb', line 89

def attribute_names_set = @attribute_names_set ||= attribute_names.to_set

.collection_name(name = nil) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/tito/resource.rb', line 32

def collection_name(name = nil)
  if name
    @_collection_name = name
  else
    @_collection_name || "#{@_resource_name}s"
  end
end

.event_scoped!Object



40
41
42
# File 'lib/tito/resource.rb', line 40

def event_scoped!
  @_event_scoped = true
end

.event_scoped?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/tito/resource.rb', line 48

def event_scoped?
  @_event_scoped == true
end

.expandable(*names) ⇒ Object



60
61
62
63
# File 'lib/tito/resource.rb', line 60

def expandable(*names)
  names.each { attribute it, :json }
  @_expandable_fields = names
end

.expandable_fieldsObject



65
66
67
# File 'lib/tito/resource.rb', line 65

def expandable_fields
  @_expandable_fields || []
end

.has_many(name, resource_class_name:, foreign_key: nil) ⇒ Object

Declare a nested resource accessor



81
82
83
84
85
86
87
# File 'lib/tito/resource.rb', line 81

def has_many(name, resource_class_name:, foreign_key: nil)
  define_method(name) do
    child_class = Tito::Admin::Resources.const_get(resource_class_name)
    child_scope = _scope.nested_scope(child_class, parent_id: identifier)
    Tito::Admin::CollectionProxy.new(scope: child_scope)
  end
end

.member_path_template(template = nil) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/tito/resource.rb', line 16

def member_path_template(template = nil)
  if template
    @_member_path_template = template
  else
    @_member_path_template
  end
end

.path_template(template = nil) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/tito/resource.rb', line 8

def path_template(template = nil)
  if template
    @_path_template = template
  else
    @_path_template
  end
end

.resource_name(name = nil) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/tito/resource.rb', line 24

def resource_name(name = nil)
  if name
    @_resource_name = name
  else
    @_resource_name
  end
end

.supported_operationsObject



56
57
58
# File 'lib/tito/resource.rb', line 56

def supported_operations
  @_supported_operations || %i[list show create update delete]
end

.supports(*ops) ⇒ Object



52
53
54
# File 'lib/tito/resource.rb', line 52

def supports(*ops)
  @_supported_operations = ops
end

Instance Method Details

#destroyObject



125
126
127
128
129
130
# File 'lib/tito/resource.rb', line 125

def destroy
  _scope.request(:delete, _scope.member_path(identifier))
  @_destroyed = true
  freeze
  true
end

#destroyed?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/tito/resource.rb', line 132

def destroyed?
  @_destroyed == true
end

#identifierObject



112
113
114
115
# File 'lib/tito/resource.rb', line 112

def identifier
  slug = respond_to?(:slug) ? self.slug : nil
  slug.present? ? slug : id
end

#new_record?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/tito/resource.rb', line 108

def new_record?
  !persisted?
end

#persisted?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/tito/resource.rb', line 104

def persisted?
  @_persisted
end

#reloadObject



136
137
138
139
140
# File 'lib/tito/resource.rb', line 136

def reload
  data = _scope.request(:get, _scope.member_path(identifier))
  _apply_response(data)
  self
end

#saveObject



117
118
119
120
121
122
123
# File 'lib/tito/resource.rb', line 117

def save
  if persisted?
    _update
  else
    _create
  end
end