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.
-
#_parent ⇒ Object
Returns the value of attribute _parent.
-
#_parent_attribute_name ⇒ Object
Returns the value of attribute _parent_attribute_name.
-
#_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
- #_set_dirty(key) ⇒ Object
-
#changed ⇒ Object
Returns an array of changed fields.
- #changed? ⇒ Boolean
-
#changes ⇒ Object
Returns hash of old and new values 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.
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
#_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 |
#_parent ⇒ Object
Returns the value of attribute _parent.
14 15 16 |
# File 'lib/flexirest/base_without_validation.rb', line 14 def _parent @_parent end |
#_parent_attribute_name ⇒ Object
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 |
#_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
82 83 84 |
# File 'lib/flexirest/base_without_validation.rb', line 82 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
78 79 80 |
# File 'lib/flexirest/base_without_validation.rb', line 78 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
74 75 76 |
# File 'lib/flexirest/base_without_validation.rb', line 74 def self._request(request, method = :get, params = nil, = {}) prepare_direct_request(request, method, ).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, ={}) 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
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 |
#_attributes ⇒ Object
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 |
#changed ⇒ Object
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
60 61 62 |
# File 'lib/flexirest/base_without_validation.rb', line 60 def changed? dirty? end |
#changes ⇒ Object
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
56 57 58 |
# File 'lib/flexirest/base_without_validation.rb', line 56 def dirty? @dirty_attributes.size > 0 end |
#each ⇒ Object
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 |
#inspect ⇒ Object
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
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_hash ⇒ Object
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_json ⇒ Object
191 192 193 194 |
# File 'lib/flexirest/base_without_validation.rb', line 191 def to_json output = to_hash output.to_json end |