Class: Poncho::Resource
- Inherits:
-
Object
- Object
- Poncho::Resource
- Includes:
- Params, Validations
- Defined in:
- lib/poncho/resource.rb
Instance Attribute Summary collapse
-
#record ⇒ Object
readonly
Returns the value of attribute record.
Instance Method Summary collapse
- #as_json(options = nil) ⇒ Object
-
#each ⇒ Object
Serialization.
-
#initialize(record) ⇒ Resource
constructor
A new instance of Resource.
-
#method_missing(method_symbol, *arguments) ⇒ Object
:nodoc:.
-
#param(name) ⇒ Object
Params.
- #param_before_type_cast(name) ⇒ Object
-
#param_for_validation?(name) ⇒ Boolean
We want to validate an attribute if its uncoerced value is not nil.
- #to_hash ⇒ Object (also: #describe)
- #to_json ⇒ Object
- #validate! ⇒ Object
Methods included from Validations
#errors, included, #invalid?, #valid?
Methods included from Params
Constructor Details
#initialize(record) ⇒ Resource
Returns a new instance of Resource.
8 9 10 11 12 13 14 |
# File 'lib/poncho/resource.rb', line 8 def initialize(record) if record.nil? raise ResourceValidationError, 'Invalid nil record' end @record = record end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_symbol, *arguments) ⇒ Object
:nodoc:
72 73 74 75 76 77 78 |
# File 'lib/poncho/resource.rb', line 72 def method_missing(method_symbol, *arguments) #:nodoc: if self.class.params.keys.include?(method_symbol) return param(method_symbol) end super end |
Instance Attribute Details
#record ⇒ Object (readonly)
Returns the value of attribute record.
6 7 8 |
# File 'lib/poncho/resource.rb', line 6 def record @record end |
Instance Method Details
#as_json(options = nil) ⇒ Object
40 41 42 43 |
# File 'lib/poncho/resource.rb', line 40 def as_json( = nil) validate! to_hash end |
#each ⇒ Object
Serialization
32 33 34 |
# File 'lib/poncho/resource.rb', line 32 def each [to_json] end |
#param(name) ⇒ Object
Params
18 19 20 21 22 |
# File 'lib/poncho/resource.rb', line 18 def param(name) param = self.class.params[name] raise Error, "Undefined param #{name}" unless param param.convert(param_before_type_cast(name)) end |
#param_before_type_cast(name) ⇒ Object
24 25 26 |
# File 'lib/poncho/resource.rb', line 24 def param_before_type_cast(name) record.send(name) if record.respond_to?(name) end |
#param_for_validation?(name) ⇒ Boolean
We want to validate an attribute if its uncoerced value is not nil
58 59 60 61 62 63 64 |
# File 'lib/poncho/resource.rb', line 58 def param_for_validation?(name) if respond_to?(name) !send(name).nil? else !param_before_type_cast(name).nil? end end |
#to_hash ⇒ Object Also known as: describe
45 46 47 48 49 50 |
# File 'lib/poncho/resource.rb', line 45 def to_hash self.class.params.keys.inject({}) do |hash, key| hash[key] = send(key) hash end end |
#to_json ⇒ Object
36 37 38 |
# File 'lib/poncho/resource.rb', line 36 def to_json(*) as_json.to_json end |
#validate! ⇒ Object
68 69 70 |
# File 'lib/poncho/resource.rb', line 68 def validate! raise ResourceValidationError, errors.to_s unless valid? end |