Class: SData::Predicate

Inherits:
Struct
  • Object
show all
Defined in:
lib/s_data/predicate.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fieldObject

Returns the value of attribute field

Returns:

  • (Object)

    the current value of field



2
3
4
# File 'lib/s_data/predicate.rb', line 2

def field
  @field
end

#relationObject

Returns the value of attribute relation

Returns:

  • (Object)

    the current value of relation



2
3
4
# File 'lib/s_data/predicate.rb', line 2

def relation
  @relation
end

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



2
3
4
# File 'lib/s_data/predicate.rb', line 2

def value
  @value
end

Class Method Details

.parse(fields_map, predicate_string) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/s_data/predicate.rb', line 3

def self.parse(fields_map, predicate_string)
  # Gotcha on Rightscale that escaped + before it hits controller, but doesn't escape other special chars!
  escaped_predicate_string = CGI::unescape(predicate_string.gsub('+', '%2B')) 
  match_data = escaped_predicate_string.match(/(\w+)\s(gt|lt|eq|ne|lteq|gteq)\s('?.+'?|'')/) || []

  canonical_field_name = match_data[1].underscore.to_sym unless match_data[1].nil?
  self.new fields_map[canonical_field_name], match_data[2], strip_quotes(match_data[3])
end

.strip_quotes(value) ⇒ Object



12
13
14
15
16
17
# File 'lib/s_data/predicate.rb', line 12

def self.strip_quotes(value)
  return value unless value.is_a?(String)
  value = value.gsub("%27", "'")
  return value unless value =~ /'.*?'/
  return value[1,value.length-2]
end

Instance Method Details

#to_conditionsObject



19
20
21
22
23
24
25
# File 'lib/s_data/predicate.rb', line 19

def to_conditions      
  if valid?
    ConditionsBuilder.build_conditions field, relation.to_sym, value
  else
    raise Sage::BusinessLogic::Exception::IncompatibleDataException, "Invalid predicate string"
  end
end

#valid?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/s_data/predicate.rb', line 27

def valid?
  field && relation && value
end