Class: Coral::Event

Inherits:
Core show all
Defined in:
lib/coral_core/event.rb

Direct Known Subclasses

PuppetEvent, RegexpEvent

Instance Attribute Summary collapse

Attributes inherited from Core

#ui

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core

#inspect, logger, #logger, logger=, ui

Methods inherited from Config

#[], #[]=, array, #array, #clear, #defaults, #delete, ensure, filter, #filter, #get, #get_array, #get_hash, hash, #hash, #import, #init, init, init_flat, #set, string, #string, string_map, #string_map, #symbol, symbol, #symbol_map, symbol_map, test, #test

Methods included from Mixin::ConfigOptions

#clear_options, #contexts, #get_options, #set_options

Methods included from Mixin::ConfigCollection

#all_properties, #clear_properties, #delete_property, #get_property, #save_properties

Methods included from Mixin::Lookup

#hiera, #hiera_config, #initialized?, #lookup, #lookup_array, #lookup_hash, #normalize

Methods included from Mixin::ConfigOps

#parse

Constructor Details

#initialize(options = {}) ⇒ Event




43
44
45
46
47
48
49
50
51
# File 'lib/coral_core/event.rb', line 43

def initialize(options = {})
  config = Config.ensure(options)
  
  super(config)
  
  @name       = config.get(:name, '')
  @delegate   = config.get(:delegate, nil)  
  @properties = config.options
end

Instance Attribute Details

#nameObject


Property accessors / modifiers



56
57
58
# File 'lib/coral_core/event.rb', line 56

def name
  @name
end

Class Method Details

.build_info(data = {}) ⇒ Object




144
145
146
# File 'lib/coral_core/event.rb', line 144

def self.build_info(data = {})
  return build_info!(data)
end

.build_info!(data = {}) ⇒ Object


Utilities



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/coral_core/event.rb', line 109

def self.build_info!(data = {})  
  events = []
  
  if data.is_a?(String)
    data = data.split(/\s*,\s*/)
  elsif data.is_a?(Hash)
    data = [ data ]
  end
  
  if data.is_a?(Array)
    data.each do |element|
      event = {}
      
      if block_given?
        event = yield(element)
      else
        case element        
        when String
          event = split_event_string(element)
              
        when Hash          
          event = element
        end
      end
      
      unless event.empty?
        events << event
      end
    end
  end
  return events
end

.create(type, info) ⇒ Object




150
151
152
153
154
155
156
157
# File 'lib/coral_core/event.rb', line 150

def self.create(type, info)
  event = nil
  begin
    event = Module.const_get("Coral").const_get("#{type.capitalize}Event").new(info)
  rescue
  end
  return event
end

.instance(options = {}, build_hash = false, keep_array = false) ⇒ Object




37
38
39
# File 'lib/coral_core/event.rb', line 37

def self.instance(options = {}, build_hash = false, keep_array = false)
  return instance!(options, build_hash, keep_array)
end

.instance!(data = {}, build_hash = false, keep_array = false) ⇒ Object


Constructor / Destructor



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/coral_core/event.rb', line 8

def self.instance!(data = {}, build_hash = false, keep_array = false)
  group  = ( build_hash ? {} : [] )    
  events = build_info(data)
  
  index = 1
  events.each do |info|
    type = info[:type]
    
    if type && ! type.empty?
      event = ( block_given? ? yield(type, info) : create(type, info) )
              
      if event
        if build_hash
          group[index] = event
        else
          group << event
        end
      end
    end
    index += 1
  end
  if ! build_hash && events.length == 1 && ! keep_array
    return group.shift
  end
  return group
end

.split_event_string(data) ⇒ Object




161
162
163
164
165
166
167
168
# File 'lib/coral_core/event.rb', line 161

def self.split_event_string(data)
  info          = {}    
  components    = data.split(':')
  info[:type]   = components.shift
  info[:string] = components.join(':')
  
  return info
end

Instance Method Details

#check(source) ⇒ Object


Event handling



102
103
104
# File 'lib/coral_core/event.rb', line 102

def check(source)
  return false
end

#exportObject


Import / Export



95
96
97
# File 'lib/coral_core/event.rb', line 95

def export
  return type
end

#property(name, default = '', format = false) ⇒ Object




75
76
77
78
79
80
81
# File 'lib/coral_core/event.rb', line 75

def property(name, default = '', format = false)
  name = name.to_sym
  
  property = default
  property = filter(@properties[name], format) if @properties.has_key?(name)
  return property  
end

#set_properties(data) ⇒ Object




66
67
68
69
70
71
# File 'lib/coral_core/event.rb', line 66

def set_properties(data)
  return @delegate.set_properties(data) if @delegate
  
  @properties = hash(data)
  return self
end

#set_property(name, value) ⇒ Object




85
86
87
88
89
90
# File 'lib/coral_core/event.rb', line 85

def set_property(name, value)
  return @delegate.set_property(name, value) if @delegate
  
  @properties[name] = value
  return self
end

#typeObject




60
61
62
# File 'lib/coral_core/event.rb', line 60

def type
  return string(@properties[:type])
end