Class: SisRuby::Params

Inherits:
Object
  • Object
show all
Defined in:
lib/sis_ruby/params.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fields(*args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sis_ruby/params.rb', line 12

def fields(*args)
  if args.none?
    return @fields
  elsif args.one?
    @fields = Array(args.first)
  else
    @fields = args
  end
  @fields = @fields.to_set  # order shouldn't matter, and no dups
  self
end

#filter(*args) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/sis_ruby/params.rb', line 25

def filter(*args)
  if args.any?
    @filter = args.first
    self
  else
    @filter
  end
end

#limit(*args) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/sis_ruby/params.rb', line 35

def limit(*args)
  if args.any?
    @limit = args.first
    self
  else
    @limit
  end
end

#offset(*args) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/sis_ruby/params.rb', line 45

def offset(*args)
  if args.any?
    @offset = args.first
    self
  else
    @offset
  end
end

#sort(*args) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/sis_ruby/params.rb', line 55

def sort(*args)
  if args.any?
    @sort = args.first
    self
  else
    @sort
  end
end

Class Method Details

.from_hash(other) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/sis_ruby/params.rb', line 76

def self.from_hash(other)
  instance = self.new
  instance.limit(other['limit']) if other['limit']
  instance.fields(other['fields'].split(',').to_set) if other['fields']
  instance.offset(other['offset']) if other['offset']
  instance.filter(other['q']) if other['q']
  instance.sort(other['sort']) if other['sort']
  instance
end

Instance Method Details

#<=>(other) ⇒ Object



113
114
115
# File 'lib/sis_ruby/params.rb', line 113

def <=>(other)
  self.to_h <=> other.to_h
end

#==(other) ⇒ Object



108
109
110
# File 'lib/sis_ruby/params.rb', line 108

def ==(other)
  other.is_a?(self.class) && other.to_h == self.to_h
end

#cloneObject



87
88
89
90
91
92
93
94
95
# File 'lib/sis_ruby/params.rb', line 87

def clone
  other = Params.new
  other.limit(self.limit)         if self.limit
  other.fields(self.fields.clone) if self.fields
  other.offset(self.offset)       if self.offset
  other.filter(self.filter.clone) if self.filter
  other.sort(self.sort)           if self.sort
  other
end

#hashObject



103
104
105
# File 'lib/sis_ruby/params.rb', line 103

def hash
  to_h.hash
end

#to_hObject



98
99
100
# File 'lib/sis_ruby/params.rb', line 98

def to_h
  to_hash
end

#to_hashObject



65
66
67
68
69
70
71
72
73
# File 'lib/sis_ruby/params.rb', line 65

def to_hash
  h = {}
  h['limit'] = limit if limit
  h['offset'] = offset if offset
  h['fields'] = fields.to_a.join(',') if fields
  h['q'] = filter if filter
  h['sort'] = sort if sort
  h
end