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

.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

#to_query_formatObject



35
36
37
38
39
40
41
42
# File 'lib/sqlcached_client/attachment.rb', line 35

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

#to_save_formatObject



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

def to_save_format
  {
    name: 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