Class: Activr::Timeline::Entry
- Inherits:
-
Object
- Object
- Activr::Timeline::Entry
- 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
-
#_id ⇒ Object
Timeline entry id.
-
#activity ⇒ Activity
readonly
Embedded activity.
-
#meta ⇒ Hash
readonly
Meta data.
-
#routing_kind ⇒ String
readonly
Routing kind.
-
#timeline ⇒ Timeline
readonly
Timeline that owns that timeline entry.
Class Method Summary collapse
-
.from_hash(hash, timeline) ⇒ Timeline::Entry
Instanciate a timeline entry from a hash.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get a meta.
-
#[]=(key, value) ⇒ Object
Set a meta.
-
#humanization_bindings(options = { }) ⇒ Hash
Bindings for humanization sentence.
-
#humanize(options = { }) ⇒ String
Humanize that timeline entry.
-
#initialize(timeline, routing_kind, activity, meta = { }) ⇒ Entry
constructor
A new instance of Entry.
-
#store! ⇒ Object
Store in database.
-
#stored? ⇒ true, false
Is it already stored.
-
#timeline_route ⇒ Timeline::Route
Get the corresponding timeline route.
-
#to_hash ⇒ Hash
Serialize timeline entry to a hash.
Constructor Details
#initialize(timeline, routing_kind, activity, meta = { }) ⇒ Entry
Returns a new instance of Entry.
67 68 69 70 71 72 |
# File 'lib/activr/timeline/entry.rb', line 67 def initialize(timeline, routing_kind, activity, = { }) @timeline = timeline @routing_kind = routing_kind @activity = activity @meta = && .symbolize_keys end |
Instance Attribute Details
#_id ⇒ Object
Returns timeline entry id.
48 49 50 |
# File 'lib/activr/timeline/entry.rb', line 48 def _id @_id end |
#activity ⇒ Activity (readonly)
Returns embedded activity.
57 58 59 |
# File 'lib/activr/timeline/entry.rb', line 57 def activity @activity end |
#meta ⇒ Hash (readonly)
Returns meta data.
60 61 62 |
# File 'lib/activr/timeline/entry.rb', line 60 def @meta end |
#routing_kind ⇒ String (readonly)
Returns routing kind.
54 55 56 |
# File 'lib/activr/timeline/entry.rb', line 54 def routing_kind @routing_kind end |
#timeline ⇒ Timeline (readonly)
Returns 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
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
82 83 84 |
# File 'lib/activr/timeline/entry.rb', line 82 def [](key) @meta[key.to_sym] end |
#[]=(key, value) ⇒ Object
Set a meta
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.
127 128 129 |
# File 'lib/activr/timeline/entry.rb', line 127 def humanization_bindings( = { }) @activity.humanization_bindings() end |
#humanize(options = { }) ⇒ String
MAY be overriden by child class for specialized humanization
Humanize that timeline entry
138 139 140 141 142 143 144 145 146 |
# File 'lib/activr/timeline/entry.rb', line 138 def humanize( = { }) if !self.timeline_route.settings[:humanize].blank? # specialized humanization Activr.sentence(self.timeline_route.settings[:humanize], self.humanization_bindings()) else # default humanization @activity.humanize() end end |
#store! ⇒ Object
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
151 152 153 |
# File 'lib/activr/timeline/entry.rb', line 151 def stored? !@_id.nil? end |
#timeline_route ⇒ Timeline::Route
Get the corresponding timeline route
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_hash ⇒ Hash
All keys are stringified (ie. there is no Symbol)
Serialize timeline entry to a 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 |