Class: FlatKit::StatType

Inherits:
Object
  • Object
show all
Defined in:
lib/flat_kit/stat_type.rb,
lib/flat_kit/stat_type/nominal_stats.rb,
lib/flat_kit/stat_type/ordinal_stats.rb,
lib/flat_kit/stat_type/numerical_stats.rb

Overview

Internal: Base class of all the statistic types

Direct Known Subclasses

NominalStats

Defined Under Namespace

Classes: NominalStats, NumericalStats, OrdinalStats

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for(type) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
# File 'lib/flat_kit/stat_type.rb', line 19

def self.for(type)
  return OrdinalStats   if ordinal_types.include?(type)
  return NominalStats   if nominal_types.include?(type)
  return NumericalStats if numerical_types.include?(type)

  raise ArgumentError, "Unknown stat type for #{type}"
end

.nominal_typesObject



7
8
9
# File 'lib/flat_kit/stat_type.rb', line 7

def self.nominal_types
  [FieldType::BooleanType, FieldType::StringType, FieldType::NullType]
end

.numerical_typesObject



15
16
17
# File 'lib/flat_kit/stat_type.rb', line 15

def self.numerical_types
  [FieldType::FloatType, FieldType::IntegerType]
end

.ordinal_typesObject



11
12
13
# File 'lib/flat_kit/stat_type.rb', line 11

def self.ordinal_types
  [FieldType::DateType, FieldType::TimestampType]
end

Instance Method Details

#collected_statsObject

Raises:

  • (NotImplementedError)


27
28
29
# File 'lib/flat_kit/stat_type.rb', line 27

def collected_stats
  raise NotImplementedError, "#{self.class.name} must implement #collected_stats"
end

#to_hash(*args) ⇒ Object

call-seq:

stat.to_hash   -> Hash
stat.to_hash( %w[ count max mean ]) -> Hash

return a hash of the stats. By default this returns a hash of all stats but passing in an array of items will limit the stats returned to only those in the Array.

If passed in an empty array or nil to to_hash then STATS is assumed to be the list of stats to return in the hash.



43
44
45
46
47
48
49
50
51
# File 'lib/flat_kit/stat_type.rb', line 43

def to_hash(*args)
  h = {}
  args = [args].flatten
  args = collected_stats if args.empty?
  args.each do |meth|
    h[meth] = send(meth)
  end
  h
end

#to_json(*args) ⇒ Object

call-seq:

stat.to_json  -> String
stat.to_json( *args ) -> String

return a json string of the stats. By default this returns a json string of all the stats. If an array of items is passed in, those that match the known stats will be all that is included in the json output.



62
63
64
65
# File 'lib/flat_kit/stat_type.rb', line 62

def to_json(*args)
  h = to_hash(*args)
  Oj.dump(h)
end