class Plotrb::Data

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

Public Class Methods

new(&block) click to toggle source
# 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

Public Instance Methods

extra_fields() click to toggle source
# 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) click to toggle source
# 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
method_missing(method, *args, &block) click to toggle source
Calls superclass method Plotrb::Kernel#method_missing
# 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

Private Instance Methods

attribute_post_processing() click to toggle source
# File lib/plotrb/data.rb, line 67
def attribute_post_processing
  process_name
  process_values
  process_source
  process_url
  process_transform
end
process_name() click to toggle source
# File lib/plotrb/data.rb, line 75
def process_name
  if @name.nil? || @name.strip.empty?
    raise ArgumentError, 'Name missing for Data object'
  end
  if ::Plotrb::Kernel.duplicate_data?(@name)
    raise ArgumentError, 'Duplicate names for Data object'
  end
end
process_source() click to toggle source
# File lib/plotrb/data.rb, line 100
def process_source
  return unless @source
  case source
    when String
      unless ::Plotrb::Kernel.find_data(@source)
        raise ArgumentError, 'Source Data not found'
      end
    when ::Plotrb::Data
      @source = @source.name
    else
      raise ArgumentError, 'Unknown Data source'
  end
end
process_transform() click to toggle source
# File lib/plotrb/data.rb, line 123
def process_transform
  return unless @transform
  if @transform.any? { |t| not t.is_a?(::Plotrb::Transform) }
    raise ArgumentError, 'Invalid Data Transform'
  end
end
process_url() click to toggle source
# File lib/plotrb/data.rb, line 114
def process_url
  return unless @url
  begin
    URI.parse(@url)
  rescue URI::InvalidURIError
    raise ArgumentError, 'Invalid URL for Data'
  end
end
process_values() click to toggle source
# File lib/plotrb/data.rb, line 84
def process_values
  return unless @values
  case @values
    when String
      begin
        Yajl::Parser.parse(@values)
      rescue Yajl::ParseError
        raise ArgumentError, 'Invalid JSON values in Data'
      end
    when Array, Hash
      # leave as it is
    else
      raise ArgumentError, 'Unsupported value type in Data'
  end
end