Class: Flexirest::BaseWithoutValidation

Inherits:
Object
  • Object
show all
Includes:
Associations, AttributeParsing, Caching, Callbacks, Configuration, Mapping, Recording
Defined in:
lib/flexirest/base_without_validation.rb

Direct Known Subclasses

Base

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Associations

included

Methods included from Recording

included

Methods included from Caching

included

Methods included from Callbacks

included

Methods included from Configuration

included

Methods included from Mapping

included

Constructor Details

#initialize(attrs = {}) ⇒ BaseWithoutValidation

Returns a new instance of BaseWithoutValidation.

Raises:

  • (Exception)


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/flexirest/base_without_validation.rb', line 22

def initialize(attrs={})
  @attributes = {}
  @dirty_attributes = Hash.new

  raise Exception.new("Cannot instantiate Base class") if self.class == Flexirest::BaseWithoutValidation

  attrs.each do |attribute_name, attribute_value|
    attribute_name = attribute_name.to_sym
    @attributes[attribute_name] = parse_date?(attribute_name) ? parse_attribute_value(attribute_value) : attribute_value
    @dirty_attributes[attribute_name] = [nil, attribute_value]
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/flexirest/base_without_validation.rb', line 133

def method_missing(name, *args)
  if name.to_s[-1,1] == "="
    name = name.to_s.chop.to_sym
    _set_attribute(name, args.first)
  else
    name_sym = name.to_sym
    name = name.to_s

    if @attributes.has_key? name_sym
      @attributes[name_sym]
    else
      if name[/^lazy_/] && mapped = self.class._mapped_method(name_sym)
        if mapped[:method] != :delete
          raise ValidationFailedException.new if respond_to?(:valid?) && !valid?
        end

        request = Request.new(mapped, self, args.first)
        Flexirest::LazyLoader.new(request)
      elsif mapped = self.class._mapped_method(name_sym)
        if mapped[:method] != :delete
          raise ValidationFailedException.new if respond_to?(:valid?) && !valid?
        end

        request = Request.new(mapped, self, args.first)
        request.call
      elsif name[/_was$/] and @attributes.has_key? (name.sub(/_was$/,'').to_sym)
        k = (name.sub(/_was$/,'').to_sym)
        @dirty_attributes[k][0]
      elsif name[/^reset_.*!$/] and @attributes.has_key? (name.sub(/^reset_/,'').sub(/!$/,'').to_sym)
        k = (name.sub(/^reset_/,'').sub(/!$/,'').to_sym)
        _reset_attribute(k)
      elsif self.class.whiny_missing
        raise NoAttributeException.new("Missing attribute #{name_sym}")
      else
        nil
      end
    end
  end
end

Instance Attribute Details

#_etagObject

Returns the value of attribute _etag.



12
13
14
# File 'lib/flexirest/base_without_validation.rb', line 12

def _etag
  @_etag
end

#_headersObject

Returns the value of attribute _headers.



13
14
15
# File 'lib/flexirest/base_without_validation.rb', line 13

def _headers
  @_headers
end

#_parentObject

Returns the value of attribute _parent.



14
15
16
# File 'lib/flexirest/base_without_validation.rb', line 14

def _parent
  @_parent
end

#_parent_attribute_nameObject

Returns the value of attribute _parent_attribute_name.



15
16
17
# File 'lib/flexirest/base_without_validation.rb', line 15

def _parent_attribute_name
  @_parent_attribute_name
end

#_statusObject

Returns the value of attribute _status.



11
12
13
# File 'lib/flexirest/base_without_validation.rb', line 11

def _status
  @_status
end

Class Method Details

._lazy_request(request, method = :get, params = nil, options = {}) ⇒ Object



82
83
84
# File 'lib/flexirest/base_without_validation.rb', line 82

def self._lazy_request(request, method = :get, params = nil, options = {})
  Flexirest::LazyLoader.new(prepare_direct_request(request, method, options), params)
end

._plain_request(request, method = :get, params = nil, options = {}) ⇒ Object



78
79
80
# File 'lib/flexirest/base_without_validation.rb', line 78

def self._plain_request(request, method = :get, params = nil, options = {})
  prepare_direct_request(request, method, options.merge(plain:true)).call(params)
end

._request(request, method = :get, params = nil, options = {}) ⇒ Object



74
75
76
# File 'lib/flexirest/base_without_validation.rb', line 74

def self._request(request, method = :get, params = nil, options = {})
  prepare_direct_request(request, method, options).call(params)
end

._request_for(method_name, *args) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/flexirest/base_without_validation.rb', line 95

def self._request_for(method_name, *args)
  if mapped = self._mapped_method(method_name)
    params = (args.first.is_a?(Hash) ? args.first : nil)
    request = Request.new(mapped, self, params)
    request
  else
    nil
  end
end

.prepare_direct_request(request, method = :get, options = {}) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/flexirest/base_without_validation.rb', line 86

def self.prepare_direct_request(request, method = :get, options={})
  unless request.is_a? Flexirest::Request
    options[:plain] ||= false
    options[:direct] ||= true
    request = Flexirest::Request.new({ url: request, method: method, options: options }, self)
  end
  request
end

Instance Method Details

#[](key) ⇒ Object



105
106
107
# File 'lib/flexirest/base_without_validation.rb', line 105

def [](key)
  @attributes[key.to_sym]
end

#[]=(key, value) ⇒ Object



109
110
111
# File 'lib/flexirest/base_without_validation.rb', line 109

def []=(key, value)
  _set_attribute(key, value)
end

#_attributesObject



39
40
41
# File 'lib/flexirest/base_without_validation.rb', line 39

def _attributes
  @attributes
end

#_clean!Object



35
36
37
# File 'lib/flexirest/base_without_validation.rb', line 35

def _clean!
  @dirty_attributes = Hash.new
end

#_copy_from(result) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/flexirest/base_without_validation.rb', line 43

def _copy_from(result)
  @attributes = result._attributes
  @_status = result._status
  self._parent = result._parent
  self._parent_attribute_name = result._parent_attribute_name
  @attributes.each do |k,v|
    if v.respond_to?(:_parent) && v._parent.present?
      @attributes[k]._parent = self
    end
  end
  _clean!
end

#_set_dirty(key) ⇒ Object



196
197
198
# File 'lib/flexirest/base_without_validation.rb', line 196

def _set_dirty(key)
  @dirty_attributes[key.to_sym] = true
end

#changedObject

Returns an array of changed fields



65
66
67
# File 'lib/flexirest/base_without_validation.rb', line 65

def changed
  @dirty_attributes.keys
end

#changed?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/flexirest/base_without_validation.rb', line 60

def changed?
  dirty?
end

#changesObject

Returns hash of old and new values for each changed field



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

def changes
  @dirty_attributes
end

#dirty?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/flexirest/base_without_validation.rb', line 56

def dirty?
  @dirty_attributes.size > 0
end

#eachObject



113
114
115
116
117
# File 'lib/flexirest/base_without_validation.rb', line 113

def each
  @attributes.each do |key, value|
    yield key, value
  end
end

#inspectObject



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/flexirest/base_without_validation.rb', line 119

def inspect
  inspection = if @attributes.any?
                 @attributes.collect { |key, value|
                   "#{key}: #{value_for_inspect(value)}"
                 }.compact.join(", ")
               else
                 "[uninitialized]"
               end
  inspection += "#{"," if @attributes.any?} ETag: #{@_etag}" unless @_etag.nil?
  inspection += "#{"," if @attributes.any?} Status: #{@_status}" unless @_status.nil?
  inspection += " (unsaved: #{@dirty_attributes.keys.map(&:to_s).join(", ")})" if @dirty_attributes.any?
  "#<#{self.class} #{inspection}>"
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/flexirest/base_without_validation.rb', line 173

def respond_to_missing?(method_name, include_private = false)
  @attributes.has_key? method_name.to_sym
end

#to_hashObject



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/flexirest/base_without_validation.rb', line 177

def to_hash
  output = {}
  @attributes.each do |key, value|
    if value.is_a? Flexirest::Base
      output[key.to_s] = value.to_hash
    elsif value.is_a? Array
      output[key.to_s] = value.map(&:to_hash)
    else
      output[key.to_s] = value
    end
  end
  output
end

#to_jsonObject



191
192
193
194
# File 'lib/flexirest/base_without_validation.rb', line 191

def to_json
  output = to_hash
  output.to_json
end