Class: Arrest::AbstractResource

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks, ActiveModel::Naming, ActiveModel::Translation
Includes:
ActiveModel::Conversion, ActiveModel::Validations, ActiveModel::Validations::Callbacks, BelongsTo, HasAttributes, HasMany, HasView
Defined in:
lib/arrest/abstract_resource.rb

Direct Known Subclasses

RootResource

Constant Summary collapse

@@POLYMORPHIC_TYPE_MAP =
{}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from HasAttributes

#attribute_values

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BelongsTo

included

Methods included from HasView

included

Methods included from HasAttributes

#attributes, #attributes=, included, #init_from_hash, #initialize_has_attributes, #load_from_stub, #render_field_to_hash?, #reset_dirtiness, #stubbed?, #to_hash, #to_jhash, #update_attributes

Methods included from HasMany

included

Constructor Details

#initialize(context, hash = {}, from_json = false) ⇒ AbstractResource

Returns a new instance of AbstractResource.



140
141
142
143
# File 'lib/arrest/abstract_resource.rb', line 140

def initialize(context, hash={}, from_json = false)
  @context = context
  initialize_has_attributes(hash, from_json)
end

Class Attribute Details

.scopesObject (readonly)

Returns the value of attribute scopes.



27
28
29
# File 'lib/arrest/abstract_resource.rb', line 27

def scopes
  @scopes
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



22
23
24
# File 'lib/arrest/abstract_resource.rb', line 22

def context
  @context
end

#idObject

Returns the value of attribute id.



138
139
140
# File 'lib/arrest/abstract_resource.rb', line 138

def id
  @id
end

Class Method Details

.active_resource_classesObject



127
128
129
# File 'lib/arrest/abstract_resource.rb', line 127

def active_resource_classes
  @@POLYMORPHIC_TYPE_MAP.values
end

.all_filtersObject



100
101
102
103
104
105
106
107
# File 'lib/arrest/abstract_resource.rb', line 100

def all_filters
  all_filters = @filters
  all_filters ||= []
  if superclass.respond_to?('filters') && superclass.filters
    all_fields += superclass.filters
  end
  all_filters
end

.body_root(response) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/arrest/abstract_resource.rb', line 42

def body_root(response)
  ::ActiveSupport::Notifications.instrument("parse.sgdb",
                                        :length => response.length) do
    if response == nil
      raise Errors::DocumentNotFoundError
    end
    all = JSON.parse(response)
    body = all["result"]
    if body == nil
      raise Errors::DocumentNotFoundError
    end
    body
  end
end

.build(context, hash) ⇒ Object



57
58
59
60
# File 'lib/arrest/abstract_resource.rb', line 57

def build(context, hash)
  resource = self.new(context, hash, true)
  resource
end

.custom_json_type(new_key) ⇒ Object



113
114
115
116
117
# File 'lib/arrest/abstract_resource.rb', line 113

def custom_json_type(new_key)
  old_key = Arrest::ClassUtils.simple_name(self).to_sym
  value = @@POLYMORPHIC_TYPE_MAP.delete(old_key) # remove default key for self
  @@POLYMORPHIC_TYPE_MAP[new_key.to_sym] = value # add custom key for type
end

.custom_resource_name(new_name) ⇒ Object



62
63
64
# File 'lib/arrest/abstract_resource.rb', line 62

def custom_resource_name(new_name)
  @custom_resource_name = new_name
end

.filtersObject



96
97
98
# File 'lib/arrest/abstract_resource.rb', line 96

def filters
  @filters
end

.inherited(child) ⇒ Object



29
30
31
32
# File 'lib/arrest/abstract_resource.rb', line 29

def inherited(child)
  ScopedRoot::register_resource(child)
  @@POLYMORPHIC_TYPE_MAP[ClassUtils.simple_name(child).to_sym] = child
end

.json_type_mapObject



109
110
111
# File 'lib/arrest/abstract_resource.rb', line 109

def json_type_map
  @@POLYMORPHIC_TYPE_MAP
end

.json_type_to_class(type) ⇒ Object



119
120
121
# File 'lib/arrest/abstract_resource.rb', line 119

def json_type_to_class(type)
  @@POLYMORPHIC_TYPE_MAP[type.to_sym]
end

.mk_proxy(context_provider) ⇒ Object



34
35
36
# File 'lib/arrest/abstract_resource.rb', line 34

def mk_proxy(context_provider)
  ResourceProxy.new(self, context_provider)
end

.parent(*args) ⇒ Object



76
77
78
79
# File 'lib/arrest/abstract_resource.rb', line 76

def parent(*args)
  method_name = args[0].to_s.to_sym
  class_eval "def #{method_name}; self.parent; end"
end

.read_only_attributes(args) ⇒ Object



89
90
91
92
93
94
# File 'lib/arrest/abstract_resource.rb', line 89

def read_only_attributes(args)
  args.each_pair do |name, clazz|
    self.send :attr_accessor,name
    add_attribute(Attribute.new(name, clazz, [:retrieve]))
  end
end

.resource_nameObject



66
67
68
69
70
71
72
73
74
# File 'lib/arrest/abstract_resource.rb', line 66

def resource_name
  if @custom_resource_name
    @custom_resource_name
  else
    simple_name = ClassUtils.simple_name(self)
    usd_name = StringUtils.underscore(simple_name)
    StringUtils.plural(usd_name)
  end
end

.scope(name, options = {}, &block) ⇒ Object



81
82
83
84
85
86
# File 'lib/arrest/abstract_resource.rb', line 81

def scope(name, options = {}, &block)
  if @scopes == nil
    @scopes = []
  end
  @scopes << Scope.new(name, options, &block)
end

.sourceObject



38
39
40
# File 'lib/arrest/abstract_resource.rb', line 38

def source
  Arrest::Source::source
end

.to_json_typeObject



123
124
125
# File 'lib/arrest/abstract_resource.rb', line 123

def to_json_type
  @@POLYMORPHIC_TYPE_MAP.invert[self]
end

Instance Method Details

#==(comparison_object) ⇒ Object



193
194
195
196
197
# File 'lib/arrest/abstract_resource.rb', line 193

def == (comparison_object)
  other_class_name = comparison_object.class.name if comparison_object
  other_id = comparison_object.id if comparison_object
  self.class.name == other_class_name && self.id == other_id
end

#cloneObject



161
162
163
# File 'lib/arrest/abstract_resource.rb', line 161

def clone
  self.class.new(self.context, self.to_hash)
end

#curlObject

convenience method printing curl command



184
185
186
187
188
189
190
191
# File 'lib/arrest/abstract_resource.rb', line 184

def curl
  hs = ""
  self.context.header_decorator.headers.each_pair do |k,v|
    hs << " -H '#{k}:#{v}' "
  end

  "curl #{hs} -v '#{Arrest::Source.source.url}/#{self.resource_location}'"
end

#deleteObject



178
179
180
181
# File 'lib/arrest/abstract_resource.rb', line 178

def delete
  self.context.cache.flush
  AbstractResource::source().delete(@context, self)
end

#new_record?Boolean

Returns:



174
175
176
# File 'lib/arrest/abstract_resource.rb', line 174

def new_record?
  [nil, ''].include?(id)
end

#reloadObject



165
166
167
168
169
170
171
172
# File 'lib/arrest/abstract_resource.rb', line 165

def reload
  @child_collections = {}
  @views = {}
  @belongs_tos = {}
  hash = internal_reload
  self.attributes= hash
  self.reset_dirtiness
end

#saveObject



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/arrest/abstract_resource.rb', line 145

def save
  if Source.skip_validations || self.valid?
    req_type = new_record? ? :post : :put
    success = !!AbstractResource::source.send(req_type, @context, self)
    self.context.cache.flush
    self.reset_dirtiness
    success
  else
    false
  end
end

#save!Object



157
158
159
# File 'lib/arrest/abstract_resource.rb', line 157

def save!
  raise self.errors.inspect unless self.save
end

#to_json_typeObject



132
133
134
# File 'lib/arrest/abstract_resource.rb', line 132

def to_json_type
  self.class.to_json_type
end