Class: Belt::NestedResourceBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/belt/route_dsl.rb

Instance Method Summary collapse

Constructor Details

#initialize(gateway, prefix, collection_prefix, inherited_tables: [], inherited_auth: nil) ⇒ NestedResourceBuilder

Returns a new instance of NestedResourceBuilder.



52
53
54
55
56
57
58
# File 'lib/belt/route_dsl.rb', line 52

def initialize(gateway, prefix, collection_prefix, inherited_tables: [], inherited_auth: nil)
  @gateway = gateway
  @prefix = prefix
  @collection_prefix = collection_prefix
  @inherited_tables = inherited_tables
  @inherited_auth = inherited_auth
end

Instance Method Details

#collectionObject



89
90
91
92
# File 'lib/belt/route_dsl.rb', line 89

def collection(&)
  MemberCollectionBuilder.new(@gateway, @collection_prefix, @inherited_tables,
                              @inherited_auth).instance_eval(&)
end

#memberObject



85
86
87
# File 'lib/belt/route_dsl.rb', line 85

def member(&)
  MemberCollectionBuilder.new(@gateway, @prefix, @inherited_tables, @inherited_auth).instance_eval(&)
end

#resources(name, options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/belt/route_dsl.rb', line 60

def resources(name, options = {})
  resource_name = name.to_s
  singular = @gateway.send(:singularize, resource_name)
  param_name = options[:param] || "#{singular}_id"
  options = merge_inherited_options(options)
  options = @gateway.send(:auto_infer_tables, resource_name, options)
  resource_options = options.merge(route_type: :resources)
  actions = @gateway.send(:determine_actions, options)

  @gateway.send(:add_route, :get, "#{@prefix}/#{resource_name}", resource_options) if actions.include?(:index)
  @gateway.send(:add_route, :post, "#{@prefix}/#{resource_name}", resource_options) if actions.include?(:create)
  if actions.include?(:show)
    @gateway.send(:add_route, :get, "#{@prefix}/#{resource_name}/{#{param_name}}",
                  resource_options)
  end
  if actions.include?(:update)
    @gateway.send(:add_route, :put, "#{@prefix}/#{resource_name}/{#{param_name}}",
                  resource_options)
  end
  return unless actions.include?(:destroy)

  @gateway.send(:add_route, :delete, "#{@prefix}/#{resource_name}/{#{param_name}}",
                resource_options)
end