Class: ProntoForms::Resource

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

Overview

Base class for resource-related classes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, client, parent = nil) ⇒ Resource

Returns a new instance of Resource.



29
30
31
32
33
# File 'lib/prontoforms/resource.rb', line 29

def initialize(data, client, parent = nil)
  @data = data
  @client = client
  @parent = parent
end

Instance Attribute Details

#clientClient (readonly)

Returns API client.

Returns:



11
12
13
# File 'lib/prontoforms/resource.rb', line 11

def client
  @client
end

#dataHash (readonly)

Returns Retrieve raw JSON data associated with this resource.

Returns:

  • (Hash)

    Retrieve raw JSON data associated with this resource



9
10
11
# File 'lib/prontoforms/resource.rb', line 9

def data
  @data
end

#parentResource (readonly)

Returns Parent object (applicable to child resources).

Returns:

  • (Resource)

    Parent object (applicable to child resources)



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

def parent
  @parent
end

Class Method Details

.property(name, key: nil, &block) ⇒ nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Defines a property of the resource

Returns:

  • (nil)


18
19
20
21
22
23
24
25
26
27
# File 'lib/prontoforms/resource.rb', line 18

def self.property(name, key: nil, &block)
  define_method(name) do
    if block_given?
      instance_eval(&block)
    elsif !key.nil?
      key = [key] unless key.is_a?(Array)
      key.inject(data) { |obj, k| obj.fetch(k) }
    end
  end
end

.resource_nameObject

The resource’s identifier



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

def self.resource_name
  name = to_s.split('::').last
  "#{name.downcase}s"
end

Instance Method Details

#resource_nameObject

The resource’s identifier



42
43
44
# File 'lib/prontoforms/resource.rb', line 42

def resource_name
  self.class.resource_name
end