Sha256: 62f29c6f37d3ac95b63c0792ff60c7d681b1fc0df646b194a30250731ac29649

Contents?: true

Size: 1.88 KB

Versions: 3

Compression:

Stored size: 1.88 KB

Contents

module Quandl
module Client

class Dataset
  
  include Concerns::Search
  include Concerns::Properties
  
  
  ##########  
  # SCOPES #
  ##########
  
  # SEARCH
  search_scope :query, :rows
  search_scope :page, ->(p){ where( page: p.to_i )}
  search_scope :source_code, ->(c){ where( code: c.to_s.upcase )}
  
  # SHOW
  scope_composer_for :show
  show_scope :rows, :exclude_data, :exclude_headers, :trim_start, :trim_end, :transform, :collapse
  show_helper :find, ->(id){ connection.where(attributes).find( id ) }
  show_helper :connection, -> { self.class.parent }
  
  
  ###############
  # ASSOCIATIONS #
  ###############
   
  def source
    @source ||= Source.find(self.source_code)
  end
  
  
  ###############
  # VALIDATIONS #
  ###############
   
  validates :source_code, presence: true
  validates :code, presence: true, format: { with: /[A-Z0-9_]+/ }
  validates :name, presence: true, :length => { :maximum => 1000 }
  
  
  ##############
  # PROPERTIES #
  ##############
  
  attributes :source_code, :code, :name, :urlize_name, 
    :description, :updated_at, :frequency,
    :from_date, :to_date, :column_names, :private, :type,
    :display_url, :column_spec, :import_spec, :import_url,
    :locations_attributes, :data, :availability_delay
    
  before_save :enforce_required_formats
  
  alias_method :locations, :locations_attributes
  alias_method :locations=, :locations_attributes=
  
  def full_code
    @full_code ||= File.join(self.source_code, self.code)
  end

  def data_table
    Data::Table.new( raw_data )
  end
  
  def raw_data
    @raw_data ||= (self.data || Dataset.find(full_code).data || [])
  end
  
  protected
  
  def enforce_required_formats
    self.data = Quandl::Data::Table.new(data).to_csv
    self.locations_attributes = locations_attributes.to_json if locations_attributes.respond_to?(:to_json) && !locations_attributes.kind_of?(String)
  end
  
end

end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
quandl_client-0.1.16 lib/quandl/client/models/dataset.rb
quandl_client-0.1.15 lib/quandl/client/models/dataset.rb
quandl_client-0.1.14 lib/quandl/client/models/dataset.rb