Class: FlexibleApi::RequestLevel

Inherits:
Object
  • Object
show all
Defined in:
lib/flexible_api/request_level.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, klass) ⇒ RequestLevel

Returns a new instance of RequestLevel.



5
6
7
8
9
10
11
12
13
14
# File 'lib/flexible_api/request_level.rb', line 5

def initialize(name, klass)
  @name = name
  @klass = klass
  @display_fields = Set.new
  @select_fields = Set.new
  @includes = []
  @notations = {}
  @eaten_levels = []
  @select_fields << "`#{@klass.table_name}`.#{@klass.primary_key}" # auto-select primary key
end

Instance Attribute Details

#display_fieldsObject (readonly)

Returns the value of attribute display_fields.



16
17
18
# File 'lib/flexible_api/request_level.rb', line 16

def display_fields
  @display_fields
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/flexible_api/request_level.rb', line 16

def name
  @name
end

#notationsObject (readonly)

Returns the value of attribute notations.



16
17
18
# File 'lib/flexible_api/request_level.rb', line 16

def notations
  @notations
end

Instance Method Details

#all_fieldsObject



62
63
64
# File 'lib/flexible_api/request_level.rb', line 62

def all_fields
  fields *@klass.columns_hash.keys.map(&:to_sym)
end

#display_fieldObject



88
89
90
91
92
93
94
# File 'lib/flexible_api/request_level.rb', line 88

def display_field
  @display_field_array ||= begin
    displays = @display_fields.to_a
    @eaten_levels.each { |l| displays.concat l.display_field }
    displays
  end
end

#eat_level(name) ⇒ Object



28
29
30
# File 'lib/flexible_api/request_level.rb', line 28

def eat_level(name)
  @eaten_levels << @klass.find_level(name)
end

#fields(*field_array) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/flexible_api/request_level.rb', line 66

def fields(*field_array)
  field_array.each do |field|
    if field.is_a?(String)
      @display_fields << field.split('.').last.to_sym
      @select_fields << field
    else
      @display_fields << field
      @select_fields << "`#{@klass.table_name}`.#{field}" if @klass.columns_hash.keys.include?(field.to_s)
    end
  end
end

#include_fieldObject



96
97
98
99
100
101
102
# File 'lib/flexible_api/request_level.rb', line 96

def include_field
  @include_field_array ||= begin
    includes = @includes.map { |i| i[:association].name }
    @eaten_levels.each { |l| includes.concat l.include_field }
    includes
  end
end

#includes(association_name, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/flexible_api/request_level.rb', line 38

def includes(association_name, options = {})
  options.assert_valid_keys :request_level, :as, :requires
  association = @klass.reflect_on_all_associations.detect { |a| a.name == association_name.to_sym }
  raise "No such association on #{@klass.name}: #{association_name}" if association.nil? # TODO
  # Allow requires to pass in
  requires *options[:requires] if options.has_key?(:requires)
  # Set the include options
  @includes << { 
    :name => options[:as] || association_name,
    :association => association, 
    :request_level => association.klass.find_level(options[:request_level])
  }
end

#notation(notation_name, options = {}, &block) ⇒ Object



32
33
34
35
36
# File 'lib/flexible_api/request_level.rb', line 32

def notation(notation_name, options = {}, &block)
  options.assert_valid_keys :requires
  requires *options[:requires]
  @notations[notation_name] = block
end

#receive(item) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/flexible_api/request_level.rb', line 104

def receive(item)
  return nil if item.nil? # method may be nil
  attributes = {}
  @eaten_levels.each do |level|
    attributes.merge! level.receive(item)
  end
  @display_fields.each do |field|
    attributes[field] = item.send(field)
  end
  @includes.each do |include|
    value = item.send(include[:association].name)
    value = value.is_a?(Enumerable) ? value.map { |e| include[:request_level].receive(e) } : include[:request_level].receive(value)
    attributes[include[:name]] = value
  end
  @notations.each do |name, block|
    attributes[name] = item.instance_eval(&block)
  end
  attributes
end

#requires(*requires_array) ⇒ Object



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

def requires(*requires_array)
  requires_array.each do |field|
    if field.is_a?(String)
      @select_fields << field
    else
      @select_fields << "`#{@klass.table_name}`.#{field}" if @klass.columns_hash.keys.include?(field.to_s)
    end
  end
end

#select_fieldObject



80
81
82
83
84
85
86
# File 'lib/flexible_api/request_level.rb', line 80

def select_field
  @select_field_array ||= begin
    selects = @select_fields.to_a
    @eaten_levels.each { |l| selects.concat l.select_field }
    selects
  end
end

#to_hashObject



18
19
20
21
22
23
24
25
26
# File 'lib/flexible_api/request_level.rb', line 18

def to_hash
  {
    :name => @name,
    :fields => display_field + notations.keys,
    :includes => @includes.map do |inc|
      { :name => inc[:name], :type => inc[:association].name.to_s.pluralize.underscore.downcase, :request_level => inc[:request_level].name }
    end
  }
end