class Her::Relation

Public Class Methods

new(model) click to toggle source
# File lib/her/relation.rb, line 3
def initialize(model)
  @model = model
  @conditions = []
  @group_conditions = []
  @having_conditions = []
  @order_conditions = []
  @limit_value = nil
  @offset_value = nil
  @do_paginate = false
  @do_count = false
end

Public Instance Methods

all(params = {}) click to toggle source
# File lib/her/relation.rb, line 46
def all(params = {})
  with_response(params) do |data|
    @model.new_collection(data)
  end
end
count() click to toggle source
# File lib/her/relation.rb, line 52
def count
  @do_count = true
  with_response do |data|
    data.first
  end
end
first() click to toggle source
# File lib/her/relation.rb, line 59
def first
  order('id asc').limit(1)
  all.first if all.respond_to?(:first)
end
group(*args) click to toggle source
# File lib/her/relation.rb, line 21
def group(*args)
  @group_conditions += args
  self
end
having(*args) click to toggle source
# File lib/her/relation.rb, line 26
def having(*args)
  @having_conditions += args
  self
end
last() click to toggle source
# File lib/her/relation.rb, line 64
def last
  order('id desc').limit(1)
  all.first if all.respond_to?(:first)
end
limit(value) click to toggle source
# File lib/her/relation.rb, line 36
def limit(value)
  @limit_value = value
  self
end
offset(value) click to toggle source
# File lib/her/relation.rb, line 41
def offset(value)
  @offset_value = value
  self
end
order(*args) click to toggle source
# File lib/her/relation.rb, line 31
def order(*args)
  @order_conditions += args
  self
end
paginate(page = 1, per_page = 20) click to toggle source
# File lib/her/relation.rb, line 69
def paginate(page = 1, per_page = 20)
  page = page.to_i < 1 ? 1 : page.to_i
  per_page = per_page.to_i < 1 ? 20 : per_page.to_i

  @do_paginate = true
  offset((page - 1) * per_page).limit(per_page)
end
where(*args) click to toggle source
# File lib/her/relation.rb, line 15
def where(*args)
  args = args.first if args.first.is_a?(Hash)
  @conditions.push(args)
  self
end

Private Instance Methods

with_response(params = {}) { |parsed_data| ... } click to toggle source
# File lib/her/relation.rb, line 79
def with_response(params = {})
  params[:her_special_where] = MultiJson.dump(@conditions) unless @conditions.empty?
  params[:her_special_group] = MultiJson.dump(@group_conditions) unless @group_conditions.empty?
  params[:her_special_having] = MultiJson.dump(@having_conditions) unless @having_conditions.empty?
  params[:her_special_order] = MultiJson.dump(@order_conditions) unless @order_conditions.empty?
  params[:her_special_limit] = @limit_value unless @limit_value.nil?
  params[:her_special_offset] = @offset_value unless @offset_value.nil?
  params[:her_special_paginate] = 1 if @do_paginate
  params[:her_special_count] = 1 if @do_count

  @model.request(params.merge(_method: :get, _path: @model.build_request_path(params))) do |parsed_data|
    yield parsed_data if block_given?
  end
end