Class: AdequateExposure::Exposure

Inherits:
Object
  • Object
show all
Defined in:
lib/adequate_exposure/exposure.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, name, fetch_block = nil, **options, &block) ⇒ Exposure

Public: Initalize an Exposure with a hash of options.

If a block is given, the Proc is assigned to value of options.

The ‘asserts_*` section raise errors if the controller was initialized with an unacceptable options Hash.

controller - The Controller class where methods will be exposed. name - The String name of the Exposure instance. fetch_block - Proc that will be executed if the exposed

attribute has no value (default: nil).

options - Hash of options for the Behavior of the exposed methods. block - If supplied, the exposed attribute method executes

the Proc.

Returns a normalized options Hash.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/adequate_exposure/exposure.rb', line 38

def initialize(controller, name, fetch_block=nil, **options, &block)
  @controller = controller
  @options = options.with_indifferent_access.merge(name: name)

  merge_lambda_option :fetch, fetch_block if fetch_block
  merge_lambda_option :fetch, block if block_given?

  assert_singleton_option :fetch
  assert_singleton_option :from
  assert_incompatible_options_pair :parent, :model
  assert_incompatible_options_pair :parent, :scope
  assert_incompatible_options_pair :find_by, :find

  normalize_options
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



3
4
5
# File 'lib/adequate_exposure/exposure.rb', line 3

def controller
  @controller
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/adequate_exposure/exposure.rb', line 3

def options
  @options
end

Class Method Details

.expose!(*args, **options, &block) ⇒ Object

Public: Initializes an Exposure and makes it accessible to a controller. For each Exposure, a getter and setter is defined. Those getters and setters are made available to the controller as helper methods.

*args - An Array of all parameters for the new Exposure. See

#initialize.

**args - A Hash of options. See #initialize. block - If supplied, the exposed attribute method executes

the Proc when called.

Returns a collection of exposed helper methods.



17
18
19
# File 'lib/adequate_exposure/exposure.rb', line 17

def self.expose!(*args, **options, &block)
  new(*args, **options, &block).expose!
end

Instance Method Details

#expose!Object

Public: Creates a getter and setter methods for the attribute. Those methods are made avaiable to the controller as helper methods.

Returns a collection of exposed helper methods.



59
60
61
62
# File 'lib/adequate_exposure/exposure.rb', line 59

def expose!
  expose_attribute!
  expose_helper_methods!
end