Class: Activr::Timeline::Entry

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Defined in:
lib/activr/timeline/entry.rb

Overview

A timeline entry correspond to an activity routed to a timeline

When instanciated, it contains:

- The `timeline` it belongs to
- A copy of the routed `activity`
- The `routing_kind` that indicates how that `activity` has been routed
- User-defined `meta` data

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeline, routing_kind, activity, meta = { }) ⇒ Entry

Returns a new instance of Entry.

Parameters:

  • timeline (Timeline)

    Timeline instance

  • routing_kind (String)

    Routing kind

  • activity (Activity)

    Activity

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

    Meta data



67
68
69
70
71
72
# File 'lib/activr/timeline/entry.rb', line 67

def initialize(timeline, routing_kind, activity, meta = { })
  @timeline     = timeline
  @routing_kind = routing_kind
  @activity     = activity
  @meta         = meta && meta.symbolize_keys
end

Instance Attribute Details

#_idObject

Returns timeline entry id.

Returns:

  • (Object)

    timeline entry id



48
49
50
# File 'lib/activr/timeline/entry.rb', line 48

def _id
  @_id
end

#activityActivity (readonly)

Returns embedded activity.

Returns:



57
58
59
# File 'lib/activr/timeline/entry.rb', line 57

def activity
  @activity
end

#metaHash (readonly)

Returns meta data.

Returns:

  • (Hash)

    meta data



60
61
62
# File 'lib/activr/timeline/entry.rb', line 60

def meta
  @meta
end

#routing_kindString (readonly)

Returns routing kind.

Returns:

  • (String)

    routing kind



54
55
56
# File 'lib/activr/timeline/entry.rb', line 54

def routing_kind
  @routing_kind
end

#timelineTimeline (readonly)

Returns timeline that owns that timeline entry.

Returns:

  • (Timeline)

    timeline that owns that timeline entry



51
52
53
# File 'lib/activr/timeline/entry.rb', line 51

def timeline
  @timeline
end

Class Method Details

.from_hash(hash, timeline) ⇒ Timeline::Entry

Instanciate a timeline entry from a hash

Parameters:

  • hash (Hash)

    Timeline entry hash

  • timeline (Timeline)

    Timeline instance

Returns:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/activr/timeline/entry.rb', line 25

def from_hash(hash, timeline)
  activity_hash = hash['activity'] || hash[:activity]
  raise "No activity found in timeline entry hash: #{hash.inspect}" if activity_hash.blank?

  activity_kind = activity_hash['kind'] || activity_hash[:kind]
  raise "No activity kind in timeline entry activity: #{activity_hash.inspect}" if activity_kind.blank?

  routing_kind = hash['routing'] || hash[:routing]
  raise "No routing_kind found in timeline entry hash: #{hash.inspect}" if routing_kind.blank?

  activity   = Activr::Activity.from_hash(activity_hash)
  route_kind = Activr::Timeline::Route.kind_for_routing_and_activity(routing_kind, activity_kind)

  klass = Activr.registry.class_for_timeline_entry(timeline.kind, route_kind)
  result = klass.new(timeline, routing_kind, activity, hash['meta'] || hash[:meta])
  result._id = hash['_id'] || hash[:_id]

  result
end

Instance Method Details

#[](key) ⇒ Object

Get a meta

Examples:

timeline_entry[:foo]
# => 'bar'

Parameters:

  • key (Symbol)

    Meta name

Returns:

  • (Object)

    Meta value



82
83
84
# File 'lib/activr/timeline/entry.rb', line 82

def [](key)
  @meta[key.to_sym]
end

#[]=(key, value) ⇒ Object

Set a meta

Examples:

timeline_entry[:foo] = 'bar'

Parameters:

  • key (Symbol)

    Meta name

  • value (Object)

    Meta value



93
94
95
# File 'lib/activr/timeline/entry.rb', line 93

def []=(key, value)
  @meta[key.to_sym] = value
end

#humanization_bindings(options = { }) ⇒ Hash

Bindings for humanization sentence

For each entity, returned hash contains:

:<entity_name>       => <entity humanization>
:<entity_name>_model => <entity model instance>

All ‘meta` are merged in returned hash too.

Parameters:

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

    Humanization options

Returns:

  • (Hash)

    Humanization bindings



127
128
129
# File 'lib/activr/timeline/entry.rb', line 127

def humanization_bindings(options = { })
  @activity.humanization_bindings(options)
end

#humanize(options = { }) ⇒ String

Note:

MAY be overriden by child class for specialized humanization

Humanize that timeline entry

Parameters:

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

    Options hash

Options Hash (options):

  • :html (true, false)

    Output HTML (default: ‘false`)

Returns:

  • (String)

    Humanized timeline entry



138
139
140
141
142
143
144
145
146
# File 'lib/activr/timeline/entry.rb', line 138

def humanize(options = { })
  if !self.timeline_route.settings[:humanize].blank?
    # specialized humanization
    Activr.sentence(self.timeline_route.settings[:humanize], self.humanization_bindings(options))
  else
    # default humanization
    @activity.humanize(options)
  end
end

#store!Object

Note:

SIDE EFFECT: The ‘_id` field is set

Store in database



158
159
160
161
162
163
# File 'lib/activr/timeline/entry.rb', line 158

def store!
  run_callbacks(:store) do
    # store
    @_id = Activr.storage.insert_timeline_entry(self)
  end
end

#stored?true, false

Is it already stored

Returns:

  • (true, false)


151
152
153
# File 'lib/activr/timeline/entry.rb', line 151

def stored?
  !@_id.nil?
end

#timeline_routeTimeline::Route

Get the corresponding timeline route

Returns:



118
119
120
121
122
123
124
# File 'lib/activr/timeline/entry.rb', line 118

def timeline_route
  @timeline_route ||= begin
    result = @timeline.route_for_kind(Activr::Timeline::Route.kind_for_routing_and_activity(@routing_kind, @activity.kind))
    raise "Failed to find a route for #{@routing_kind} / #{@activity.kind}: #{self.inspect}" if result.nil?
    result
  end
end

#to_hashHash

Note:

All keys are stringified (ie. there is no Symbol)

Serialize timeline entry to a hash

Returns:

  • (Hash)

    Timeline entry hash



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/activr/timeline/entry.rb', line 102

def to_hash
  # fields
  result = {
    'rcpt'     => @timeline.recipient_id,
    'routing'  => @routing_kind,
    'activity' => @activity.to_hash,
  }

  result['meta'] = @meta.stringify_keys unless @meta.blank?

  result
end