Module: Reportly::ConsoleMethods

Extended by:
ConsoleMethods
Included in:
ConsoleMethods
Defined in:
lib/reportly/console_methods.rb

Defined Under Namespace

Classes: ReportlyNotValid

Instance Method Summary collapse

Instance Method Details

#is_valid_klass?(model) ⇒ Boolean

accepts Array or a ActiveRecord_Relation Should respond_to? :each and be descend from active record

Returns:

  • (Boolean)


50
51
52
# File 'lib/reportly/console_methods.rb', line 50

def is_valid_klass?(model)
  model.first.class.descends_from_active_record? rescue false and  model.respond_to? :each
end

#make_array_for(model) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/reportly/console_methods.rb', line 23

def make_array_for(model)
  # report User.first
  # 2.1.3 :003 >   User.first.class
  #  => User(id: uuid, partner_id: uuid, created_at: datetime, updated_at: datetime)
  model = [model] if model.class.superclass == ActiveRecord::Base
  # report User
  # 2.1.3 :004 > User.class
  #  => Class
  model = model.send(:all) if model.class.name == 'Class' and model.respond_to? :all
  
  # User.all.first(2) and User.where(name: 'yannis') are kind of arrays and respond to :each
  
  # 2.1.3 :006 > User.first(2).class
  #  => Array
   
  # 2.1.3 :013 > User.all.class
  #  => User::ActiveRecord_Relation
  model
  
end

#report(model, *fields) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/reportly/console_methods.rb', line 9

def report(model, *fields)
  
  model = make_array_for(model)
  
  validate(model)
  report = Reportly::Engine.report(model, *fields)
  @results = report
  puts report.join("\n")
end

#resultsObject



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

def results
  @results
end

#validate(model) ⇒ Object

Raises:



44
45
46
# File 'lib/reportly/console_methods.rb', line 44

def validate(model)
  raise ReportlyNotValid, "Reportly accepts only ActiveRecord Objects" unless is_valid_klass?(model)
end