Module: Polyseerio::Resource::Factory

Defined in:
lib/resource/factory.rb

Overview

Resource building methods.

Class Method Summary collapse

Class Method Details

._makeObject

Create a resource.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/resource/factory.rb', line 84

def self._make
  proc do |type, request, cid, options = {}|
    unless Definition::DEFINITION.key? type
      raise ArgumentError, 'Could not find definition for resource: ' \
        "#{type}"
    end

    definition = Definition::DEFINITION.fetch(type)

    # Create the resource / class.
    resource = if defines_singleton? definition
                 {}
               else
                 create(type, request, cid, options)
               end

    # Add statics.
    if definition.key? Definition::STATICS
      statics = SDK::Static.factory(
        request,
        type,
        definition[Definition::STATICS],
        options
      )

      add_statics(resource, statics)
    end

    # Add methods.
    if definition.key? Definition::METHODS
      methods = SDK::Method.factory(
        request,
        type,
        definition[Definition::METHODS],
        options
      )

      add_methods(resource, request, methods)
    end

    resource
  end
end

.add_method(*args) ⇒ Object

Add an instance method to a Class.



27
28
29
30
31
32
33
# File 'lib/resource/factory.rb', line 27

def self.add_method(*args)
  proc do |(name, method), resource|
    resource.class_eval do
      define_method(name, &Helper.forward_self(method))
    end
  end.curry.call(*args)
end

.add_methods(resource, _request, methods = {}) ⇒ Object

Add a collection of instance methods to a Class.



43
44
45
46
47
# File 'lib/resource/factory.rb', line 43

def self.add_methods(resource, _request, methods = {})
  return resource if methods.empty?

  methods.each_with_object(resource, &add_method)
end

.add_static(*args) ⇒ Object

Add a static method to a Class.



18
19
20
21
22
23
24
# File 'lib/resource/factory.rb', line 18

def self.add_static(*args)
  proc do |(name, method), resource|
    resource.class_eval do
      define_singleton_method(name, &method)
    end
  end.curry.call(*args)
end

.add_statics(resource, statics = {}) ⇒ Object

Add a collection of static methods to a Class.



36
37
38
39
40
# File 'lib/resource/factory.rb', line 36

def self.add_statics(resource, statics = {})
  return resource if statics.empty?

  statics.each_with_object(resource, &add_static)
end

.create(type, request, cid, copts = {}) ⇒ Object

Create a resource. TODO: copts are not optional



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

def self.create(type, request, cid, copts = {})
  resource = Object.const_set(to_class_name(type, cid), Class.new(Base))
  resource.define_singleton_method(:copts) do
    copts
  end

  resource.define_singleton_method(:type) do
    type
  end

  resource.define_singleton_method(:request) do
    request
  end

  resource.define_singleton_method(:cid) do
    cid
  end

  resource
end

.defines_singleton?(definition) ⇒ Boolean

Determine if a resource definition represents a singleton.

Returns:

  • (Boolean)


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

def self.defines_singleton?(definition)
  !definition.key?(:methods) || definition[:methods].empty?
end

.get_memoize_key(resource, _request, cid, _options = {}) ⇒ Object

Generate a memoize key based on factory arguments



129
130
131
# File 'lib/resource/factory.rb', line 129

def self.get_memoize_key(resource, _request, cid, _options = {})
  :"#{resource}_#{cid}"
end

.make(*args) ⇒ Object

Memoized calls to make. A little ugly, would like a better memoize API.



144
145
146
# File 'lib/resource/factory.rb', line 144

def self.make(*args)
  @make.call(*args)
end

.memoize_keyObject

Convenience for get_memoize_key in proc form.



134
135
136
137
138
# File 'lib/resource/factory.rb', line 134

def self.memoize_key
  proc do |*args|
    get_memoize_key(*args)
  end
end

.to_class_name(resource, cid = '') ⇒ Object

Takes a resource name and creates a class name.



50
51
52
53
54
55
56
57
58
# File 'lib/resource/factory.rb', line 50

def self.to_class_name(resource, cid = '')
  parts = resource.to_s.split('-').map(&:capitalize)

  parts[parts.size - 1] = Inflection.singular(parts.last)

  name = parts.join ''

  "#{name}#{cid}"
end