Class: Sfn::Resource

Inherits:
HasSatisfaction show all
Includes:
Associations, Attributes, Util
Defined in:
lib/satisfaction/resource.rb,
lib/satisfaction/resource/attributes.rb

Direct Known Subclasses

Company, Person, Product, Reply, Tag, Topic

Defined Under Namespace

Modules: Attributes

Instance Attribute Summary collapse

Attributes inherited from HasSatisfaction

#satisfaction

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#requestify

Methods included from Attributes

included

Methods included from Associations

#belongs_to, #has_many

Constructor Details

#initialize(id, satisfaction) ⇒ Resource

Returns a new instance of Resource.



11
12
13
14
15
# File 'lib/satisfaction/resource.rb', line 11

def initialize(id, satisfaction)
  super satisfaction
  @id = id
  setup_associations if respond_to?(:setup_associations)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/satisfaction/resource.rb', line 7

def id
  @id
end

Class Method Details

.decode_sfn(value, satisfaction) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/satisfaction/resource/attributes.rb', line 58

def self.decode_sfn(value, satisfaction)
  case value
  when Hash
    id = value['id']
    returning(new(id, satisfaction)){|r| r.attributes = value}
  else
    new(value, satisfaction)
  end
end

Instance Method Details

#deleteObject



37
38
39
# File 'lib/satisfaction/resource.rb', line 37

def delete
  satisfaction.delete("#{path}.json")
end

#inspectObject



58
59
60
61
62
63
64
# File 'lib/satisfaction/resource.rb', line 58

def inspect
  if loaded?
    "<#{self.class.name} #{attributes.map{|k,v| "#{k}: #{v}"}.join(' ') if !attributes.nil?}>"
  else
    "<#{self.class.name} #{path} UNLOADED>"      
  end
end

#loadObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/satisfaction/resource.rb', line 21

def load
  result = satisfaction.get("#{path}.json")
  
  if result.first == :ok
    self.attributes = JSON.parse(result.last)
    was_loaded(result.last)
    self
  else
    result
  end
end

#loaded?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/satisfaction/resource.rb', line 54

def loaded?
  !@attributes.nil?
end

#pathObject



17
18
19
# File 'lib/satisfaction/resource.rb', line 17

def path
  raise "path not implemented in Resource base class"
end

#put(attrs) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/satisfaction/resource.rb', line 41

def put(attrs)
  params = requestify(attrs, self.class.name.demodulize.underscore)
  result = satisfaction.put("#{path}.json", params)
  
  if result.first == :ok
    json = JSON.parse(result.last)
    self.attributes = json
    self
  else
    result
  end
end

#was_loaded(result) ⇒ Object



33
34
35
# File 'lib/satisfaction/resource.rb', line 33

def was_loaded(result)
  #override this to augment post-loading behavior
end