Class: Phabulous::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/phabulous/entity.rb

Direct Known Subclasses

Feed, Paste, Revision, User

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Entity

Returns a new instance of Entity.



48
49
50
51
52
# File 'lib/phabulous/entity.rb', line 48

def initialize(attributes = {})
  attributes.each do |attr, value|
    send("#{attr}=", value) if respond_to?("#{attr}=")
  end unless attributes.nil?
end

Instance Attribute Details

#phidObject

Returns the value of attribute phid.



61
62
63
# File 'lib/phabulous/entity.rb', line 61

def phid
  @phid
end

Class Method Details

.allObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/phabulous/entity.rb', line 13

def self.all
  @all ||= Phabulous.conduit.request("#{conduit_name}.query").collect do |attributes|
    # Some conduit.query calls come back as plain arrays like
    #  [
    #    {
    #      attr: 1
    #     },
    #     {
    #     }
    #  ]
    # as
    # {
    #   phid => {
    #    attr1: x,
    #    ...
    #   },
    #   phid-2 => {
    #     attr2: y
    #   }
    # }
    #
    # This code attempts to handle both cases
    if attributes.length == 2 &&
        attributes.first.is_a?(String) && attributes.first =~ /^PHID.*$/
      new(attributes.last)
    else
      new(attributes)
    end
  end
end

.attr_accessor(*args) ⇒ Object



3
4
5
6
7
# File 'lib/phabulous/entity.rb', line 3

def self.attr_accessor(*args)
  @attributes ||= []
  @attributes |= args
  super(*args)
end

.attributesObject



9
10
11
# File 'lib/phabulous/entity.rb', line 9

def self.attributes
  @attributes
end

.conduit_nameObject



63
64
65
# File 'lib/phabulous/entity.rb', line 63

def self.conduit_name
  name.demodulize.downcase
end

.find(id) ⇒ Object



44
45
46
# File 'lib/phabulous/entity.rb', line 44

def self.find(id)
  all.select { |entity| entity.phid == id }.first
end

Instance Method Details

#attributesObject



54
55
56
57
58
59
# File 'lib/phabulous/entity.rb', line 54

def attributes
  self.class.attributes.inject({}) do |memo, attr|
    memo[attr] = send(attr) if respond_to?(attr)
    memo
  end if self.class.attributes
end