Class: Brocktail::BaseModel
- Inherits:
-
Object
- Object
- Brocktail::BaseModel
- Defined in:
- lib/brocktail/base_model.rb
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #attributes=(attributes) ⇒ Object
-
#initialize(attributes = {}) ⇒ BaseModel
constructor
A new instance of BaseModel.
- #to_i ⇒ Object
- #to_json ⇒ Object
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, = {}) [:resource] ||= self.name.split('::').last.downcase puts if [:singular] return self.new(Crack::JSON.parse(json)[[:resource].to_s]) else resources = [] # Rails 3 way: Arrayified objects # [{"object":{...}}, {"object":{...}}, ..., {"object":{...}}] Crack::JSON.parse(json).each { |obj| resources << self.new(obj[[: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_i ⇒ Object
16 17 18 |
# File 'lib/brocktail/base_model.rb', line 16 def to_i id end |
#to_json ⇒ Object
20 21 22 |
# File 'lib/brocktail/base_model.rb', line 20 def to_json # TODO end |