Class: WorkSnaps::Base

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

Direct Known Subclasses

Project, Task, TaskAssignment, User, UserAssignment

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ WorkSnaps::Base

Initializes a new object

Parameters:

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


71
72
73
# File 'lib/worksnaps/base.rb', line 71

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

Class Method Details

.attr_reader(*attrs) ⇒ Object

Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key

Parameters:

  • attrs (Array, Set, Symbol)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/worksnaps/base.rb', line 8

def self.attr_reader(*attrs)
  mod = Module.new do
    attrs.each do |attribute|
      define_method attribute do
        @attrs[attribute.to_sym]
      end
      define_method "#{attribute}?" do
        !!@attrs[attribute.to_sym]
      end
    end
  end
  const_set(:Attributes, mod)
  include mod
end

.fetch(attrs) ⇒ WorkSnaps::Base

Retrieves an object from the identity map.

Parameters:

Returns:

Raises:



34
35
36
37
38
39
40
41
# File 'lib/worksnaps/base.rb', line 34

def self.fetch(attrs)
  return unless identity_map
  if object = identity_map.fetch(Marshal.dump(attrs))
    return object
  end
  return yield if block_given?
  raise WorkSnaps::Error::IdentityMapKeyError, "key not found"
end

.fetch_or_new(attrs = {}) ⇒ WorkSnaps::Base

Retrieves an object from the identity map, or stores it in the identity map if it doesn’t already exist.

Parameters:

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

Returns:



57
58
59
60
61
62
63
64
65
# File 'lib/worksnaps/base.rb', line 57

def self.fetch_or_new(attrs={})
  return unless attrs
  return new(attrs) unless identity_map

  fetch(attrs) do
    object = new(attrs)
    store(object)
  end
end

.identity_mapObject

return [WorkSnaps::IdentityMap]



24
25
26
27
28
# File 'lib/worksnaps/base.rb', line 24

def self.identity_map
  return unless WorkSnaps.identity_map
  @identity_map = WorkSnaps.identity_map.new unless defined?(@identity_map) && @identity_map.class == WorkSnaps.identity_map
  @identity_map
end

.store(object) ⇒ WorkSnaps::Base

Stores an object in the identity map.

Parameters:

  • object (Object)

Returns:



47
48
49
50
# File 'lib/worksnaps/base.rb', line 47

def self.store(object)
  return object unless identity_map
  identity_map.store(Marshal.dump(object.attrs), object)
end

Instance Method Details

#[](method) ⇒ Object

Fetches an attribute of an object using hash notation

Parameters:

  • method (String, Symbol)

    Message to send to the object



78
79
80
81
82
# File 'lib/worksnaps/base.rb', line 78

def [](method)
  send(method.to_sym)
rescue NoMethodError
  nil
end

#attrsHash Also known as: to_hash

Retrieve the attributes of an object

Returns:



87
88
89
# File 'lib/worksnaps/base.rb', line 87

def attrs
  @attrs
end

#update(attrs) ⇒ WorkSnaps::Base

Update the attributes of an object

Parameters:

Returns:



96
97
98
99
# File 'lib/worksnaps/base.rb', line 96

def update(attrs)
  @attrs.update(attrs)
  self
end