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.



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

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.



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

def scopes
  @scopes
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

Class Method Details

.active_resource_classesObject



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

def active_resource_classes
  @@POLYMORPHIC_TYPE_MAP.values
end

.all_filtersObject



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

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



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

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



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

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

.custom_json_type(new_key) ⇒ Object



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

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



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

def custom_resource_name(new_name)
  @custom_resource_name = new_name
end

.filtersObject



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

def filters
  @filters
end

.inherited(child) ⇒ Object



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

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

.json_type_mapObject



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

def json_type_map
  @@POLYMORPHIC_TYPE_MAP
end

.json_type_to_class(type) ⇒ Object



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

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

.mk_proxy(context_provider) ⇒ Object



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

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

.parent(*args) ⇒ Object



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

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



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

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



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

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



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

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

.sourceObject



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

def source
  Arrest::Source::source
end

.to_json_typeObject



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

def to_json_type
  @@POLYMORPHIC_TYPE_MAP.invert[self]
end

Instance Method Details

#==(comparison_object) ⇒ Object



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

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



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

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

#curlObject

convenience method printing curl command



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

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



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

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

#new_record?Boolean

Returns:



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

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

#reloadObject



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

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

#saveObject



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

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



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

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

#to_json_typeObject



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

def to_json_type
  self.class.to_json_type
end