Class: Herder::Interactable::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/herder/interactable/query.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Query

Returns a new instance of Query.



4
5
6
7
8
# File 'lib/herder/interactable/query.rb', line 4

def initialize options
  params[:interactable_id] = options[:id] if options[:id]
  params[:interactable_type] = options[:type] if options[:type]
  newest
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *args, &block) ⇒ Object (protected)



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/herder/interactable/query.rb', line 75

def method_missing(key, *args, &block)
  if key.to_s.ends_with? "?"
    key = key.to_s.gsub("?", "")
    state? key
  elsif key.to_s.ends_with? "="
    key = key.to_s.gsub("=", "")
    set(key).to(*args)
  elsif params[:limit] || params[:offset]
    states key
  else
    state(key)
  end
end

Instance Method Details

#each(&block) ⇒ Object

this is used for lazy execution of the query



11
12
13
# File 'lib/herder/interactable/query.rb', line 11

def each &block
  query.each &block
end

#limit(count) ⇒ Object



19
20
21
22
# File 'lib/herder/interactable/query.rb', line 19

def limit count
  params[:limit] = count
  self
end

#newestObject



28
29
30
# File 'lib/herder/interactable/query.rb', line 28

def newest
  order "created_at DESC"
end

#oldestObject



24
25
26
# File 'lib/herder/interactable/query.rb', line 24

def oldest
  order "created_at ASC"
end

#set(key) ⇒ Object



32
33
34
35
# File 'lib/herder/interactable/query.rb', line 32

def set key
  params[:key] = key
  self
end

#state(key) ⇒ Object



47
48
49
50
51
# File 'lib/herder/interactable/query.rb', line 47

def state key
  limit 1
  newest
  states(key).first
end

#state?(key) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/herder/interactable/query.rb', line 42

def state? key
  interaction = state(key)
  interaction && interaction.value.to_s == "true"
end

#states(key) ⇒ Object



53
54
55
56
# File 'lib/herder/interactable/query.rb', line 53

def states key
  params[:key] = key
  query
end

#to(value) ⇒ Object



37
38
39
40
# File 'lib/herder/interactable/query.rb', line 37

def to value
  params[:value] = value
  create
end

#to_sObject



15
16
17
# File 'lib/herder/interactable/query.rb', line 15

def to_s
  query.to_s
end