Module: FlexibleApi::ClassMethods

Defined in:
lib/flexible_api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#levelsObject

Returns the value of attribute levels.



19
20
21
# File 'lib/flexible_api.rb', line 19

def levels
  @levels
end

Instance Method Details

#default_request_level(level_name) ⇒ Object



56
57
58
# File 'lib/flexible_api.rb', line 56

def default_request_level(level_name)
  @default_request_level_name = level_name
end

#define_request_level(name, &block) ⇒ Object

Define a request level for this class Takes a name, and a block which defined the request level



27
28
29
30
31
32
# File 'lib/flexible_api.rb', line 27

def define_request_level(name, &block)
  level = RequestLevel.new(name, self)
  level.instance_eval &block
  @levels ||= {}
  @levels[name] = level
end

#find_all_hash(options = {}) ⇒ Object

Find all of an element (or association) and load it at the given request level



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/flexible_api.rb', line 43

def find_all_hash(options = {})
  options.assert_valid_keys(:request_level)
  level = find_level(options[:request_level])

  query = self
  level.scope_field.each do |s, args|
    query = args.nil? ? query.send(s) : query = query.send(s, *args)
  end

  records = query.all(:select => level.select_field.join(', '), :include => level.include_field)
  records.map { |r| level.receive(r) }
end

#find_hash(id, options = {}) ⇒ Object

Find a single element and load it at the given request level



35
36
37
38
39
40
# File 'lib/flexible_api.rb', line 35

def find_hash(id, options = {})
  options.assert_valid_keys(:request_level)
  level = find_level(options[:request_level])
  record = self.find(id, :select => level.select_field.join(', '), :include => level.include_field)
  level.receive record
end

#find_level(name = nil) ⇒ Object

Find a given level by name and return the request level



61
62
63
64
65
66
67
# File 'lib/flexible_api.rb', line 61

def find_level(name = nil)
  @levels ||= {}
  level = name.nil? ? load_default_request_level : @levels[name.to_sym]
  level = superclass.find_level(name) and @levels[name.to_sym] = level if level.nil? && superclass.present?
  raise NoSuchRequestLevelError.new(name, self.name) if level.nil?
  level
end

#request_levelsObject



21
22
23
# File 'lib/flexible_api.rb', line 21

def request_levels
  @levels.nil? ? [] : @levels.keys
end