Class: Zodra::Resource
- Inherits:
-
Object
- Object
- Zodra::Resource
- Defined in:
- lib/zodra/resource.rb
Constant Summary collapse
- CRUD_ACTIONS =
{ index: { http_method: :get, member: false }, show: { http_method: :get, member: true }, create: { http_method: :post, member: false }, update: { http_method: :patch, member: true }, destroy: { http_method: :delete, member: true } }.freeze
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#contract_name ⇒ Object
readonly
Returns the value of attribute contract_name.
-
#controller_name ⇒ Object
readonly
Returns the value of attribute controller_name.
-
#custom_actions ⇒ Object
readonly
Returns the value of attribute custom_actions.
-
#except ⇒ Object
readonly
Returns the value of attribute except.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#only ⇒ Object
readonly
Returns the value of attribute only.
-
#singular ⇒ Object
readonly
Returns the value of attribute singular.
Instance Method Summary collapse
- #add_child(resource) ⇒ Object
- #add_collection_action(http_method, action_name) ⇒ Object
- #add_member_action(http_method, action_name) ⇒ Object
- #crud_actions ⇒ Object
-
#initialize(name:, singular: false, contract: nil, controller: nil, only: nil, except: nil) ⇒ Resource
constructor
A new instance of Resource.
- #singular? ⇒ Boolean
Constructor Details
#initialize(name:, singular: false, contract: nil, controller: nil, only: nil, except: nil) ⇒ Resource
Returns a new instance of Resource.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/zodra/resource.rb', line 16 def initialize(name:, singular: false, contract: nil, controller: nil, only: nil, except: nil) @name = name.to_sym @singular = singular @contract_name = contract&.to_sym || @name @controller_name = controller @only = only&.map(&:to_sym) @except = except&.map(&:to_sym) @custom_actions = [] @children = [] end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
13 14 15 |
# File 'lib/zodra/resource.rb', line 13 def children @children end |
#contract_name ⇒ Object (readonly)
Returns the value of attribute contract_name.
13 14 15 |
# File 'lib/zodra/resource.rb', line 13 def contract_name @contract_name end |
#controller_name ⇒ Object (readonly)
Returns the value of attribute controller_name.
13 14 15 |
# File 'lib/zodra/resource.rb', line 13 def controller_name @controller_name end |
#custom_actions ⇒ Object (readonly)
Returns the value of attribute custom_actions.
13 14 15 |
# File 'lib/zodra/resource.rb', line 13 def custom_actions @custom_actions end |
#except ⇒ Object (readonly)
Returns the value of attribute except.
13 14 15 |
# File 'lib/zodra/resource.rb', line 13 def except @except end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/zodra/resource.rb', line 13 def name @name end |
#only ⇒ Object (readonly)
Returns the value of attribute only.
13 14 15 |
# File 'lib/zodra/resource.rb', line 13 def only @only end |
#singular ⇒ Object (readonly)
Returns the value of attribute singular.
13 14 15 |
# File 'lib/zodra/resource.rb', line 13 def singular @singular end |
Instance Method Details
#add_child(resource) ⇒ Object
47 48 49 |
# File 'lib/zodra/resource.rb', line 47 def add_child(resource) @children << resource end |
#add_collection_action(http_method, action_name) ⇒ Object
43 44 45 |
# File 'lib/zodra/resource.rb', line 43 def add_collection_action(http_method, action_name) @custom_actions << { name: action_name.to_sym, http_method: http_method.to_sym, member: false } end |
#add_member_action(http_method, action_name) ⇒ Object
39 40 41 |
# File 'lib/zodra/resource.rb', line 39 def add_member_action(http_method, action_name) @custom_actions << { name: action_name.to_sym, http_method: http_method.to_sym, member: true } end |
#crud_actions ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/zodra/resource.rb', line 31 def crud_actions actions = CRUD_ACTIONS.keys actions -= [:index] if singular? actions &= @only if @only actions -= @except if @except actions end |
#singular? ⇒ Boolean
27 28 29 |
# File 'lib/zodra/resource.rb', line 27 def singular? @singular end |