Class: CSDL::InteractionFilterProcessor

Inherits:
Processor
  • Object
show all
Defined in:
lib/csdl/interaction_filter_processor.rb

Overview

InteractionFilterProcessor is a class that inherits from Processor, providing additional methods for building CSDL specifically for Interaction Filters.

Additional DSL methods provide the return statement, curly brace scopes (statement scopes), and VEDO tagging.

Examples:

nodes = CSDL::Builder.new.root do
  [
    tag_tree(%w(movies), "Video") {
      condition("links.url", :any, "youtube.com,vimeo.com")
    },
    tag_tree(%w(movies), "Social Networks") {
      condition("links.url", :any, "twitter.com,facebook.com")
    },

    return {
      _or {
        [
          condition("fb.topics.category", :in, "Movie,Film,TV"),
          condition("fb.parent.topics.category", :in, "Movie,Film,TV")
        ]
      }
    }
  ]
end
CSDL::InteractionFilterProcessor.new.process(nodes) # => 'tag.movies "Video" {links.url any "youtube.com,vimeo.com"} tag.movies "Social Networks" {links.url any "twitter.com,facebook.com"} return {fb.topics.category in "Movie,Film,TV" OR fb.parent.topics.cateogry in "Movie,Film,TV"}'

See Also:

Instance Method Summary collapse

Methods inherited from Processor

#on_and, #on_argument, #on_condition, #on_logical_group, #on_not, #on_operator, #on_or, #on_raw, #on_root, #on_string, #on_target

Instance Method Details

#on_return(node) ⇒ String

Generate a return statement by processing the child statement_scope node.

Examples:

node = s(:return,
         s(:statement_scope,
          s(:string, "foo")))
CSDL::InteractionFilterProcessor.new.process(node) # => 'return {"foo"}'

Raises:


52
53
54
55
56
57
58
59
60
# File 'lib/csdl/interaction_filter_processor.rb', line 52

def on_return(node)
  statement_scope = node.children.compact.find { |child| child.type == :statement_scope }

  if statement_scope.nil?
    fail ::CSDL::MissingReturnStatementScopeError, "Invalid CSDL AST: return statment scope is missing"
  end

  "return #{process(statement_scope)}"
end

#on_statement_scope(node) ⇒ String

Wrap child nodes in braces. Generally not useful on its own, see #on_return or #on_tag for integrated usage.

Examples:

node = s(:statement_scope,
        s(:string, "foo"))
CSDL::InteractionFilterProcessor.new.process(node) # => '{"foo"}'

See Also:


76
77
78
# File 'lib/csdl/interaction_filter_processor.rb', line 76

def on_statement_scope(node)
  "{" + process_all(node.children.compact).join(" ") + "}"
end

#on_tag(node) ⇒ String

Process :tag node with it's child nodes :tag_namespaces (optional), :tag_class, and :statement_scope.

Examples:

Tag Classification

node = s(:tag,
        s(:tag_class,
         s(:string, "MyTag")),
        s(:statement_scope,
         s(:string, "foo")))
CSDL::InteractionFilterProcessor.new.process(node) # => 'tag "MyTag" {"foo"}'

Tag Tree Classification

node = s(:tag,
        s(:tag_namespaces,
         s(:tag_namespace, "foo"),
         s(:tag_namespace, "bar"),
         s(:tag_namespace, "baz")),
        s(:tag_class,
         s(:string, "MyTag")),
        s(:statement_scope,
         s(:string, "value")))
CSDL::InteractionFilterProcessor.new.process(node) # => 'tag.foo.bar.baz "MyTag" {"value"}'

Raises:


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/csdl/interaction_filter_processor.rb', line 109

def on_tag(node)
  children = node.children.compact
  tag_namespaces  = children.find { |child| child.type == :tag_namespaces }
  tag_class       = children.find { |child| child.type == :tag_class }
  statement_scope = children.find { |child| child.type == :statement_scope }

  if tag_class.nil?
    fail ::CSDL::MissingTagClassError, "Invalid CSDL AST: :tag node must have a :tag_class child node"
  end

  if statement_scope.nil?
    fail ::CSDL::MissingTagStatementScopeError, "Invalid CSDL AST: :tag node must have a :statement_scope child node"
  end

  tag_namespace = "tag"
  unless tag_namespaces.nil?
    tag_namespace += process(tag_namespaces)
  end

  children = [tag_namespace] + process_all([ tag_class, statement_scope ])
  children.join(" ")
end

#on_tag_class(node) ⇒ String

Process the first child of the :tag_class node.

See Also:


140
141
142
# File 'lib/csdl/interaction_filter_processor.rb', line 140

def on_tag_class(node)
  process(node.children.first)
end

#on_tag_namespace(node) ⇒ String

Process the terminal value of the :tag_namespace node.

See Also:


152
153
154
# File 'lib/csdl/interaction_filter_processor.rb', line 152

def on_tag_namespace(node)
  node.children.first.to_s
end

#on_tag_namespaces(node) ⇒ String

Process the :tag_namespace child nodes of a :tag_namespaces node.

Examples:

node = s(:tag_namespaces,
        s(:tag_namespace, "foo"),
        s(:tag_namespace, "bar"),
        s(:tag_namespace, "baz"))
CSDL::InteractionFilterProcessor.new.process(node) # => '.foo.bar.baz'

Raises:


171
172
173
174
175
176
177
178
179
# File 'lib/csdl/interaction_filter_processor.rb', line 171

def on_tag_namespaces(node)
  child_tag_namespaces = node.children.compact.select { |child| child.type == :tag_namespace }

  if child_tag_namespaces.empty?
    fail ::CSDL::MissingTagNodesError, "Invalid CSDL AST: A :tag_namespaces node must have at least one :tag_namespace child"
  end

  "." + process_all(child_tag_namespaces).join(".")
end

#validate_target!(target_key) ⇒ void

This method returns an undefined value.

Raises an CSDL::InvalidInteractionTargetError if the target isn't a valid CSDL target for interaction filters. Will be called from the base class when given a :condition node with a :target node.

Examples:

CSDL::InteractionFilterProcessorProcessor.new.validate_target!("fake") # => raises InvalidInteractionTargetError

Raises:

See Also:


196
197
198
199
200
# File 'lib/csdl/interaction_filter_processor.rb', line 196

def validate_target!(target_key)
  unless ::CSDL.interaction_target?(target_key)
    fail ::CSDL::InvalidInteractionTargetError, "Interaction filters cannot use target '#{target_key}'"
  end
end