Class: ObjectServices::Base

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

Defined Under Namespace

Classes: ServiceError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Base

:nodoc:



10
11
12
13
# File 'lib/object_services/base.rb', line 10

def initialize(model)
  @model = model
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



3
4
5
# File 'lib/object_services/base.rb', line 3

def errors
  @errors
end

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/object_services/base.rb', line 3

def model
  @model
end

Instance Method Details

#create(params) ⇒ Boolean

Create a record with a given param object

Parameters:

  • params (Object)

Returns:

  • (Boolean)

    whether the model was created or not



25
26
27
# File 'lib/object_services/base.rb', line 25

def create(params)
  update_model(params)
end

#destroyBoolean

Destroys the service’s model and clears it

Returns:

  • (Boolean)

    whether the model was destroyed or not



31
32
33
34
35
36
37
38
39
40
# File 'lib/object_services/base.rb', line 31

def destroy
  if @model.present?
    @model.destroy
    @model = nil
    true
  else
    @errors << self.class.model_not_found
    return false
  end
end

#update(params) ⇒ Boolean

Update the service’s model with a given param object

Parameters:

  • params (Object)

Returns:

  • (Boolean)

    whether the model was updated or not



18
19
20
# File 'lib/object_services/base.rb', line 18

def update(params)
  update_model(params)
end