Module: Riaction::Riaction::ClassMethods

Defined in:
lib/riaction/riaction.rb

Instance Method Summary collapse

Instance Method Details

#establish_riactionary_classObject

Patches a class to turn it into a riactionary class, providing some default checks and attributes



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/riaction/riaction.rb', line 43

def establish_riactionary_class
  class << self

    # returns true if a class has riaction setup
    def riactionary?
      true
    end
    
    def riactionless?
      @riactionless ||= false
    end
    
    def riaction_profile_keys
      @riaction_profile_keys ||= {}
    end
    
    def riaction_events
      @riaction_events ||= {}
    end
    
    def riaction_options
      @riaction_options ||= ::Riaction::Constants.riaction_options
    end
    
    def riaction_use_profile
      @riaction_use_profile ||= nil
    end
    
    def riactionless(&block)
      if block_given?
        @riactionless = true
        begin
          block_value = yield
        ensure
          @riactionless = false
        end
      end
    end
    
    def reset_riaction
      riaction_profile_keys.clear
      riaction_events.clear
      riaction_options.merge!(::Riaction::Constants.riaction_options)
      @riaction_use_profile = nil
    end
    
    def add_or_update_riaction_profile(type, opts)
      display_name = opts.delete(:display_name) || nil
      riaction_check_type(:display_name, display_name, [Symbol, Proc, NilClass])
      unless opts.keys.any?{|type| ::Riaction::Constants.supported_identifier_types.include?(type)}
        raise ConfigurationError.new("#{self.to_s} defining a riaction profile must use supported IActionable types: #{::Riaction::Constants.supported_identifier_types.map(&:to_s).join(", ")}")
      end
      riaction_profile_keys.store(type, {
        :display_name => display_name,
        :identifiers => opts
      })
      @riaction_use_profile ||= type
    end

    def add_or_update_riaction_event(name, opts)
      # set values
      trigger = opts.delete(:trigger) || :create
      profile = opts.delete(:profile)
      profile_type = opts.delete(:profile_type)
      params = opts.delete(:params) || {}
      guard = opts.delete(:if) || opts.delete(:guard) || true
      # check for required types and presence 
      if profile.nil?
        raise ConfigurationError.new("#{self.to_s} defining a riaction event must provide a profile")
      end
      riaction_check_type(:trigger, trigger, [Symbol])
      riaction_check_type(:profile, profile, [Symbol, Proc])
      riaction_check_type(:profile_type, profile_type, [Symbol, NilClass])
      riaction_check_type(:params, params, [Symbol, Proc, Hash])
      riaction_check_type(:guard, guard, [Symbol, Proc, TrueClass])
      # store our event data
      riaction_events.store(name, {
        :trigger => trigger,
        :profile => profile,
        :profile_type => profile_type,
        :params => params,
        :guard => guard
      })
      # create necessary callbacks and instance methods for triggers
      if ::Riaction::Constants.crud_actions.include? trigger
        send "after_#{trigger}".to_sym, ::Riaction::CrudEventCallback.new(name)
      else
        define_method("trigger_#{trigger}!") do
          if self.riaction_log_event?(name) && !self.class.riactionless?
            Resque.enqueue(::Riaction::EventPerformer, name, self.class.base_class.to_s, self.id)
          end
        end
      end
    end
    
    def riaction_profile_types_defined
      riaction_profile_keys.size
    end
    
    def riaction_events_defined
      riaction_events.size
    end
    
    def riaction_check_type(name, value, allowed_types)
      unless allowed_types.any?{|type| value.is_a?(type)} 
        raise ConfigurationError.new("value given for #{name} must be of types: #{allowed_types.map(&:to_s).join(', ')}")
      end
    end
  end
  
  include ::Riaction::Riaction::InstanceMethods
end

#establish_riactionary_event_classObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/riaction/riaction.rb', line 170

def establish_riactionary_event_class
  (::Riaction::Riaction::EVENT_CLASSES << self.to_s).uniq!

  class << self
    def riaction_events?
      true
    end
    
    def riaction_defines_event?(event_name)
      riaction_events.has_key? event_name
    end
  end

  include ::Riaction::Riaction::Event::InstanceMethods
end

#establish_riactionary_profile_classObject



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/riaction/riaction.rb', line 156

def establish_riactionary_profile_class
  (::Riaction::Riaction::PROFILE_CLASSES << self.to_s).uniq!

  class << self
    def riaction_profile?
      true
    end
  end

  include ::Riaction::Riaction::Profile::InstanceMethods

  after_create ::Riaction::ProfileCreationCallback.new
end

#riaction(type, opts) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/riaction/riaction.rb', line 20

def riaction(type, opts)
  establish_riactionary_class unless riactionary?
  if type == :profile
    establish_riactionary_profile_class unless riaction_profile?
    add_or_update_riaction_profile(opts.delete(:type), opts)
  elsif type == :event
    establish_riactionary_event_class unless riaction_events?
    add_or_update_riaction_event(opts.delete(:name), opts)
  elsif type == :option || type == :options
    opts.each_pair do |option, value|
      if ::Riaction::Constants.riaction_options.has_key?(option)
        # merge hashes, replace all else;
        if riaction_options[option].is_a? Hash
          riaction_options[option].merge! value
        else
          riaction_options[option] = value
        end
      end
    end
  end
end

#riaction_events?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/riaction/riaction.rb', line 194

def riaction_events?
  false
end

#riaction_profile?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/riaction/riaction.rb', line 190

def riaction_profile?
  false
end

#riactionary?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/riaction/riaction.rb', line 186

def riactionary?
  false
end