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

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/ncore/attributes.rb', line 174

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_credsObject

Returns the value of attribute api_creds.



120
121
122
# File 'lib/ncore/attributes.rb', line 120

def api_creds
  @api_creds
end

#errorsObject

Returns the value of attribute errors.



121
122
123
# File 'lib/ncore/attributes.rb', line 121

def errors
  @errors
end

#metadataObject

Returns the value of attribute metadata.



121
122
123
# File 'lib/ncore/attributes.rb', line 121

def 
  @metadata
end

Instance Method Details

#[](attr) ⇒ Object



148
149
150
# File 'lib/ncore/attributes.rb', line 148

def [](attr)
  @attribs[attr]
end

#attributesObject Also known as: as_json



143
144
145
# File 'lib/ncore/attributes.rb', line 143

def attributes
  Util.deep_clone(@attribs)
end

#initialize(attribs = {}, api_creds = nil, options = {}) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ncore/attributes.rb', line 124

def initialize(attribs={}, api_creds=nil, options={})
  @attribs   = {}.with_indifferent_access
  attribs    = attribs.dup.with_indifferent_access
  preload    = options[:preload].present? ? options[: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



115
116
117
# File 'lib/ncore/attributes.rb', line 115

def parse_request_params(params, opts={})
  self.class.parse_request_params(params, opts)
end

#respond_to?(method, incl_private = false) ⇒ Boolean

Returns:

  • (Boolean)


158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ncore/attributes.rb', line 158

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