Class: Brocktail::BaseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/brocktail/base_model.rb

Direct Known Subclasses

Act, Event

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ BaseModel

Returns a new instance of BaseModel.



4
5
6
# File 'lib/brocktail/base_model.rb', line 4

def initialize(attributes = {})
  self.attributes = attributes
end

Class Method Details

.api_path(path = nil) ⇒ Object



45
46
47
# File 'lib/brocktail/base_model.rb', line 45

def api_path(path = nil)
  @path ||= path
end

.parse(json, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/brocktail/base_model.rb', line 24

def self.parse(json, options = {})
  options[:resource] ||= self.name.split('::').last.downcase
  puts options
  
  if options[:singular]
    return self.new(Crack::JSON.parse(json)[options[:resource].to_s])
  else
    resources = []
    
    # Rails 3 way: Arrayified objects
    # [{"object":{...}}, {"object":{...}}, ..., {"object":{...}}]
    Crack::JSON.parse(json).each { |obj| resources << self.new(obj[options[:resource].to_s]) }
    
    # Rails old way: Rooted JSON
    # {"objects":[{...}, {...}, ..., {...}]}
    # Crack::JSON.parse(json)[options[:resource].to_s.pluralize].each { |attrs| resources << self.new(attrs) }
    return resources
  end
end

Instance Method Details

#==(other) ⇒ Object



12
13
14
# File 'lib/brocktail/base_model.rb', line 12

def ==(other)
  id == other.id
end

#attributes=(attributes) ⇒ Object



8
9
10
# File 'lib/brocktail/base_model.rb', line 8

def attributes=(attributes)
  attributes.each { |k,v| send("#{k}=", v)}
end

#to_iObject



16
17
18
# File 'lib/brocktail/base_model.rb', line 16

def to_i
  id
end

#to_jsonObject



20
21
22
# File 'lib/brocktail/base_model.rb', line 20

def to_json
  # TODO
end