Module: PGit::Helpers::QueryMethods

Included in:
Project, Project
Defined in:
lib/pgit/helpers/query_methods.rb

Instance Method Summary collapse

Instance Method Details

#attr_given(args) ⇒ Object


54
55
56
57
58
59
60
# File 'lib/pgit/helpers/query_methods.rb', line 54

def attr_given(args)
  args.each do |item|
    define_method "given_#{item}?" do
      instance_variable_get("@#{item}") != not_given(item)
    end
  end
end

#attr_has(args) ⇒ Object


46
47
48
49
50
51
52
# File 'lib/pgit/helpers/query_methods.rb', line 46

def attr_has(args)
  args.each do |method_name|
    define_method "has_#{method_name}?" do |val|
      instance_variable_get("@#{method_name}") == val
    end
  end
end

#attr_query(*args) ⇒ Object


23
24
25
26
# File 'lib/pgit/helpers/query_methods.rb', line 23

def attr_query(*args)
  attr_has(args)
  attr_given(args)
end

#defaulted_attrsObject

attributes that have been set to default values


37
38
39
# File 'lib/pgit/helpers/query_methods.rb', line 37

def defaulted_attrs
  given_attrs.reject {|attr| send("given_#{attr}?")}
end

#ensure_given_attr(attribute) ⇒ Object

Raises:


18
19
20
21
# File 'lib/pgit/helpers/query_methods.rb', line 18

def ensure_given_attr(attribute)
  attr = send(attribute)
  raise PGit::Error::User.new(attr.to_s) if attr == not_given(attribute)
end

#ensure_given_queriesObject


14
15
16
# File 'lib/pgit/helpers/query_methods.rb', line 14

def ensure_given_queries
  given_attrs.each {|attr| ensure_given_attr(attr) }
end

#given_attrsObject

attributes that have corresponding #given_attr? methods


42
43
44
# File 'lib/pgit/helpers/query_methods.rb', line 42

def given_attrs
  methods.grep(/^given_.+\?$/).map { |m| m.to_s.gsub(/^given_/, '').gsub(/\?$/, '')}
end

#not_given(attribute) ⇒ Object


10
11
12
# File 'lib/pgit/helpers/query_methods.rb', line 10

def not_given(attribute)
  "no_#{attribute}_given".to_sym
end

#set_attr(attribute) ⇒ Object


4
5
6
7
8
# File 'lib/pgit/helpers/query_methods.rb', line 4

def set_attr(attribute)
  unless instance_variable_get("@#{attribute}")
    instance_variable_set("@#{attribute}", @query_hash[attribute.to_s] || yield)
  end
end

#set_default_attr(attribute) ⇒ Object


28
29
30
# File 'lib/pgit/helpers/query_methods.rb', line 28

def set_default_attr(attribute)
  set_attr(attribute) { not_given(attribute) }
end

#set_default_queriesObject


32
33
34
# File 'lib/pgit/helpers/query_methods.rb', line 32

def set_default_queries
  given_attrs.each {|attr| set_default_attr(attr) }
end