Class: FoundationApi::Table::Record

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, Model::AttributeMethods, Model::Mapping, Request, Persistence
Defined in:
lib/foundation_api/table/record.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Persistence

#destroy, #destroy!, #map_attributes, #new_record?, #persisted?, #save, #save!

Methods included from Model::AttributeMethods

#[], #[]=, #method_missing

Constructor Details

#initialize(*args) ⇒ Record

Returns a new instance of Record.



127
128
129
130
131
132
133
134
# File 'lib/foundation_api/table/record.rb', line 127

def initialize(*args)
  attributes = args.extract_options!
  if args.first == :no_translate_attributes
    @attributes = HashWithIndifferentAccess.new attributes
  else
    @attributes = translate_attributes attributes
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class FoundationApi::Model::AttributeMethods

Class Method Details

.allObject



43
44
45
46
47
48
# File 'lib/foundation_api/table/record.rb', line 43

def all
  with_optional_caching :all do
    res = get_table_data(fully_qualified_table_name)
    res && instantiate_objects(res) 
  end
end

.find(*args) ⇒ Object



26
27
28
29
30
31
# File 'lib/foundation_api/table/record.rb', line 26

def find(*args)
  with_optional_caching args.first do
    res = get_table_data(fully_qualified_table_name, :filters => ["id = #{args.first}"])
    res && res.size > 0 ? instantiate_objects(res).first : nil
  end
end

.where(options) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/foundation_api/table/record.rb', line 33

def where(options)
  with_optional_caching :where, options do
    if options.is_a? Hash
      options = {:conditions => options }
    end
    res = get_table_data(fully_qualified_table_name, translate_options(options))
    res && instantiate_objects(res) 
  end or []
end

.with_optional_caching(*args) ⇒ Object

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/foundation_api/table/record.rb', line 50

def with_optional_caching(*args)
  raise ArgumentError, "You must specify block" unless block_given?
  if caching_enabled
    options = cache_expires ? {expires_in: cache_expires} : {}
    Rails.cache.fetch([self.name] + args, options) do
      yield
    end
  else
    yield
  end
end

Instance Method Details

#idObject



136
137
138
# File 'lib/foundation_api/table/record.rb', line 136

def id
  @id ||= self[:id].to_i
end