class Magentwo::Dataset

Attributes

model[RW]
opts[RW]

Public Class Methods

new(model, opts=nil) click to toggle source
# File lib/dataset.rb, line 4
def initialize model, opts=nil
  self.model = model
  self.opts = opts || {
    :filters => [],
    :pagination => {
      :current_page => Filter::CurrentPage.new(1),
      :page_size => Filter::PageSize.new(0)
    },
    :ordering => [],
    :fields => nil
  }
end

Public Instance Methods

all(meta_data:false) click to toggle source
# File lib/dataset.rb, line 112
def all meta_data:false
  self.model.all self, :meta_data => meta_data
end
count() click to toggle source
# File lib/dataset.rb, line 100
def count
  self.info[:total_count]
end
each(&block) click to toggle source
# File lib/dataset.rb, line 180
def each(&block)
  raise ArgumentError, "no block given" unless block_given?
  self.model.all.each(&block)
end
each_page(page_size=Magentwo.default_page_size, &block) click to toggle source
# File lib/dataset.rb, line 185
def each_page page_size=Magentwo.default_page_size, &block
  raise ArgumentError, "no block given" unless block_given?

  received_element_count = page_size
  current_page = 1
  total_count = nil
  until(total_count && current_page*page_size > (total_count + page_size)) do
    page = self.page(current_page, page_size).all meta_data:true
    total_count = page[:total_count] unless total_count
    block.call(page[:items])
    current_page += 1
  end
end
exclude(args) click to toggle source
# File lib/dataset.rb, line 39
def exclude args
  filter args, invert:true
end
fields() click to toggle source
# File lib/dataset.rb, line 104
def fields
  self.info[:fields]
end
filter(hash_or_other, invert:false) click to toggle source

Filters

# File lib/dataset.rb, line 20
def filter hash_or_other, invert:false
  filters = case hash_or_other
  when Hash
    raise ArgumentError, "empty hash supplied" if hash_or_other.empty?
    hash_or_other.map do |key, value|
      klass = case value
      when Array
        invert ? Filter::Nin : Filter::In
      else
        invert ? Filter::Neq : Filter::Eq
      end
      klass.new(key, value)
    end
  else
    raise ArgumentError, "filter function expects Hash as input"
  end
  Dataset.new self.model, self.opts.merge(:filters => self.opts[:filters] + filters)
end
first() click to toggle source
# File lib/dataset.rb, line 108
def first
  self.model.first self
end
from(args) click to toggle source
# File lib/dataset.rb, line 67
def from args
  Dataset.new self.model, self.opts.merge(:filters => self.opts[:filters] + [Filter::From.new(args.keys.first, args.values.first)])
end
gt(args) click to toggle source
# File lib/dataset.rb, line 43
def gt args
  Dataset.new self.model, self.opts.merge(:filters => self.opts[:filters] + [Filter::Gt.new(args.keys.first, args.values.first)])
end
gteq(args) click to toggle source
# File lib/dataset.rb, line 51
def gteq args
  Dataset.new self.model, self.opts.merge(:filters => self.opts[:filters] + [Filter::Gteq.new(args.keys.first, args.values.first)])
end
info() click to toggle source

Fetching

# File lib/dataset.rb, line 92
def info
  result = self.model.get self.page(1, 1).to_query, {:meta_data => true}
  {
    :fields => result[:items]&.first&.keys,
    :total_count => result[:total_count]
  }
end
like(args) click to toggle source
# File lib/dataset.rb, line 63
def like args
  Dataset.new self.model, self.opts.merge(:filters => self.opts[:filters] + [Filter::Like.new(args.keys.first, args.values.first)])
end
lt(args) click to toggle source
# File lib/dataset.rb, line 47
def lt args
  Dataset.new self.model, self.opts.merge(:filters => self.opts[:filters] + [Filter::Lt.new(args.keys.first, args.values.first)])
end
lteq(args) click to toggle source
# File lib/dataset.rb, line 55
def lteq args
  Dataset.new self.model, self.opts.merge(:filters => self.opts[:filters] + [Filter::Lteq.new(args.keys.first, args.values.first)])
end
map(&block) click to toggle source

Functors

# File lib/dataset.rb, line 175
def map(&block)
  raise ArgumentError, "no block given" unless block_given?
  self.model.all.map(&block)
end
order_by(field, direction="ASC") click to toggle source

Ordering

# File lib/dataset.rb, line 85
def order_by field, direction="ASC"
  Dataset.new self.model, self.opts.merge(:ordering => self.opts[:ordering] + [Filter::OrderBy.new(field, direction)])
end
page(page, page_size=Magentwo.default_page_size) click to toggle source

Pagination

# File lib/dataset.rb, line 78
def page page, page_size=Magentwo.default_page_size
  Dataset.new self.model, self.opts.merge(:pagination => {:current_page => Filter::CurrentPage.new(page), :page_size => Filter::PageSize.new(page_size)})
end
print_readable() click to toggle source

Transformation

select(*fields) click to toggle source
# File lib/dataset.rb, line 59
def select *fields
  Dataset.new self.model, self.opts.merge(:fields => Filter::Fields.new(fields))
end
to(args) click to toggle source
# File lib/dataset.rb, line 71
def to args
  Dataset.new self.model, self.opts.merge(:filters => self.opts[:filters] + [Filter::To.new(args.keys.first, args.values.first)])
end
to_query() click to toggle source
# File lib/dataset.rb, line 149
def to_query
  self.validate
  ds = self
  [
    ds.opts[:filters]
    .each_with_index
    .map { |opt, idx| opt.to_query(idx) }
    .join("&"),

    ds.opts[:pagination]
    .map { |k, v| v.to_query}
    .join("&"),


    ds.opts[:ordering]
    .map { |opt, idx| opt.to_query(idx) }
    .join("&"),

    ds.opts[:fields]? ds.opts[:fields].to_query() : ""
  ].reject(&:empty?)
  .join("&")
end
validate() click to toggle source

Validation

# File lib/dataset.rb, line 202
def validate
  true
end