Module: Conversational::Conversation
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/conversational/conversation.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
- .exclude(classes) ⇒ Object
- .find_subclass_by_topic(topic, options = {}) ⇒ Object
- .topic_defined?(topic) ⇒ Boolean
- .topic_subclass_name(topic) ⇒ Object
Instance Method Summary collapse
-
#details(options = {}) ⇒ Object
Returns the specific sublass of conversation based from the topic Example:.
- #topic_defined? ⇒ Boolean
Class Method Details
.exclude(classes) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/conversational/conversation.rb', line 208 def self.exclude(classes) if classes if classes.is_a?(Array) classes.each do |class_name| (class_name) end else (classes) end end @@excluded_classes = classes end |
.find_subclass_by_topic(topic, options = {}) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/conversational/conversation.rb', line 181 def self.find_subclass_by_topic(topic, = {}) subclass = nil if topic.nil? || topic.empty? unless [:exclude_blank_unknown] subclass = blank_topic_subclass.constantize if blank_topic_subclass end else project_class_name = self.topic_subclass_name(topic) begin project_class = project_class_name.constantize rescue project_class = nil end # the subclass has been defined # check that it is a subclass klass if project_class && project_class <= parent && ([:include_all] || !self.exclude?(project_class)) subclass = project_class else unless [:exclude_blank_unknown] subclass = unknown_topic_subclass.constantize if unknown_topic_subclass end end end subclass end |
.topic_defined?(topic) ⇒ Boolean
174 175 176 177 178 179 |
# File 'lib/conversational/conversation.rb', line 174 def self.topic_defined?(topic) self.find_subclass_by_topic( topic, :exclude_blank_unknown => true ) end |
.topic_subclass_name(topic) ⇒ Object
221 222 223 |
# File 'lib/conversational/conversation.rb', line 221 def self.topic_subclass_name(topic) topic.camelize + (class_suffix || parent).to_s end |
Instance Method Details
#details(options = {}) ⇒ Object
Returns the specific sublass of conversation based from the topic Example:
<tt>
Class Conversation
include Conversational::Conversation
end
Class HelloConversation < Conversation
end
Class GoodbyeConversation < Conversation
end
hello = Conversation.new("someone", "hello")
hello.details => #<HelloConversation topic: "hello", with: "someone">
unknown = Conversation.new("someone", "cheese")
unknown.details => nil
Conversation.unknown_topic_subclass = HelloConversation
unknown = Conversation.new("someone", "cheese")
unknown.details => #<HelloConversation topic: "cheese", with: "someone">
blank = Conversation.new("someone")
blank.details => nil
Conversation.blank_topic_subclass = GoodbyeConversation
blank = Conversation.new("someone")
blank.details => #<GoodbyeConversation topic: nil, with: "someone">
Conversation.exclude HelloConversation
hello = Conversation.new("someone", "hello")
hello.details => nil
hello.details(:include_all => true) => #<HelloConversation topic: "hello", with: "someone">
</tt>
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/conversational/conversation.rb', line 62 def details( = {}) details_subclass = Conversational::Conversation.find_subclass_by_topic( topic, ) if details_subclass self.respond_to?(:becomes) ? becomes(details_subclass) : Conversational::Conversation.becomes( details_subclass, self ) end end |
#topic_defined? ⇒ Boolean
75 76 77 |
# File 'lib/conversational/conversation.rb', line 75 def topic_defined? details_subclass = Conversational::Conversation.topic_defined?(topic) end |