Class: Birddog::Birddog

Inherits:
Object
  • Object
show all
Includes:
FieldConditions
Defined in:
lib/birddog/birddog.rb

Constant Summary collapse

AREL_KEYS =
[:select, :limit, :joins, :includes, :group, :from, :where, :having]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FieldConditions

#cast_numeric, #conditionally_scoped, #conditions_for_date, #conditions_for_numeric, #conditions_for_string_search, #regexed?

Constructor Details

#initialize(model) ⇒ Birddog

Returns a new instance of Birddog.



15
16
17
18
19
20
21
22
23
24
# File 'lib/birddog/birddog.rb', line 15

def initialize(model)
  @model = model
  @fields = {}
  @averagable = []
  @minimumable = []
  @maximumable = []
  @sumable = []

  define_common_aggregates_for(model)
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



13
14
15
# File 'lib/birddog/birddog.rb', line 13

def fields
  @fields
end

Instance Method Details

#aggregatable(*fields) ⇒ Object



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

def aggregatable(*fields)
  fields.flatten.compact.uniq.each do |agg_field|
    averagable(agg_field)
    minimumable(agg_field)
    maximumable(agg_field)
    sumable(agg_field)
  end
end

#aggregate?(name) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/birddog/birddog.rb', line 43

def aggregate?(name)
  @fields[name].fetch(:aggregate, false)
end

#alias_field(name, other_field) ⇒ Object



47
48
49
50
# File 'lib/birddog/birddog.rb', line 47

def alias_field(name, other_field)
  @fields[name] = @fields[other_field]
  aggregate?(name) ? define_aggregate_field(name, @fields[name]) : define_field(name, @fields[name])
end

#averagable(*fields) ⇒ Object



61
62
63
64
65
# File 'lib/birddog/birddog.rb', line 61

def averagable(*fields)
  fields.flatten.compact.uniq.each do |avg_field|
    @averagable << avg_field unless @averagable.include?(avg_field)
  end
end

#field(name, options = {}, &mapping) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/birddog/birddog.rb', line 26

def field(name, options={}, &mapping)
  @fields[name] = field = {
    :attribute       => field_attribute(name, options.fetch(:attribute, nil), options.fetch(:aggregate, nil)),
    :cast            => options.fetch(:cast, false),
    :type            => options.fetch(:type, :string),
    :case_sensitive  => options.fetch(:case_sensitive, true),
    :match_substring => options.fetch(:match_substring, false),
    :regex           => options.fetch(:regex, false),
    :wildcard        => options.fetch(:wildcard, false),
    :aggregate       => options.fetch(:aggregate, false),
    :options         => options.select { |k,v| AREL_KEYS.include?(k) }, 
    :mapping         => mapping || lambda{ |v| v }
  }

  aggregate?(name) ? define_aggregate_field(name, field) : define_field(name, field)
end

#keyword(name, &block) ⇒ Object



85
86
87
# File 'lib/birddog/birddog.rb', line 85

def keyword(name, &block)
  define_scope(name, &block)
end

#maximumable(*fields) ⇒ Object



73
74
75
76
77
# File 'lib/birddog/birddog.rb', line 73

def maximumable(*fields)
  fields.flatten.compact.uniq.each do |max_field|
    @maximumable << max_field unless @maximumable.include?(max_field)
  end
end

#minimumable(*fields) ⇒ Object



67
68
69
70
71
# File 'lib/birddog/birddog.rb', line 67

def minimumable(*fields)
  fields.flatten.compact.uniq.each do |min_field|
    @minimumable << min_field unless @minimumable.include?(min_field)
  end
end

#search(query) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/birddog/birddog.rb', line 89

def search(query)
  if query.include?("..") && query =~ /\s*([[:alnum:]_]+)\s*:\s*{([^[:space:]]+)}\.\.{([^[:space:]]+)}\s*$/ # query is a range object
    begin_range = @model.scopes_for_query("#{$1}:>=#{$2}")
    return begin_range.scopes_for_query("#{$1}:<=#{$3}")        
  else
    key, value = tokenize(query)
    return scope_for(@model, key, value)
  end
end

#sumable(*fields) ⇒ Object



79
80
81
82
83
# File 'lib/birddog/birddog.rb', line 79

def sumable(*fields)
  fields.flatten.compact.uniq.each do |sum_field|
    @sumable << sum_field unless @sumable.include?(sum_field)
  end
end

#warn_common_aggregate_conditionalObject



304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/birddog/birddog.rb', line 304

def warn_common_aggregate_conditional
  raise "WARNING"
rescue => e
  warn <<-WARNING
  ===================================================================
  Birddog currently does not process conditionals on common aggregates
  your program may not behave as expected.

  Called at:

  #{e.backtrace.join("#{$/}      ")}
  ===================================================================
  WARNING
end