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 =
[:save, :create, :update, :delete]
DEFAULT_CLASS_ACTIONS =
[:get, :find, :find_first, :aggregate, :update, :delete]
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

#add_errors, allow_instantiation?, attr_accessor, attr_reader, attr_writer, attributes, #attributes=, collection_attributes, entity_attr_accessor, entity_attr_reader, entity_attr_writer, entity_attributes, entity_collection_attr_accessor, entity_collection_attr_reader, entity_collection_attr_writer, inherited, #initialize, #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?, #to_hash, #to_json

Constructor Details

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

Class Method Details

._endpointObject



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

def _endpoint
  DEFAULT_ENDPOINT.new(self)
end

.action(name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/stbaldricks/entities/lib/top_level.rb', line 57

def action(name)
  instance_action(name) if [:save, :create].include?(name)

  if name == :update
    define_method(name) do |*args|
      endpoint.update(id, self, *args)
    end
  end

  if name == :delete
    define_method(:delete) do |*_args|
      endpoint.delete(id)
    end
  end

  class_action(name)
end

.actions(actions) ⇒ Object



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

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

.blacklist_action(name) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/stbaldricks/entities/lib/top_level.rb', line 75

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



49
50
51
52
53
54
55
# File 'lib/stbaldricks/entities/lib/top_level.rb', line 49

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



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

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
# 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



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

def instance_action(name)
  define_method(name) do |*args|
    endpoint.send(name, self, *args)
  end
end

Instance Method Details

#endpointObject



94
95
96
# File 'lib/stbaldricks/entities/lib/top_level.rb', line 94

def endpoint
  self.class.endpoint
end