Class: SBF::Client::TopLevelEntity

Inherits:
BaseEntity show all
Includes:
Entities::Cacheable
Defined in:
lib/stbaldricks/entities/lib/top_level.rb

Constant Summary collapse

DEFAULT_ENDPOINT =
SBF::Client::EntityEndpoint
DEFAULT_INSTANCE_ACTIONS =
%i(save create update delete).freeze
DEFAULT_CLASS_ACTIONS =
%i(get find find_first aggregate update delete).freeze
DEFAULT_CRUD_ACTIONS =
DEFAULT_INSTANCE_ACTIONS + DEFAULT_CLASS_ACTIONS

Constants inherited from BaseEntity

BaseEntity::ELSE

Instance Attribute Summary

Attributes inherited from BaseEntity

#errors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Entities::Cacheable

#cache_id, included

Methods inherited from BaseEntity

allow_instantiation?, attr_accessor, attr_reader, attr_writer, collection_attributes, defined_attributes, #destroyed?, #dirty_data, entity_attr_accessor, entity_attr_reader, entity_attr_writer, entity_attributes, entity_collection_attr_accessor, entity_collection_attr_reader, entity_collection_attr_writer, #error, inherited, #initialize, #keys_hash, #model_name, multitype_attr_accessor, multitype_attr_reader, multitype_attr_writer, multitype_collection_attr_accessor, multitype_collection_attr_reader, multitype_collection_attr_writer, #not_provided_attributes, optional_attributes, #persisted?, #reload!, #reload_recursive, #rollback!, #to_hash, #to_json, #to_partial

Methods included from EntityResponseConcern

#add_errors, #data, #error?, #errors?, #errors_http_code=, #http_code, #success?

Constructor Details

This class inherits a constructor from SBF::Client::BaseEntity

Class Method Details

._endpointObject



28
29
30
# File 'lib/stbaldricks/entities/lib/top_level.rb', line 28

def _endpoint
  DEFAULT_ENDPOINT.new(self)
end

.action(name) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/stbaldricks/entities/lib/top_level.rb', line 60

def action(name)
  instance_action(name) if %i(save create).include?(name)

  if name == :update
    define_method(name) do |*args|
      result = endpoint.update(id, self, *args)
      result unless result.is_a?(SBF::Client::TopLevelEntity)
      result.errors? ? false : result
    end
  end

  if name == :delete
    define_method(:delete) do |*_args|
      response = endpoint.delete(id)
      if response.error?
        add_errors(response.error)
        errors.instance_variable_set(:@http_code, response.http_code)
        false
      else
        @destroyed = true
        freeze
      end
    end
  end

  class_action(name)
end

.actions(actions) ⇒ Object



40
41
42
# File 'lib/stbaldricks/entities/lib/top_level.rb', line 40

def actions(actions)
  actions.each { |m| action(m) }
end

.blacklist_action(name) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/stbaldricks/entities/lib/top_level.rb', line 88

def blacklist_action(name)
  define_method(name) do |*_|
    raise NoMethodError.new("#{name} is blacklisted", name)
  end

  singleton_class.class_eval do
    define_method(name) do |*_|
      raise NoMethodError.new("#{name} is blacklisted", name)
    end
  end
end

.class_action(name) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/stbaldricks/entities/lib/top_level.rb', line 52

def class_action(name)
  singleton_class.class_eval do
    define_method(name) do |*args|
      endpoint.send(name, *args)
    end
  end
end

.define_endpoint(mod, options) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/stbaldricks/entities/lib/top_level.rb', line 32

def define_endpoint(mod, options)
  singleton_class.class_eval do
    define_method(:_endpoint) do
      @_endpoint ||= mod.new(options.fetch(:target_class, self))
    end
  end
end

.endpoint(mod = nil, options = {}) ⇒ Object



22
23
24
25
26
# File 'lib/stbaldricks/entities/lib/top_level.rb', line 22

def endpoint(mod = nil, options = {})
  return define_endpoint(mod, options) if mod

  _endpoint
end

.instance_action(name) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/stbaldricks/entities/lib/top_level.rb', line 44

def instance_action(name)
  define_method(name) do |*args|
    result = endpoint.send(name, self, *args)
    result unless result.is_a?(SBF::Client::TopLevelEntity)
    %i(save create).include?(name) && result.errors? ? false : result
  end
end

Instance Method Details

#endpointObject



107
108
109
# File 'lib/stbaldricks/entities/lib/top_level.rb', line 107

def endpoint
  self.class.endpoint
end