Module: NCore::Attributes
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/ncore/attributes.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- KNOWN_FALSE_TRIGGERS =
Method names known to cause strange behavior in other libraries where said libraries check for these methods to determine other behavior.
%w(map each)
Instance Attribute Summary collapse
-
#api_creds ⇒ Object
Returns the value of attribute api_creds.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
Instance Method Summary collapse
- #[](attr) ⇒ Object
- #attributes ⇒ Object (also: #as_json)
- #initialize(attribs = {}, api_creds = nil) ⇒ Object
- #parse_request_params(params, opts = {}) ⇒ Object
- #respond_to?(method, incl_private = false) ⇒ Boolean
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (private)
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/ncore/attributes.rb', line 141 def method_missing(method, *args, &block) case method.to_s when /(.+)\?$/ if @attribs.has_key?($1) || respond_to?($1.to_sym) !! self[$1] else super end when /\=$/ super else if @attribs.has_key?(method) || !self.class.strict_attributes self[method] else super end end end |
Instance Attribute Details
#api_creds ⇒ Object
Returns the value of attribute api_creds.
92 93 94 |
# File 'lib/ncore/attributes.rb', line 92 def api_creds @api_creds end |
#errors ⇒ Object
Returns the value of attribute errors.
93 94 95 |
# File 'lib/ncore/attributes.rb', line 93 def errors @errors end |
#metadata ⇒ Object
Returns the value of attribute metadata.
93 94 95 |
# File 'lib/ncore/attributes.rb', line 93 def end |
Instance Method Details
#[](attr) ⇒ Object
115 116 117 |
# File 'lib/ncore/attributes.rb', line 115 def [](attr) @attribs[attr] end |
#attributes ⇒ Object Also known as: as_json
110 111 112 |
# File 'lib/ncore/attributes.rb', line 110 def attributes Util.deep_clone(@attribs) end |
#initialize(attribs = {}, api_creds = nil) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ncore/attributes.rb', line 96 def initialize(attribs={}, api_creds=nil) @attribs = {}.with_indifferent_access attribs = attribs.dup.with_indifferent_access creds_attr = attribs.delete(:credentials) @api_creds = api_creds || creds_attr load( metadata: attribs.delete(:metadata), errors: attribs.delete(:errors), data: attribs.delete(:data) || attribs ) end |
#parse_request_params(params, opts = {}) ⇒ Object
87 88 89 |
# File 'lib/ncore/attributes.rb', line 87 def parse_request_params(params, opts={}) self.class.parse_request_params(params, opts) end |
#respond_to?(method, incl_private = false) ⇒ Boolean
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/ncore/attributes.rb', line 125 def respond_to?(method, incl_private=false) m2 = method.to_s.sub(/(\?)$/,'') if method.to_s =~ /\=$/ super elsif @attribs.has_key?(m2) true elsif !self.class.strict_attributes && !KNOWN_FALSE_TRIGGERS.include?(m2) true else super end end |