Class: Frigate::Operation::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/frigate/operation/base.rb

Overview

Is here initialize the whole operation class A bigger description will be available soon

Constant Summary collapse

ALLOWED_ACTIONS =
[:create, :update]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Returns a new instance of Base.

Parameters:

  • opts (Hash) (defaults to: {})


59
60
61
# File 'lib/frigate/operation/base.rb', line 59

def initialize(opts={})
  @opts = opts
end

Instance Attribute Details

#model_formObject (readonly)

Returns the value of attribute model_form.



54
55
56
# File 'lib/frigate/operation/base.rb', line 54

def model_form
  @model_form
end

#optsObject (readonly)

Returns the value of attribute opts.



54
55
56
# File 'lib/frigate/operation/base.rb', line 54

def opts
  @opts
end

#paramsObject (readonly)

Returns the value of attribute params.



54
55
56
# File 'lib/frigate/operation/base.rb', line 54

def params
  @params
end

Class Method Details

.action(action) ⇒ Object

Parameters:

  • action (Symbol)


36
37
38
# File 'lib/frigate/operation/base.rb', line 36

def action(action)
  ALLOWED_ACTIONS.include?(action) ? (@operation_action = action.to_sym) : (raise ArgumentError.new('action is invalid'))
end

.form_classObject

Initializes form class and serves as a getter method



10
11
12
# File 'lib/frigate/operation/base.rb', line 10

def form_class
  @form_class ||= Class.new(Frigate::Form::Base)
end

.model(model_klass) ⇒ Object

Parameters:

  • model_klass (Class)


31
32
33
# File 'lib/frigate/operation/base.rb', line 31

def model(model_klass)
  @model_klass = model_klass
end

.model_klassObject

Serves as a getter method for model_klass class variable



15
16
17
# File 'lib/frigate/operation/base.rb', line 15

def model_klass
  @model_klass ? @model_klass : (raise StandardError.new('@model_klass nil'))
end

.operation(&block) ⇒ Object

Parameters:

  • block (Proc)


41
42
43
# File 'lib/frigate/operation/base.rb', line 41

def operation(&block)
  @operation_block = block
end

.operation_actionObject

Serves as a getter method for operation_action class variable



20
21
22
# File 'lib/frigate/operation/base.rb', line 20

def operation_action
  @operation_action ? @operation_action : (raise StandardError.new('@operation_action nil'))
end

.operation_blockObject

Serves as a getter method for operation_block class variable



25
26
27
28
# File 'lib/frigate/operation/base.rb', line 25

def operation_block
  # @operation_block.is_a?(Proc) ? @operation_block : (raise StandardError.new('operation block is not a Proc'))
  @operation_block
end

.run(params, opts = {}) ⇒ Object

Parameters:

  • params (Hash)
  • opts (Hash) (defaults to: {})


49
50
51
# File 'lib/frigate/operation/base.rb', line 49

def run(params, opts={})
  new(opts).run(params)
end

Instance Method Details

#run(params) ⇒ Frigate::Operations

Runs the whole operation mechanism

Parameters:

  • params (Hash)

Returns:

  • (Frigate::Operations)

    returns self



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/frigate/operation/base.rb', line 66

def run(params)
  @params = Hashie::Mash.new(params)
  # running actions
  case self.class.operation_action
  when :update
    @model_form = Action::Update.run(self, params)
  else
    @model_form = Action::Create.run(self, params)
  end
  self # return self
end