Class: TheGrid::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/the_grid/api.rb

Defined Under Namespace

Classes: Command

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation) ⇒ Api

Returns a new instance of Api.



5
6
7
8
# File 'lib/the_grid/api.rb', line 5

def initialize(relation)
  @relation = relation
  @options = { :delegated_commands => {} }
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/the_grid/api.rb', line 3

def options
  @options
end

#relationObject (readonly)

Returns the value of attribute relation.



3
4
5
# File 'lib/the_grid/api.rb', line 3

def relation
  @relation
end

Instance Method Details

#compose!(params) ⇒ Object



14
15
16
17
18
# File 'lib/the_grid/api.rb', line 14

def compose!(params)
  configure(params).fetch(:cmd).each do |cmd|
    @relation = run_command!(cmd, params) unless command(cmd).batch?
  end
end

#delegate(commands) ⇒ Object



10
11
12
# File 'lib/the_grid/api.rb', line 10

def delegate(commands)
  options[:delegated_commands].merge! commands.stringify_keys
end

#run_command!(name, params) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/the_grid/api.rb', line 20

def run_command!(name, params)
  @options.merge! command(name).contextualize(@relation, params)

  if command_delegated?(name)
    assoc_name = options[:delegated_commands][name.to_s]
    assoc = @relation.reflections[assoc_name].klass.scoped
    @relation.merge command(name).execute_on(assoc, params)
  else
    command(name).execute_on(@relation, params)
  end
end