Class: Charging::Base
- Inherits:
-
Object
- Object
- Charging::Base
- Defined in:
- lib/charging/base.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_PAGE =
1
- DEFAULT_LIMIT =
10
- COMMON_ATTRIBUTES =
[:uuid, :uri, :etag]
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#last_response ⇒ Object
readonly
Returns the value of attribute last_response.
Class Method Summary collapse
Instance Method Summary collapse
-
#attributes ⇒ Object
Returns a hash with attributes.
- #create!(&block) ⇒ Object
-
#deleted? ⇒ Boolean
Returns true if object already deleted on API.
- #destroy!(&block) ⇒ Object
-
#initialize(attributes, response) ⇒ Base
constructor
A new instance of Base.
- #normalize_etag! ⇒ Object
-
#persisted? ⇒ Boolean
Returns true if the object exists on Charging service.
-
#unpersisted? ⇒ Boolean
Returns true if the object exists on Charging service.
Constructor Details
#initialize(attributes, response) ⇒ Base
Returns a new instance of Base.
13 14 15 16 17 18 19 20 21 |
# File 'lib/charging/base.rb', line 13 def initialize(attributes, response) Helpers.load_variables(self, get_attributes, attributes) @last_response = response @errors = [] @deleted = false normalize_etag! end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
10 11 12 |
# File 'lib/charging/base.rb', line 10 def errors @errors end |
#last_response ⇒ Object (readonly)
Returns the value of attribute last_response.
10 11 12 |
# File 'lib/charging/base.rb', line 10 def last_response @last_response end |
Class Method Details
.validate_attributes!(attributes) ⇒ Object
:nodoc:
72 73 74 75 76 |
# File 'lib/charging/base.rb', line 72 def self.validate_attributes!(attributes) # :nodoc: keys = attributes.keys.map(&:to_sym) diff = keys - (const_get(:ATTRIBUTES) + const_get(:READ_ONLY_ATTRIBUTES) + COMMON_ATTRIBUTES) raise ArgumentError, "Invalid attributes for #{self.name}: #{attributes.inspect}" if diff.any? end |
Instance Method Details
#attributes ⇒ Object
Returns a hash with attributes
68 69 70 |
# File 'lib/charging/base.rb', line 68 def attributes Helpers.hashify(self, self.class::ATTRIBUTES) end |
#create!(&block) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/charging/base.rb', line 23 def create!(&block) execute_and_capture_raises_at_errors(201) do @last_response = block.call end self end |
#deleted? ⇒ Boolean
Returns true if object already deleted on API
63 64 65 |
# File 'lib/charging/base.rb', line 63 def deleted? @deleted || false end |
#destroy!(&block) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/charging/base.rb', line 31 def destroy!(&block) execute_and_capture_raises_at_errors(204) do @last_response = block.call end if errors.empty? @deleted = true @persisted = false end self end |
#normalize_etag! ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/charging/base.rb', line 44 def normalize_etag! if @etag.nil? @etag = last_response.headers[:etag] if last_response && last_response.code === 200 else @etag = @etag.inspect end end |
#persisted? ⇒ Boolean
Returns true if the object exists on Charging service.
53 54 55 |
# File 'lib/charging/base.rb', line 53 def persisted? (uuid && etag && uri && !deleted?) || false end |
#unpersisted? ⇒ Boolean
Returns true if the object exists on Charging service.
58 59 60 |
# File 'lib/charging/base.rb', line 58 def unpersisted? !persisted? end |