Class: ApiMaker::BaseResource

Inherits:
Object
  • Object
show all
Defined in:
lib/api_maker/base_resource.rb

Constant Summary collapse

CRUD =
[:create, :read, :update, :destroy].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ability: nil, args: {}, model:) ⇒ BaseResource

Returns a new instance of BaseResource.



73
74
75
76
77
# File 'lib/api_maker/base_resource.rb', line 73

def initialize(ability: nil, args: {}, model:)
  @ability = ability
  @args = args
  @model = model
end

Instance Attribute Details

#abilityObject (readonly)

Returns the value of attribute ability.



2
3
4
# File 'lib/api_maker/base_resource.rb', line 2

def ability
  @ability
end

#argsObject (readonly)

Returns the value of attribute args.



2
3
4
# File 'lib/api_maker/base_resource.rb', line 2

def args
  @args
end

#modelObject (readonly)

Returns the value of attribute model.



2
3
4
# File 'lib/api_maker/base_resource.rb', line 2

def model
  @model
end

Class Method Details

._attributesObject



14
15
16
# File 'lib/api_maker/base_resource.rb', line 14

def self._attributes
  ApiMaker::MemoryStorage.current.storage_for(self, :attributes)
end

._relationshipsObject



51
52
53
# File 'lib/api_maker/base_resource.rb', line 51

def self._relationships
  ApiMaker::MemoryStorage.current.storage_for(self, :relationships)
end

.attributes(*attributes, **args) ⇒ Object



8
9
10
11
12
# File 'lib/api_maker/base_resource.rb', line 8

def self.attributes(*attributes, **args)
  attributes.each do |attribute|
    ApiMaker::MemoryStorage.current.add(self, :attributes, attribute, args)
  end
end

.collection_commands(*list) ⇒ Object



18
19
20
21
22
# File 'lib/api_maker/base_resource.rb', line 18

def self.collection_commands(*list)
  list.each do |collection_command|
    ApiMaker::MemoryStorage.current.add(self, :collection_commands, collection_command)
  end
end

.collection_nameObject



55
56
57
# File 'lib/api_maker/base_resource.rb', line 55

def self.collection_name
  @collection_name ||= plural_name.underscore.dasherize
end

.default_selectObject



59
60
61
62
63
# File 'lib/api_maker/base_resource.rb', line 59

def self.default_select
  _attributes.select do |_attribute_name, args|
    !args.fetch(:args).key?(:selected_by_default) || args.fetch(:args).fetch(:selected_by_default)
  end
end

.member_commands(*list) ⇒ Object



24
25
26
27
28
# File 'lib/api_maker/base_resource.rb', line 24

def self.member_commands(*list)
  list.each do |member_command|
    ApiMaker::MemoryStorage.current.add(self, :member_commands, member_command)
  end
end

.model_classObject



36
37
38
39
# File 'lib/api_maker/base_resource.rb', line 36

def self.model_class
  # Use the name to constantize to avoid reloading issues with Rails
  model_class_name.constantize
end

.model_class=(klass) ⇒ Object



30
31
32
33
34
# File 'lib/api_maker/base_resource.rb', line 30

def self.model_class=(klass)
  # Set the name to avoid reloading issues with Rails
  @model_class_name ||= klass.name
  ApiMaker::MemoryStorage.current.model_class_for(resource: self, klass: klass)
end

.model_class_nameObject



41
42
43
# File 'lib/api_maker/base_resource.rb', line 41

def self.model_class_name
  @model_class_name ||= name.gsub(/Resource$/, "").gsub(/^Resources::/, "")
end

.plural_nameObject



65
66
67
# File 'lib/api_maker/base_resource.rb', line 65

def self.plural_name
  @plural_name ||= short_name.pluralize
end

.relationships(*relationships) ⇒ Object



45
46
47
48
49
# File 'lib/api_maker/base_resource.rb', line 45

def self.relationships(*relationships)
  relationships.each do |relationship|
    ApiMaker::MemoryStorage.current.add(self, :relationships, relationship)
  end
end

.short_nameObject



69
70
71
# File 'lib/api_maker/base_resource.rb', line 69

def self.short_name
  @short_name ||= name.match(/\AResources::(.+)Resource\Z/)[1]
end