Class: HaveAPI::Resource

Inherits:
Common
  • Object
show all
Defined in:
lib/haveapi/resource.rb

Direct Known Subclasses

HaveAPI::Resources::ActionState

Class Attribute Summary collapse

Class Method Summary collapse

Methods inherited from Common

check_build, has_attr, inherit_attrs

Class Attribute Details

.resource_nameObject



50
51
52
# File 'lib/haveapi/resource.rb', line 50

def self.resource_name
  (@resource_name ? @resource_name.to_s : to_s).demodulize
end

Class Method Details

.action_defined(klass) ⇒ Object



18
19
20
21
# File 'lib/haveapi/resource.rb', line 18

def self.action_defined(klass)
  @actions ||= []
  @actions << klass
end

.actionsObject



32
33
34
# File 'lib/haveapi/resource.rb', line 32

def self.actions(&)
  (@actions || []).each(&)
end

.define_action(name, superclass: Action) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/haveapi/resource.rb', line 119

def self.define_action(name, superclass: Action, &)
  return false if const_defined?(name)

  cls = Class.new(superclass)
  const_set(name, cls)
  cls.resource = self
  cls.action_name = name
  superclass.delayed_inherited(cls)
  cls.class_exec(&)
end

.define_resource(name, superclass: Resource, &block) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/haveapi/resource.rb', line 109

def self.define_resource(name, superclass: Resource, &block)
  return false if const_defined?(name) && self != HaveAPI::Resource

  cls = Class.new(superclass)
  const_set(name, cls) if self != HaveAPI::Resource
  cls.resource_name = name
  cls.class_exec(&block) if block
  cls
end

.describe(hash, context) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/haveapi/resource.rb', line 82

def self.describe(hash, context)
  ret = { description: desc, actions: {}, resources: {} }

  context.resource = self

  orig_resource_path = context.resource_path
  context.resource_path = context.resource_path + [resource_name.underscore]

  hash[:actions].each do |action, path|
    context.action = action
    context.path = path

    a_name = action.action_name.underscore
    a_desc = action.describe(context)

    ret[:actions][a_name] = a_desc if a_desc
  end

  hash[:resources].each do |resource, children|
    ret[:resources][resource.resource_name.underscore] = resource.describe(children, context)
  end

  context.resource_path = orig_resource_path

  ret
end

.inherited(subclass) ⇒ Object



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

def self.inherited(subclass)
  super
  subclass.instance_variable_set(:@obj_type, obj_type)
end

.params(name, &block) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/haveapi/resource.rb', line 23

def self.params(name, &block)
  if block
    @params ||= {}
    @params[name] = block
  else
    @params[name]
  end
end

.resourcesObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/haveapi/resource.rb', line 36

def self.resources
  constants.select do |c|
    obj = const_get(c)

    begin
      if obj.obj_type == :resource
        yield obj
      end
    rescue NoMethodError
      next
    end
  end
end

.rest_nameObject



58
59
60
# File 'lib/haveapi/resource.rb', line 58

def self.rest_name
  singular ? resource_name.singularize.underscore : resource_name.tableize
end

.routes(prefix = '/', resource_path: []) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/haveapi/resource.rb', line 62

def self.routes(prefix = '/', resource_path: [])
  ret = []
  prefix = "#{prefix}#{@route || rest_name}/"
  new_resource_path = resource_path + [resource_name.underscore]

  actions do |a|
    # Call used_by for selected model adapters. It is safe to do
    # only when all classes are loaded.
    a.initialize

    ret << Route.new(a.build_route(prefix).chomp('/'), a, new_resource_path)
  end

  resources do |r|
    ret << { r => r.routes(prefix, resource_path: new_resource_path) }
  end

  ret
end