Module: Her::Model::ORM

Included in:
Her::Model
Defined in:
lib/her/model/orm.rb

Overview

This module adds ORM-like capabilities to the model

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, attrs = nil) ⇒ Object

Handles missing methods by routing them through @data



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/her/model/orm.rb', line 20

def method_missing(method, attrs=nil) # {{{
  assignment_method = method.to_s =~ /\=$/
  method = method.to_s.gsub(/(\?|\!|\=)$/, "").to_sym
  if attrs and assignment_method
    @data[method.to_s.gsub(/\=$/, "").to_sym] = attrs
  else
    if @data.include?(method)
      @data[method]
    else
      super
    end
  end
end

Class Method Details

.initialize_collection(name, collection_data) ⇒ Object

Initialize a collection of resources



14
15
16
# File 'lib/her/model/orm.rb', line 14

def self.initialize_collection(name, collection_data) # {{{
  collection_data.map { |item_data| Object.const_get(name.to_s.classify).new(item_data) }
end

Instance Method Details

#all(params = {}) ⇒ Object

Fetch a collection of resources

Examples:

@users = User.all # GET /users


54
55
56
57
58
# File 'lib/her/model/orm.rb', line 54

def all(params={}) # {{{
  request(params.merge(:_method => :get, :_path => "#{@her_collection_path}")) do |parsed_data|
    new_collection(parsed_data)
  end
end

#create(params = {}) ⇒ Object

Create a resource and return it

Examples:

@user = User.create({ :fullname => "Tobias Fünke" }) # POST /users/1


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/her/model/orm.rb', line 64

def create(params={}) # {{{
  resource = new(params)
  perform_hook(resource, :before, :create)
  perform_hook(resource, :before, :save)
  params = resource.instance_eval { @data }
  request(params.merge(:_method => :post, :_path => "#{@her_collection_path}")) do |parsed_data|
    resource.instance_eval do
      @data = parsed_data[:data]
    end
  end
  perform_hook(resource, :after, :save)
  perform_hook(resource, :after, :create)

  resource
end

#destroyObject

Destroy a resource

Examples:

@user = User.find(1) # GET /users/1
@user.destroy # DELETE /users/1


127
128
129
130
131
132
133
134
135
# File 'lib/her/model/orm.rb', line 127

def destroy # {{{
  params = @data.dup
  self.class.perform_hook(self, :before, :destroy)
  self.class.request(params.merge(:_method => :delete, :_path => "#{self.class.collection_path}/#{id}")) do |parsed_data|
    @data = parsed_data[:data]
  end
  self.class.perform_hook(self, :after, :destroy)
  self
end

#destroy_existing(id) ⇒ Object

Destroy an existing resource

Examples:

User.destroy_existing(1) # DELETE /users/1


141
142
143
144
145
146
# File 'lib/her/model/orm.rb', line 141

def destroy_existing(id) # {{{
  params = {}
  request(params.merge(:_method => :delete, :_path => "#{collection_path}/#{id}")) do |parsed_data|
    new(parsed_data[:data])
  end
end

#find(id, params = {}) ⇒ Object

Fetch a specific resource based on an ID

Examples:

@user = User.find(1) GET /users/1


44
45
46
47
48
# File 'lib/her/model/orm.rb', line 44

def find(id, params={}) # {{{
  request(params.merge(:_method => :get, :_path => "#{@her_collection_path}/#{id}")) do |parsed_data|
    new(parsed_data[:data])
  end
end

#initialize(single_data) ⇒ Object

Initialize a new object with data received from an HTTP request



7
8
9
10
# File 'lib/her/model/orm.rb', line 7

def initialize(single_data) # {{{
  @data = single_data
  @data = self.class.parse_relationships(@data)
end

#new_collection(parsed_data) ⇒ Object

Initialize a collection of resources with raw data from an HTTP request



35
36
37
38
# File 'lib/her/model/orm.rb', line 35

def new_collection(parsed_data) # {{{
  collection_data = parsed_data[:data]
  Her::Model::ORM.initialize_collection(self.to_s.downcase.to_sym, collection_data)
end

#saveObject

Save a resource

Examples:

Save a resource after fetching it

@user = User.find(1) # GET /users/1
@user.fullname = "Tobias Fünke"
@user.save # PUT /users/1

Save a new resource by creating it

@user = User.new({ :fullname => "Tobias Fünke" })
@user.save # POST /users


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/her/model/orm.rb', line 99

def save # {{{
  params = @data.dup
  if @data[:id]
    self.class.perform_hook(self, :before, :update)
    self.class.perform_hook(self, :before, :save)
    self.class.request(params.merge(:_method => :put, :_path => "#{self.class.collection_path}/#{id}")) do |parsed_data|
      @data = parsed_data[:data]
    end
    self.class.perform_hook(self, :after, :save)
    self.class.perform_hook(self, :after, :update)
    self
  else
    self.class.perform_hook(self, :before, :create)
    self.class.perform_hook(self, :before, :save)
    self.class.request(params.merge(:_method => :post, :_path => "#{self.class.collection_path}")) do |parsed_data|
      @data = parsed_data[:data]
    end
    self.class.perform_hook(self, :after, :save)
    self.class.perform_hook(self, :after, :create)
  end
  self
end

#save_existing(id, params) ⇒ Object

Save an existing resource and return it

Examples:

@user = User.save_existing(1, { :fullname => "Tobias Fünke" }) # PUT /users/1


84
85
86
87
# File 'lib/her/model/orm.rb', line 84

def save_existing(id, params) # {{{
  resource = new(params.merge(:id => id))
  resource.save
end