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

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)


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

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



3
4
5
# File 'lib/flat_kit/stat_type.rb', line 3

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

.numerical_typesObject



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

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

.ordinal_typesObject



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

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

Instance Method Details

#collected_statsObject

Raises:

  • (NotImplementedError)


22
23
24
# File 'lib/flat_kit/stat_type.rb', line 22

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.



38
39
40
41
42
43
44
45
46
# File 'lib/flat_kit/stat_type.rb', line 38

def to_hash( *args )
  h = {}
  args = [ args ].flatten
  args = self.collected_stats if args.empty?
  args.each do |meth|
    h[meth] = self.send( meth )
  end
  return 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.



57
58
59
60
# File 'lib/flat_kit/stat_type.rb', line 57

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