Class: Flexirest::BaseWithoutValidation
- Inherits:
-
Object
- Object
- Flexirest::BaseWithoutValidation
- Includes:
- Associations, AttributeParsing, Caching, Callbacks, Configuration, Mapping, Recording
- Defined in:
- lib/flexirest/base_without_validation.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#_etag ⇒ Object
Returns the value of attribute _etag.
-
#_headers ⇒ Object
Returns the value of attribute _headers.
-
#_status ⇒ Object
Returns the value of attribute _status.
Class Method Summary collapse
- ._lazy_request(request, method = :get, params = nil, options = {}) ⇒ Object
- ._plain_request(request, method = :get, params = nil, options = {}) ⇒ Object
- ._request(request, method = :get, params = nil, options = {}) ⇒ Object
- ._request_for(method_name, *args) ⇒ Object
- .prepare_direct_request(request, method = :get, options = {}) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #_attributes ⇒ Object
- #_clean! ⇒ Object
- #_copy_from(result) ⇒ Object
-
#changed ⇒ Object
Returns an array of changed fields.
- #changed? ⇒ Boolean
-
#changes ⇒ Object
Returns hash of old and new vaules for each changed field.
- #dirty? ⇒ Boolean
- #each ⇒ Object
-
#initialize(attrs = {}) ⇒ BaseWithoutValidation
constructor
A new instance of BaseWithoutValidation.
- #inspect ⇒ Object
- #method_missing(name, *args) ⇒ Object
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #to_hash ⇒ Object
- #to_json ⇒ Object
Methods included from Associations
Methods included from Recording
Methods included from Caching
Methods included from Callbacks
Methods included from Configuration
Methods included from Mapping
Constructor Details
#initialize(attrs = {}) ⇒ BaseWithoutValidation
Returns a new instance of BaseWithoutValidation.
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/flexirest/base_without_validation.rb', line 20 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
123 124 125 126 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 |
# File 'lib/flexirest/base_without_validation.rb', line 123 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
#_etag ⇒ Object
Returns the value of attribute _etag.
12 13 14 |
# File 'lib/flexirest/base_without_validation.rb', line 12 def _etag @_etag end |
#_headers ⇒ Object
Returns the value of attribute _headers.
13 14 15 |
# File 'lib/flexirest/base_without_validation.rb', line 13 def _headers @_headers end |
#_status ⇒ Object
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
72 73 74 |
# File 'lib/flexirest/base_without_validation.rb', line 72 def self._lazy_request(request, method = :get, params = nil, = {}) Flexirest::LazyLoader.new(prepare_direct_request(request, method, ), params) end |
._plain_request(request, method = :get, params = nil, options = {}) ⇒ Object
68 69 70 |
# File 'lib/flexirest/base_without_validation.rb', line 68 def self._plain_request(request, method = :get, params = nil, = {}) prepare_direct_request(request, method, .merge(plain:true)).call(params) end |
._request(request, method = :get, params = nil, options = {}) ⇒ Object
64 65 66 |
# File 'lib/flexirest/base_without_validation.rb', line 64 def self._request(request, method = :get, params = nil, = {}) prepare_direct_request(request, method, ).call(params) end |
._request_for(method_name, *args) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/flexirest/base_without_validation.rb', line 85 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
76 77 78 79 80 81 82 83 |
# File 'lib/flexirest/base_without_validation.rb', line 76 def self.prepare_direct_request(request, method = :get, ={}) unless request.is_a? Flexirest::Request [:plain] ||= false [:direct] ||= true request = Flexirest::Request.new({ url: request, method: method, options: }, self) end request end |
Instance Method Details
#[](key) ⇒ Object
95 96 97 |
# File 'lib/flexirest/base_without_validation.rb', line 95 def [](key) @attributes[key.to_sym] end |
#[]=(key, value) ⇒ Object
99 100 101 |
# File 'lib/flexirest/base_without_validation.rb', line 99 def []=(key, value) _set_attribute(key, value) end |
#_attributes ⇒ Object
37 38 39 |
# File 'lib/flexirest/base_without_validation.rb', line 37 def _attributes @attributes end |
#_clean! ⇒ Object
33 34 35 |
# File 'lib/flexirest/base_without_validation.rb', line 33 def _clean! @dirty_attributes = Hash.new end |
#_copy_from(result) ⇒ Object
41 42 43 44 |
# File 'lib/flexirest/base_without_validation.rb', line 41 def _copy_from(result) @attributes = result._attributes @_status = result._status end |
#changed ⇒ Object
Returns an array of changed fields
55 56 57 |
# File 'lib/flexirest/base_without_validation.rb', line 55 def changed @dirty_attributes.keys end |
#changed? ⇒ Boolean
50 51 52 |
# File 'lib/flexirest/base_without_validation.rb', line 50 def changed? dirty? end |
#changes ⇒ Object
Returns hash of old and new vaules for each changed field
60 61 62 |
# File 'lib/flexirest/base_without_validation.rb', line 60 def changes @dirty_attributes end |
#dirty? ⇒ Boolean
46 47 48 |
# File 'lib/flexirest/base_without_validation.rb', line 46 def dirty? @dirty_attributes.size > 0 end |
#each ⇒ Object
103 104 105 106 107 |
# File 'lib/flexirest/base_without_validation.rb', line 103 def each @attributes.each do |key, value| yield key, value end end |
#inspect ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/flexirest/base_without_validation.rb', line 109 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
163 164 165 |
# File 'lib/flexirest/base_without_validation.rb', line 163 def respond_to_missing?(method_name, include_private = false) @attributes.has_key? method_name.to_sym end |
#to_hash ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/flexirest/base_without_validation.rb', line 167 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_json ⇒ Object
181 182 183 184 |
# File 'lib/flexirest/base_without_validation.rb', line 181 def to_json output = to_hash output.to_json end |