Class: Resourcerer::Resource
- Inherits:
-
Object
- Object
- Resourcerer::Resource
- Defined in:
- lib/resourcerer/resource.rb
Overview
Public: Representation of a model that can be found, built, and assigned attributes.
Instance Attribute Summary collapse
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.define(klass, name, **options, &block) ⇒ Object
Public: Defines a Resource and makes it accessible to a controller.
Instance Method Summary collapse
-
#get(controller) ⇒ Object
Public: Returns an object using the specified Resource configuration.
-
#initialize(klass, name, using: [], **options, &block) ⇒ Resource
constructor
Public: Initalize a Resource with configuration options.
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: [], **, &block) @name = name = Configuration.new(, &block). Array.wrap(using).each do |preset| klass.resourcerer_configuration.fetch(preset).apply() end end |
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
7 8 9 |
# File 'lib/resourcerer/resource.rb', line 7 def controller @controller end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/resourcerer/resource.rb', line 7 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/resourcerer/resource.rb', line 7 def 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, **, &block) resource = new(klass, name, **, &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 |