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, options = {}) ⇒ 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)
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/ncore/attributes.rb', line 196 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.
142 143 144 |
# File 'lib/ncore/attributes.rb', line 142 def api_creds @api_creds end |
#errors ⇒ Object
Returns the value of attribute errors.
143 144 145 |
# File 'lib/ncore/attributes.rb', line 143 def errors @errors end |
#metadata ⇒ Object
Returns the value of attribute metadata.
143 144 145 |
# File 'lib/ncore/attributes.rb', line 143 def @metadata end |
Instance Method Details
#[](attr) ⇒ Object
170 171 172 |
# File 'lib/ncore/attributes.rb', line 170 def [](attr) @attribs[attr] end |
#attributes ⇒ Object Also known as: as_json
165 166 167 |
# File 'lib/ncore/attributes.rb', line 165 def attributes Util.deep_clone(@attribs) end |
#initialize(attribs = {}, api_creds = nil, options = {}) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/ncore/attributes.rb', line 146 def initialize(attribs={}, api_creds=nil, ={}) @attribs = {}.with_indifferent_access attribs = attribs.dup.with_indifferent_access preload = [:preload].present? ? [:preload].dup.with_indifferent_access : nil creds_attr = attribs.delete(:credentials) @api_creds = api_creds || creds_attr if preload load(data: preload.delete(:data) || preload.except(:credentials, :metadata, :errors)) end load( metadata: attribs.delete(:metadata), errors: attribs.delete(:errors), data: attribs.delete(:data) || attribs ) end |
#parse_request_params(params, opts = {}) ⇒ Object
137 138 139 |
# File 'lib/ncore/attributes.rb', line 137 def parse_request_params(params, opts={}) self.class.parse_request_params(params, opts) end |
#respond_to?(method, incl_private = false) ⇒ Boolean
180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/ncore/attributes.rb', line 180 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 |