Class: Zodra::Resource

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

Instance Method Summary collapse

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

#childrenObject (readonly)

Returns the value of attribute children.



13
14
15
# File 'lib/zodra/resource.rb', line 13

def children
  @children
end

#contract_nameObject (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_nameObject (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_actionsObject (readonly)

Returns the value of attribute custom_actions.



13
14
15
# File 'lib/zodra/resource.rb', line 13

def custom_actions
  @custom_actions
end

#exceptObject (readonly)

Returns the value of attribute except.



13
14
15
# File 'lib/zodra/resource.rb', line 13

def except
  @except
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/zodra/resource.rb', line 13

def name
  @name
end

#onlyObject (readonly)

Returns the value of attribute only.



13
14
15
# File 'lib/zodra/resource.rb', line 13

def only
  @only
end

#singularObject (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_actionsObject



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

Returns:

  • (Boolean)


27
28
29
# File 'lib/zodra/resource.rb', line 27

def singular?
  @singular
end