Class: SqlcachedClient::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/sqlcached_client/attachment.rb

Constant Summary collapse

PREDICATES =
['=', '<=', '>']

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, conditions, content) ⇒ Attachment

Returns a new instance of Attachment.

Parameters:

  • conditions (Hash)

    { var_1: ‘value 1’, var_2: ‘value 2’ }



12
13
14
15
16
# File 'lib/sqlcached_client/attachment.rb', line 12

def initialize(name, conditions, content)
  @name = name
  @conditions = conditions.with_indifferent_access
  @content = content
end

Class Attribute Details

.entity_classObject (readonly)

Returns the value of attribute entity_class.



20
21
22
# File 'lib/sqlcached_client/attachment.rb', line 20

def entity_class
  @entity_class
end

.variablesObject (readonly)

Returns the value of attribute variables.



20
21
22
# File 'lib/sqlcached_client/attachment.rb', line 20

def variables
  @variables
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



8
9
10
# File 'lib/sqlcached_client/attachment.rb', line 8

def conditions
  @conditions
end

#contentObject

Returns the value of attribute content.



9
10
11
# File 'lib/sqlcached_client/attachment.rb', line 9

def content
  @content
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/sqlcached_client/attachment.rb', line 8

def name
  @name
end

Class Method Details

.add_variable(variable_name, predicate) ⇒ Object Also known as: depends_on



22
23
24
25
26
# File 'lib/sqlcached_client/attachment.rb', line 22

def add_variable(variable_name, predicate)
  raise "Invalid predicate" if !PREDICATES.include?(predicate)
  @variables = [] if @variables.nil?
  @variables << OpenStruct.new(name: variable_name, predicate: predicate)
end

Instance Method Details

#entity_classObject



35
36
37
# File 'lib/sqlcached_client/attachment.rb', line 35

def entity_class
  self.class.entity_class
end

#entity_namespaceObject



39
40
41
# File 'lib/sqlcached_client/attachment.rb', line 39

def entity_namespace
  entity_class.try(:entity_namespace)
end

#to_query_formatObject



43
44
45
46
47
48
49
50
# File 'lib/sqlcached_client/attachment.rb', line 43

def to_query_format
  {
    name: external_name,
    condition_values: Hash[
      variables.map { |v| [v.name, conditions[v.name]] }
    ]
  }
end

#to_save_formatObject



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

def to_save_format
  {
    name: external_name,
    attachment: content,
    conditions: variables.map do |v|
      "#{v.name} #{v.predicate} #{conditions[v.name]}"
    end
  }
end

#variablesObject

class << self



31
32
33
# File 'lib/sqlcached_client/attachment.rb', line 31

def variables
  self.class.variables
end