Class: Squad::Resource

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes = {}) ⇒ Resource

Returns a new instance of Resource.



137
138
139
140
141
142
143
# File 'lib/squad.rb', line 137

def initialize(name, attributes = {}) 
  @attributes = Hash[self.class.attributes.map{|key| [key, nil]}]
  @resource_name = name
  update_attributes(attributes)
  @status = nil 
  @header = DEFAULT_HEADER
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



70
71
72
# File 'lib/squad.rb', line 70

def header
  @header
end

#idObject

Returns the value of attribute id.



71
72
73
# File 'lib/squad.rb', line 71

def id
  @id
end

Class Method Details

.attribute(name) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/squad.rb', line 145

def self.attribute(name)
  attributes << name unless attributes.include? name
  define_method(name) do 
    @attributes[name]
  end
  define_method(:"#{name}=") do |value|
    @attributes[name] = value
  end
end

.attributesObject



155
# File 'lib/squad.rb', line 155

def self.attributes; @attributes ||= [] end

.bulk(name, &block) ⇒ Object



159
160
161
# File 'lib/squad.rb', line 159

def self.bulk(name, &block)
  bulks[name] = block
end

.bulksObject



157
# File 'lib/squad.rb', line 157

def self.bulks; @bulks ||= {} end

.collection(name) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/squad.rb', line 175

def self.collection(name)
  collections[name] = Proc.new do 
    show do |params|
      resource = Squad.routes[name].new(name)
      resource.query("#{@resource_name}_id", @id)
    end 
  end
end

.collectionsObject



174
# File 'lib/squad.rb', line 174

def self.collections; @collections ||= {} end

.element(name, &block) ⇒ Object



165
166
167
# File 'lib/squad.rb', line 165

def self.element(name, &block)
  elements[name] = block
end

.elementsObject



163
# File 'lib/squad.rb', line 163

def self.elements; @elements ||= {} end

.factor(&block) ⇒ Object



131
132
133
134
135
# File 'lib/squad.rb', line 131

def self.factor(&block)
  klass = dup
  klass.class_eval(&block)
  klass 
end

.index(attribute) ⇒ Object



170
171
172
# File 'lib/squad.rb', line 170

def self.index(attribute)
  indices << attribute unless indices.include?(attribute)
end

.indicesObject



169
# File 'lib/squad.rb', line 169

def self.indices; @indices ||= [] end

.reference(name) ⇒ Object



184
185
186
187
188
# File 'lib/squad.rb', line 184

def self.reference(name)
  field = :"#{name}_id" 
  attribute field 
  index field 
end

Instance Method Details

#allObject



196
# File 'lib/squad.rb', line 196

def all; Collection.new(self, key[:all].call("SMEMBERS")) end

#attributesObject



254
# File 'lib/squad.rb', line 254

def attributes; @attributes end

#bad_gatewayObject



83
# File 'lib/squad.rb', line 83

def bad_gateway;           @status = 502 end

#bad_requestObject



79
# File 'lib/squad.rb', line 79

def bad_request;           @status = 400 end

#createdObject



77
# File 'lib/squad.rb', line 77

def created;               @status = 201 end

#deleteObject



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/squad.rb', line 237

def delete
  OhmUtil.script(redis,
    OhmUtil::LUA_DELETE, 0, {
      "name" => @resource_name,
      "id" => id,
      "key" => key[id].to_s
    }.to_json,
    {}.to_json,
    {}.to_json
  )

  @attributes.each do |key, value|
    attributes[key] = nil 
  end
  @id = nil
end

#element_type?Boolean

Returns:

  • (Boolean)


129
# File 'lib/squad.rb', line 129

def element_type?; !defined?(@results) || !@results.kind_of?(Collection) end

#execute(params, &block) ⇒ Object



125
126
127
# File 'lib/squad.rb', line 125

def execute(params, &block)
  @results = yield params
end

#find(id) ⇒ Object



192
193
194
# File 'lib/squad.rb', line 192

def find(id)
  resource.new(@resource_name, {"id" => id}).load!
end

#internal_server_errorObject



81
# File 'lib/squad.rb', line 81

def internal_server_error; @status = 500 end

#keyObject



275
# File 'lib/squad.rb', line 275

def key;   @key ||= Nest.new(@resource_name, redis) end

#load!Object

Raises:



203
204
205
206
207
208
# File 'lib/squad.rb', line 203

def load!
  result = key[id].call("HGETALL") 
  raise NotFoundError if result.size == 0
  update_attributes(Hash[*result])
  return self
end

#no_cotentObject



78
# File 'lib/squad.rb', line 78

def no_cotent;             @status = 204 end

#not_foundObject



80
# File 'lib/squad.rb', line 80

def not_found;             @status = 404 end

#not_implementedObject



82
# File 'lib/squad.rb', line 82

def not_implemented;       @status = 501 end

#okObject

status code sytax suger



76
# File 'lib/squad.rb', line 76

def ok;                    @status = 200 end

#query(attribute, value) ⇒ Object



198
199
200
201
# File 'lib/squad.rb', line 198

def query(attribute, value)
  ids = key[:indices][attribute][value].call("SMEMBERS")
  Collection.new(self, ids)
end

#redisObject



276
# File 'lib/squad.rb', line 276

def redis; Squad.redis end

#render(request) ⇒ Object



119
120
121
122
123
# File 'lib/squad.rb', line 119

def render(request)
  raise NotImplementedError unless method_block = @request_methods[request.request_method]
  load! unless id.nil?
  execute(request.params, &method_block)
end

#reproduce(attributes) ⇒ Object



256
257
258
# File 'lib/squad.rb', line 256

def reproduce(attributes)
  self.class.new(@resource_name, OhmUtil.dict(attributes))
end

#resourceObject



190
# File 'lib/squad.rb', line 190

def resource; self.class end

#run(seg) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/squad.rb', line 85

def run(seg)  
  inbox = {}
  default_actions

  while seg.capture(:segment, inbox)
    segment = inbox[:segment].to_sym
    @request_methods = {}

    if new? && !defined?(@bulk_name)
      if bulk = self.class.bulks[segment]
        @bulk_name = segment
        return instance_eval(&bulk)
      else
        @id = segment
        load!
        default_element_action
        next 
      end
    end

    if !defined?(@element_name) && !defined?(@collection_name)
      if element = self.class.elements[segment]
        @element_name = segment
        return instance_eval(&element)
      elsif collection = self.class.collections[segment]
        @collection_name = segment
        return instance_eval(&collection)
      end
    end

    raise BadRequestError 
  end
end

#saveObject



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/squad.rb', line 217

def save
  feature = {name: @resource_name}
  feature["id"] = @id unless new? 
  
  indices = {}
  resource.indices.each do |field|
    next unless (value = send(field))
    indices[field] = Array(value).map(&:to_s)
  end

  @id = OhmUtil.script(redis,
    OhmUtil::LUA_SAVE,
    0,
    feature.to_json,
    serialize_attributes.to_json,
    indices.to_json,
    {}.to_json
  )
end

#statusObject



73
# File 'lib/squad.rb', line 73

def status; @status || ok end

#to_hashObject



260
261
262
263
264
265
# File 'lib/squad.rb', line 260

def to_hash
  hash = attributes
  hash[:id] = @id unless @id.nil?

  return hash
end

#to_jsonObject



267
268
269
270
271
272
273
# File 'lib/squad.rb', line 267

def to_json
  if element_type?
    JSON.dump(self.to_hash)
  else
    @results.to_json
  end
end

#update_attributes(atts) ⇒ Object



210
211
212
213
214
215
# File 'lib/squad.rb', line 210

def update_attributes(atts)
  @attributes.each do |attribute, value|
    send(:"#{attribute}=", atts[attribute.to_s]) if atts.has_key?(attribute.to_s)
  end
  @id = atts["id"] if atts["id"]
end