Class: Wash::Entry
- Inherits:
-
Object
- Object
- Wash::Entry
- Defined in:
- lib/wash/entry.rb
Overview
Entry represents a common base class for Wash entries. All plugin entries should extend this class.
Instance Attribute Summary collapse
-
#name ⇒ Object
All entries have a name.
Class Method Summary collapse
-
.attributes(attr, *attrs) ⇒ Object
attributes is a class-level tag specifying all the attributes that make sense for instances of this specific kind of entry.
-
.children ⇒ Object
children returns this Entry’s child classes.
-
.description(description) ⇒ Object
description is a class-level tag specifying the entry’s description.
-
.is_singleton ⇒ Object
is_singleton is a class-level tag indicating that the given Entry’s a singleton.
-
.label(lbl) ⇒ Object
label is a class-level tag specifying the entry’s label.
-
.meta_attribute_schema(schema) ⇒ Object
meta_attribute_schema sets the meta attribute’s schema to schema.
-
.metadata_schema(schema) ⇒ Object
metadata_schema sets the metadata schema to schema.
-
.parent_of(child_klass, *child_klasses) ⇒ Object
parent_of indicates that this kind of Entry is the parent of the given child classes (i.e. child entries).
-
.signal(name, description) ⇒ Object
signal adds the given signal to the entry’s list of supported signals.
-
.signal_group(name, regex, description) ⇒ Object
signal_group adds the given signal group to the entry’s list of supported signals.
-
.slash_replacer(char) ⇒ Object
slash_replacer is a class-level tag that specifies the slash replacer.
-
.state(field, *fields) ⇒ Object
state is a class-level tag that specifies the minimum state required to reconstruct all instances of this specific kind of entry.
Instance Method Summary collapse
-
#cache_ttls(ttls = {}) ⇒ Object
cache_ttls sets the cache TTLs (time-to-live) of the given methods.
-
#prefetch(method, *methods) ⇒ Object
prefetch indicates that the given methods should be prefetched.
-
#schema ⇒ Object
schema returns the entry’s schema.
- #to_json ⇒ Object
-
#type_id ⇒ Object
type_id returns the entry’s type ID, which is its fully-qualified class name.
Instance Attribute Details
#name ⇒ Object
All entries have a name. Note that the name is always included in the entry’s state hash.
242 243 244 |
# File 'lib/wash/entry.rb', line 242 def name @name end |
Class Method Details
.attributes(attr, *attrs) ⇒ Object
attributes is a class-level tag specifying all the attributes that make sense for instances of this specific kind of entry. It will pass the specified attributes along to attr_accessor so that instances can set their values. For example, something like
26 27 28 29 |
# File 'lib/wash/entry.rb', line 26 def attributes(attr, *attrs) @attributes ||= [] @attributes += set_fields(attr, *attrs) end |
.children ⇒ Object
children returns this Entry’s child classes. It is a helper for Entry schemas, and is useful for DRY’ing up schema code when one kind of Entry’s children matches another kind of Entry’s children.
169 170 171 |
# File 'lib/wash/entry.rb', line 169 def children @child_klasses end |
.description(description) ⇒ Object
description is a class-level tag specifying the entry’s description. It is a helper for Entry schemas.
107 108 109 |
# File 'lib/wash/entry.rb', line 107 def description(description) @description = description end |
.is_singleton ⇒ Object
is_singleton is a class-level tag indicating that the given Entry’s a singleton. It is a helper for Entry schemas.
Note that if an Entry has the is_singleton tag and its name is not filled-in when that Entry is listed, then the Entry’s name will be set to the specified label. This means that plugin authors do not have to set singleton entries’ names, and it also enforces the convention that singleton entries’ labels should match their names.
99 100 101 |
# File 'lib/wash/entry.rb', line 99 def is_singleton @singleton = true end |
.label(lbl) ⇒ Object
label is a class-level tag specifying the entry’s label. It is a helper for Entry schemas.
80 81 82 |
# File 'lib/wash/entry.rb', line 80 def label(lbl) @label = lbl end |
.meta_attribute_schema(schema) ⇒ Object
meta_attribute_schema sets the meta attribute’s schema to schema. It is a helper for Entry schemas.
127 128 129 |
# File 'lib/wash/entry.rb', line 127 def (schema) = schema end |
.metadata_schema(schema) ⇒ Object
metadata_schema sets the metadata schema to schema. It is a helper for Entry schemas.
134 135 136 |
# File 'lib/wash/entry.rb', line 134 def (schema) = schema end |
.parent_of(child_klass, *child_klasses) ⇒ Object
parent_of indicates that this kind of Entry is the parent of the given child classes (i.e. child entries). It is a helper for Entry schemas.
152 153 154 155 |
# File 'lib/wash/entry.rb', line 152 def parent_of(child_klass, *child_klasses) @child_klasses ||= [] @child_klasses += [child_klass] + child_klasses end |
.signal(name, description) ⇒ Object
signal adds the given signal to the entry’s list of supported signals. It is a helper for Entry schemas.
113 114 115 |
# File 'lib/wash/entry.rb', line 113 def signal(name, description) add_signal_schema(name, '', description) end |
.signal_group(name, regex, description) ⇒ Object
signal_group adds the given signal group to the entry’s list of supported signals. It is a helper for Entry schemas.
119 120 121 |
# File 'lib/wash/entry.rb', line 119 def signal_group(name, regex, description) add_signal_schema(name, regex, description) end |
.slash_replacer(char) ⇒ Object
slash_replacer is a class-level tag that specifies the slash replacer. It should only be used if there is a chance that instances of the given class can contain a “#” in their names. Otherwise, slash_replacer should be ignored.
44 45 46 |
# File 'lib/wash/entry.rb', line 44 def slash_replacer(char) @slash_replacer = char end |
.state(field, *fields) ⇒ Object
state is a class-level tag that specifies the minimum state required to reconstruct all instances of this specific kind of entry. Each specified state field will be passed along to attr_accessor so that instances can get/set their values.
Note that Wash.run uses Class#allocate when it reconstructs the entries, so it does not call the initialize method.
71 72 73 74 |
# File 'lib/wash/entry.rb', line 71 def state(field, *fields) @state ||= [] @state += set_fields(field, *fields) end |
Instance Method Details
#cache_ttls(ttls = {}) ⇒ Object
cache_ttls sets the cache TTLs (time-to-live) of the given methods.
328 329 330 331 |
# File 'lib/wash/entry.rb', line 328 def cache_ttls(ttls = {}) @cache_ttls ||= {} @cache_ttls = @cache_ttls.merge(ttls) end |
#prefetch(method, *methods) ⇒ Object
prefetch indicates that the given methods should be prefetched. This means that the gem will invoke those methods on this particular entry instance and include their results when serializing that entry. Note that the methods are invoked during serialization.
310 311 312 |
# File 'lib/wash/entry.rb', line 310 def prefetch(method, *methods) prefetched_methods.concat([method] + methods) end |
#schema ⇒ Object
schema returns the entry’s schema. It should not be overridden.
334 335 336 337 338 |
# File 'lib/wash/entry.rb', line 334 def schema schemaHash = {} self.class.send(:schema, schemaHash) schemaHash end |
#to_json ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/wash/entry.rb', line 244 def to_json(*) unless @name && @name.size > 0 unless singleton raise "A nameless entry is being serialized. The entry is an instance of #{type_id}" end @name = label end hash = { type_id: type_id, name: @name, } # Include the methods hash[:methods] = self.class.send(:methods).map do |method| if prefetched_methods.include?(method) [method, self.send(method)] else method end end # Include the remaining keys. Note that these checks are here to # ensure that we don't serialize empty keys. They're meant to save # some space. if attributes.size > 0 && (attributes_hash = to_hash(attributes)) hash[:attributes] = attributes_hash end if cache_ttls.size > 0 hash[:cache_ttls] = cache_ttls end if slash_replacer.size > 0 hash[:slash_replacer] = slash_replacer end hash[:state] = to_hash(state).merge(klass: type_id, name: @name).to_json if Wash.send(:pretty_print?) JSON.pretty_generate(hash) else JSON.generate(hash) end end |
#type_id ⇒ Object
type_id returns the entry’s type ID, which is its fully-qualified class name.
288 289 290 |
# File 'lib/wash/entry.rb', line 288 def type_id self.class.send(:type_id) end |