Module: Opium::Model::Persistable::ClassMethods

Defined in:
lib/opium/model/persistable.rb

Instance Method Summary collapse

Instance Method Details

#add_header_to(methods, header, value, options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/opium/model/persistable.rb', line 33

def add_header_to( methods, header, value, options = {} )
  Array( methods ).each do |method|
    added_headers[method] = { header: header, value: value, options: options }
    
    added_headers[method][:options][:only] = Array( options[:only] )
    added_headers[method][:options][:except] = Array( options[:except] )
  end
  nil
end

#added_headersObject



29
30
31
# File 'lib/opium/model/persistable.rb', line 29

def added_headers
  @added_headers ||= {}.with_indifferent_access
end

#create(attributes = {}) ⇒ Object



13
14
15
# File 'lib/opium/model/persistable.rb', line 13

def create( attributes = {} )
  new( attributes ).tap {|model| model.save}
end

#create!(attributes = {}) ⇒ Object



17
18
19
# File 'lib/opium/model/persistable.rb', line 17

def create!( attributes = {} )
  new( attributes ).tap {|model| model.save!}
end

#delete_all(query = nil) ⇒ Object



25
26
27
# File 'lib/opium/model/persistable.rb', line 25

def delete_all( query = nil )
  
end

#destroy_all(query = nil) ⇒ Object



21
22
23
# File 'lib/opium/model/persistable.rb', line 21

def destroy_all( query = nil )
  
end

#get_header_for(method, context, owner = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/opium/model/persistable.rb', line 43

def get_header_for( method, context, owner = nil )
  return {} unless added_headers[method]
  
  eval_only = !added_headers[method][:options][:only].empty?
  eval_except = !added_headers[method][:options][:except].empty?
  
  within_only = added_headers[method][:options][:only].include?( context )
  within_except = added_headers[method][:options][:except].include?( context )
  
  value = added_headers[method][:value]
  value = value.call( owner ) if owner && value.respond_to?( :call )
  
  if value && ( ( !eval_only && !eval_except ) || ( eval_only && within_only ) || ( eval_except && !within_except ) )
    { headers: { added_headers[method][:header] => value } } 
  else
    {}
  end
end