Class: Flexirest::Base

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

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 Validation

#_errors, #full_error_messages, included, #valid?

Methods included from Callbacks

included

Methods included from Configuration

included

Methods included from Mapping

included

Constructor Details

#initialize(attrs = {}) ⇒ Base

Returns a new instance of Base.

Raises:

  • (Exception)


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

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

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

  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



127
128
129
130
131
132
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
# File 'lib/flexirest/base.rb', line 127

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 unless 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 unless 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.



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

def _etag
  @_etag
end

#_headersObject

Returns the value of attribute _headers.



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

def _headers
  @_headers
end

#_statusObject

Returns the value of attribute _status.



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

def _status
  @_status
end

Class Method Details

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



77
78
79
# File 'lib/flexirest/base.rb', line 77

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



73
74
75
# File 'lib/flexirest/base.rb', line 73

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



69
70
71
# File 'lib/flexirest/base.rb', line 69

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

._request_for(method_name, *args) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/flexirest/base.rb', line 89

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



81
82
83
84
85
86
87
# File 'lib/flexirest/base.rb', line 81

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

Instance Method Details

#[](key) ⇒ Object



99
100
101
# File 'lib/flexirest/base.rb', line 99

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

#[]=(key, value) ⇒ Object



103
104
105
# File 'lib/flexirest/base.rb', line 103

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

#_attributesObject



38
39
40
# File 'lib/flexirest/base.rb', line 38

def _attributes
  @attributes
end

#_clean!Object



34
35
36
# File 'lib/flexirest/base.rb', line 34

def _clean!
  @dirty_attributes = Hash.new
end

#_copy_from(result) ⇒ Object



42
43
44
45
# File 'lib/flexirest/base.rb', line 42

def _copy_from(result)
  @attributes = result._attributes
  @_status = result._status
end

#changedObject

Returns an array of changed fields



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

def changed
  @dirty_attributes.keys
end

#changed?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/flexirest/base.rb', line 51

def changed?
  dirty?
end

#changesObject

Returns hash of old and new vaules for each changed field



61
62
63
# File 'lib/flexirest/base.rb', line 61

def changes
  @dirty_attributes
end

#dirty?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/flexirest/base.rb', line 47

def dirty?
  @dirty_attributes.size > 0
end

#eachObject



107
108
109
110
111
# File 'lib/flexirest/base.rb', line 107

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

#errorsObject



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

def errors
  @attributes[:errors] || (_errors != {} ? _errors : nil)
end

#inspectObject



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/flexirest/base.rb', line 113

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)


167
168
169
# File 'lib/flexirest/base.rb', line 167

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

#to_hashObject



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/flexirest/base.rb', line 171

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



185
186
187
188
# File 'lib/flexirest/base.rb', line 185

def to_json
  output = to_hash
  output.to_json
end