Class: ApiMaker::BaseResource
- Inherits:
-
Object
- Object
- ApiMaker::BaseResource
- Defined in:
- lib/api_maker/base_resource.rb
Constant Summary collapse
- CRUD =
[:create, :read, :update, :destroy].freeze
Instance Attribute Summary collapse
-
#ability ⇒ Object
readonly
Returns the value of attribute ability.
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Class Method Summary collapse
- ._attributes ⇒ Object
- ._relationships ⇒ Object
- .attributes(*attributes, **args) ⇒ Object
- .collection_commands(*list) ⇒ Object
- .collection_name ⇒ Object
- .default_select ⇒ Object
- .member_commands(*list) ⇒ Object
- .model_class ⇒ Object
- .model_class=(klass) ⇒ Object
- .model_class_name ⇒ Object
- .plural_name ⇒ Object
- .relationships(*relationships) ⇒ Object
- .short_name ⇒ Object
Instance Method Summary collapse
-
#initialize(ability: nil, args: {}, model:) ⇒ BaseResource
constructor
A new instance of BaseResource.
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
#ability ⇒ Object (readonly)
Returns the value of attribute ability.
2 3 4 |
# File 'lib/api_maker/base_resource.rb', line 2 def ability @ability end |
#args ⇒ Object (readonly)
Returns the value of attribute args.
2 3 4 |
# File 'lib/api_maker/base_resource.rb', line 2 def args @args end |
#model ⇒ Object (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
._attributes ⇒ Object
14 15 16 |
# File 'lib/api_maker/base_resource.rb', line 14 def self._attributes ApiMaker::MemoryStorage.current.storage_for(self, :attributes) end |
._relationships ⇒ Object
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_name ⇒ Object
55 56 57 |
# File 'lib/api_maker/base_resource.rb', line 55 def self.collection_name @collection_name ||= plural_name.underscore.dasherize end |
.default_select ⇒ Object
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_class ⇒ Object
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_name ⇒ Object
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_name ⇒ Object
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_name ⇒ Object
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 |