Class: Praxis::Mapper::Resource

Inherits:
Object
  • Object
show all
Extended by:
Finalizable
Defined in:
lib/praxis-mapper/resource.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Finalizable

_finalize!, extended, finalizable, finalize!, finalized?, inherited

Constructor Details

#initialize(record) ⇒ Resource

Returns a new instance of Resource.



252
253
254
# File 'lib/praxis-mapper/resource.rb', line 252

def initialize(record)
  @record = record
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



260
261
262
263
264
265
266
267
# File 'lib/praxis-mapper/resource.rb', line 260

def method_missing(name,*args)
  if @record.respond_to?(name)
    self.class.define_accessor(name)
    self.send(name)
  else
    super
  end
end

Class Attribute Details

.decorationsObject (readonly)

Returns the value of attribute decorations.



36
37
38
# File 'lib/praxis-mapper/resource.rb', line 36

def decorations
  @decorations
end

.model_mapObject (readonly)

Returns the value of attribute model_map.



35
36
37
# File 'lib/praxis-mapper/resource.rb', line 35

def model_map
  @model_map
end

.propertiesObject (readonly)

Returns the value of attribute properties.



37
38
39
# File 'lib/praxis-mapper/resource.rb', line 37

def properties
  @properties
end

Instance Attribute Details

#recordObject

Returns the value of attribute record.



30
31
32
# File 'lib/praxis-mapper/resource.rb', line 30

def record
  @record
end

Class Method Details

._finalize!Object



80
81
82
83
84
85
86
# File 'lib/praxis-mapper/resource.rb', line 80

def self._finalize!
  finalize_resource_delegates
  define_model_accessors
  define_decorators

  super
end

.all(condition = {}) ⇒ Object



166
167
168
169
170
# File 'lib/praxis-mapper/resource.rb', line 166

def self.all(condition={})
  records = self.model.all(condition)

  self.wrap(records)
end

.decorate(name, &block) ⇒ Object



72
73
74
# File 'lib/praxis-mapper/resource.rb', line 72

def self.decorate(name, &block)
  self.decorations[name] = Class.new(ResourceDecorator, &block)
end

.define_accessor(name) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/praxis-mapper/resource.rb', line 236

def self.define_accessor(name)
  if name.to_s =~ /\?/
    ivar_name = "is_#{name.to_s[0..-2]}"
  else
    ivar_name = "#{name}"
  end

  module_eval "  def \#{name}\n    return @__\#{ivar_name} if defined? @__\#{ivar_name}\n    @__\#{ivar_name} = record.\#{name}\n  end\n  RUBY\nend\n", __FILE__, __LINE__ + 1

.define_decorator(name, block) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/praxis-mapper/resource.rb', line 117

def self.define_decorator(name, block)
  unless self.instance_methods.include?(name)
    # assume it'll be a regular accessor and create it
    self.define_accessor(name)
  end
  # alias original method and wrap it
  raw_name = "_raw_#{name}"
  alias_method(raw_name.to_sym, name)

  module_eval "    def \#{name}\n      object = self.\#{raw_name}\n      self.class.decorations[\#{name.inspect}].new(self, object)\n    end\n  RUBY\nend\n", __FILE__, __LINE__ + 1

.define_decoratorsObject



111
112
113
114
115
# File 'lib/praxis-mapper/resource.rb', line 111

def self.define_decorators
  self.decorations.each do |name,block|
    self.define_decorator(name, block)
  end
end


221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/praxis-mapper/resource.rb', line 221

def self.define_delegation_for_related_association(resource_name, resource_attribute, related_association)
  related_resource_class = model_map[related_association[:model]]
  return unless related_resource_class

  module_eval "    def \#{resource_attribute}\n      @__\#{resource_attribute} ||= if (rec = self.\#{resource_name})\n      if (related = rec.\#{resource_attribute})\n        \#{related_resource_class.name}.wrap(related)\n      end\n    end\n  end\n  RUBY\nend\n", __FILE__, __LINE__ + 1


211
212
213
214
215
216
217
218
219
# File 'lib/praxis-mapper/resource.rb', line 211

def self.define_delegation_for_related_attribute(resource_name, resource_attribute)
  module_eval "    def \#{resource_attribute}\n      @__\#{resource_attribute} ||= if (rec = self.\#{resource_name})\n      rec.\#{resource_attribute}\n        end\n    end\n  RUBY\nend\n", __FILE__, __LINE__ + 1

.define_model_accessorsObject



99
100
101
102
103
104
105
106
107
108
# File 'lib/praxis-mapper/resource.rb', line 99

def self.define_model_accessors
  return if model.nil?

  model.associations.each do |k,v|
    if self.instance_methods.include? k
      warn "WARNING: #{self.name} already has method named #{k.inspect}. Will not define accessor for resource association."
    end
    define_model_association_accessor(k,v)
  end
end

.define_model_association_accessor(name, association_spec) ⇒ Object

Defines wrappers for model associations that return Resources



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/praxis-mapper/resource.rb', line 184

def self.define_model_association_accessor(name, association_spec)
  association_model = association_spec.fetch(:model)
  association_resource_class = model_map[association_model]

  if association_resource_class
    module_eval "    def \#{name}\n      records = record.\#{name}\n        return nil if records.nil?\n      @__\#{name} ||= \#{association_resource_class}.wrap(records)\n    end\n    RUBY\n  end\nend\n", __FILE__, __LINE__ + 1

.define_resource_delegate(resource_name, resource_attribute) ⇒ Object



199
200
201
202
203
204
205
206
207
208
# File 'lib/praxis-mapper/resource.rb', line 199

def self.define_resource_delegate(resource_name, resource_attribute)
  related_model = model.associations[resource_name][:model]
  related_association = related_model.associations[resource_attribute]

  if related_association
    self.define_delegation_for_related_association(resource_name, resource_attribute, related_association)
  else
    self.define_delegation_for_related_attribute(resource_name, resource_attribute)
  end
end

.finalize_resource_delegatesObject



88
89
90
91
92
93
94
95
96
# File 'lib/praxis-mapper/resource.rb', line 88

def self.finalize_resource_delegates
  return unless @resource_delegates

  @resource_delegates.each do |record_name, record_attributes|
    record_attributes.each do |record_attribute|
      self.define_resource_delegate(record_name, record_attribute)
    end
  end
end

.for_record(record) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/praxis-mapper/resource.rb', line 134

def self.for_record(record)
  return record._resource if record._resource

  if resource_class_for_record = model_map[record.class]
    return record._resource = resource_class_for_record.new(record)
  else
    version = self.name.split("::")[0..-2].join("::")
    resource_name = record.class.name.split("::").last

    raise "No resource class corresponding to the model class '#{record.class}' is defined. (Did you forget to define '#{version}::#{resource_name}'?)"
  end
end

.get(condition) ⇒ Object



160
161
162
163
164
# File 'lib/praxis-mapper/resource.rb', line 160

def self.get(condition)
  record = self.model.get(condition)

  self.wrap(record)
end

.inherited(klass) ⇒ Object

TODO: also support an attribute of sorts on the versioned resource module. ie, V1::Resources.api_version.

replacing the self.superclass == Praxis::Mapper::Resource condition below.


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/praxis-mapper/resource.rb', line 42

def self.inherited(klass)
  super

  klass.instance_eval do
    # It is expected that each versioned set of resources
    # will have a common Base class, and so should share
    # a model_map
    if self.superclass == Praxis::Mapper::Resource
      @model_map = Hash.new
    else
      @model_map = self.superclass.model_map
    end

    @decorations = {}
    @properties = self.superclass.properties.clone
  end

end

.model(klass = nil) ⇒ Object

TODO: Take symbol/string and resolve the klass (but lazily, so we don’t care about load order)



62
63
64
65
66
67
68
69
# File 'lib/praxis-mapper/resource.rb', line 62

def self.model(klass=nil)
  if klass
    @model = klass
    self.model_map[klass] = self
  else
    @model
  end
end

.property(name, **options) ⇒ Object



76
77
78
# File 'lib/praxis-mapper/resource.rb', line 76

def self.property(name, **options)
  self.properties[name] = options
end

.resource_delegate(spec) ⇒ Object



177
178
179
180
181
# File 'lib/praxis-mapper/resource.rb', line 177

def self.resource_delegate(spec)
  spec.each do |resource_name, attributes|
    resource_delegates[resource_name] = attributes
  end
end

.resource_delegatesObject



173
174
175
# File 'lib/praxis-mapper/resource.rb', line 173

def self.resource_delegates
  @resource_delegates ||= {}
end

.wrap(records) ⇒ Object



148
149
150
151
152
153
154
155
156
157
# File 'lib/praxis-mapper/resource.rb', line 148

def self.wrap(records)
  case records
  when nil
    return []
  when Enumerable
    return records.compact.collect { |record| self.for_record(record) }
  else
    return self.for_record(records)
  end
end

Instance Method Details

#respond_to_missing?(name) ⇒ Boolean

Returns:

  • (Boolean)


256
257
258
# File 'lib/praxis-mapper/resource.rb', line 256

def respond_to_missing?(name,*)
  @record.respond_to?(name) || super
end