Class: Hashie::Deash

Inherits:
Mash
  • Object
show all
Defined in:
lib/desk/deash.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/desk/deash.rb', line 40

def method_missing(method, *args, &block)
  return self.[](method) if key?(method)
  # TODO: Make this DRY
  if includes_key_chain?("_links."+method.to_s)
    return nil if !self._links[method]
    return Desk.get(self._links[method].href.sub(Desk.api_path, ""))
  elsif includes_key_chain?("raw._links."+method.to_s)
    return nil if !self.raw._links[method]
    return Desk.get(self.raw._links[method].href.sub(Desk.api_path, ""))
  elsif includes_key_chain?("raw."+method.to_s)
    return nil if !self.raw[method]
    return self.raw[method]
  end
  return super
end

Instance Method Details

#countObject



22
23
24
25
26
27
28
# File 'lib/desk/deash.rb', line 22

def count
  if includes_key_chain?("raw._embedded.entries")
    self.raw._embedded['entries'].count
  else
    super
  end
end

#dynamic_cached_method(meth, value) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/desk/deash.rb', line 56

def dynamic_cached_method(meth, value)
  (class << self; self; end).class_eval do
    define_method meth do
      instance_variable_set("@#{meth}", value)
    end
  end
end

#eachObject



30
31
32
33
34
35
36
37
38
# File 'lib/desk/deash.rb', line 30

def each
  if includes_key_chain?("raw._embedded.entries")
    self.raw._embedded['entries'].each do |entry|
      yield entry
    end
  else
    super
  end
end

#id(parent_id = false) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/desk/deash.rb', line 64

def id(parent_id = false)
  id = nil
  if includes_key_chain?("raw._links.self.href") ||
     includes_key_chain?("_links.self.href")
    c = self._links.self['class']
    if Desk.respond_to? "#{c}_id"
      id = Desk.send("#{c}_id", self._links.self.href, parent_id)
    else
      p = self._links.self.href.split("/")
      if p.size > 5 && !parent_id
        id = p[6]
      elsif (p.size < 6 && !parent_id) || (p.size > 5 && parent_id)
        id = p[4]
      end
      id = id.to_i if id.to_i != 0
    end
  end
  id
end

#includes_key_chain?(chain) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
# File 'lib/desk/deash.rb', line 88

def includes_key_chain?(chain)
  current_chain = self
  chain.split(".").each do |k|
    return false if !current_chain.key?(k)
    current_chain = current_chain[k]
  end
  true
end


97
98
99
100
# File 'lib/desk/deash.rb', line 97

def links
  Links.new(self._links) if key?("_links")
  Links.new(self.raw._links) if includes_key_chain?("raw._links")
end

#parent_idObject



84
85
86
# File 'lib/desk/deash.rb', line 84

def parent_id
  id(true)
end

#resultsObject



102
103
104
# File 'lib/desk/deash.rb', line 102

def results
  self._embedded['entries'] if self.raw.key?('_embedded') && self._embedded.key?('entries')
end