Class: Resourcerer::Resource

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

Overview

Public: Representation of a model that can be found, built, and assigned attributes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, name, using: [], **options, &block) ⇒ Resource

Public: Initalize a Resource with configuration options.

klass - The Controller class where the Resource is executed. name - The Symbol name of the Resource instance. options - Hash of options for the Configuration of the methods. block - If supplied, the block is executed to provide options.

Returns a normalized options Hash.



46
47
48
49
50
51
52
53
# File 'lib/resourcerer/resource.rb', line 46

def initialize(klass, name, using: [], **options, &block)
  @name = name
  @options = Configuration.new(options, &block).options

  Array.wrap(using).each do |preset|
    klass.resourcerer_configuration.fetch(preset).apply(options)
  end
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



7
8
9
# File 'lib/resourcerer/resource.rb', line 7

def controller
  @controller
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/resourcerer/resource.rb', line 7

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/resourcerer/resource.rb', line 7

def options
  @options
end

Class Method Details

.define(klass, name, **options, &block) ⇒ Object

Public: Defines a Resource and makes it accessible to a controller. For each Resource, a getter and setter is defined in the controller.

klass - The Controller class where the Resource getter will be defined. name - The name of the generated Resource. options - Config Hash for the new Resource. See Configuration::OPTIONS. block - If supplied, the block is executed to provide options.

Returns nothing.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/resourcerer/resource.rb', line 18

def self.define(klass, name, **options, &block)
  resource = new(klass, name, **options, &block)

  klass.instance_eval do
    ivar = "@resourcerer_#{ name.to_s.gsub('?', '_question_mark') }"

    private define_method(name) {
      if instance_variable_defined?(ivar)
        instance_variable_get(ivar)
      else
        instance_variable_set(ivar, resource.clone.get(self))
      end
    }

    private define_method("#{ name }=") { |value|
      instance_variable_set(ivar, value)
    }
  end
end

Instance Method Details

#get(controller) ⇒ Object

Public: Returns an object using the specified Resource configuration. The object will be built or found, and might be assigned attributes.

controller - The instance of the controller where the resource is fetched.

Returns the resource object.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/resourcerer/resource.rb', line 61

def get(controller)
  @controller = controller
  collection = call(:collection, call(:model))

  if id = call(:id)
    call(:find, id, collection)
  else
    call(:build, safe_attrs, collection)
  end.tap do |object|
    call(:assign, object, safe_attrs) if object && call(:assign?, object)
  end
end