Class: Plotrb::Data

Inherits:
Object show all
Includes:
Base
Defined in:
lib/plotrb/data.rb

Overview

The basic tabular data model used by Vega. See https://github.com/trifacta/vega/wiki/Data

Defined Under Namespace

Classes: Format

Instance Method Summary collapse

Methods included from Base

#add_attributes, #attributes, #classify, #collect_attributes, #define_boolean_attribute, #define_boolean_attributes, #define_multi_val_attribute, #define_multi_val_attributes, #define_single_val_attribute, #define_single_val_attributes, #defined_attributes, included, #set_attributes

Constructor Details

#initialize(&block) ⇒ Data

Returns a new instance of Data.



23
24
25
26
27
28
29
30
31
32
# File 'lib/plotrb/data.rb', line 23

def initialize(&block)
  define_single_val_attributes(:name, :values, :source, :url)
  define_multi_val_attribute(:transform)
  self.singleton_class.class_eval {
    alias_method :file, :url
  }
  self.instance_eval(&block) if block_given?
  ::Plotrb::Kernel.data << self
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/plotrb/data.rb', line 55

def method_missing(method, *args, &block)
  case method.to_s
    # set format of the data
    when /^as_(csv|tsv|json|topojson|treejson)$/
      self.format($1.to_sym, &block)
    else
      super
  end
end

Instance Method Details

#extra_fieldsObject



46
47
48
49
50
51
52
53
# File 'lib/plotrb/data.rb', line 46

def extra_fields
  @extra_fields ||= [:data, :index]
  if @transform
    @extra_fields.concat(@transform.collect { |t| t.extra_fields }).
        flatten!.uniq!
  end
  @extra_fields
end

#format(*args, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/plotrb/data.rb', line 34

def format(*args, &block)
  case args.size
    when 0
      @format
    when 1
      @format = ::Plotrb::Data::Format.new(args[0].to_sym, &block)
      self
    else
      raise ArgumentError, 'Invalid Data format'
  end
end