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)



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/ncore/attributes.rb', line 133

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.



79
80
81
# File 'lib/ncore/attributes.rb', line 79

def api_creds
  @api_creds
end

#errorsObject

Returns the value of attribute errors.



80
81
82
# File 'lib/ncore/attributes.rb', line 80

def errors
  @errors
end

#metadataObject

Returns the value of attribute metadata.



80
81
82
# File 'lib/ncore/attributes.rb', line 80

def 
  @metadata
end

Instance Method Details

#[](attr) ⇒ Object



107
108
109
# File 'lib/ncore/attributes.rb', line 107

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

#attributesObject



102
103
104
# File 'lib/ncore/attributes.rb', line 102

def attributes
  Util.deep_clone(@attribs)
end

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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ncore/attributes.rb', line 83

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

  if attribs.keys.sort == %w(data error metadata)
    load_attrs = attribs
  else
    load_attrs = {
      metadata: attribs.delete(:metadata),
      errors: attribs.delete(:errors),
      data: attribs.delete(:data) || attribs
    }
  end
  load(load_attrs)
end

#parse_request_params(params, opts = {}) ⇒ Object



74
75
76
# File 'lib/ncore/attributes.rb', line 74

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

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

Returns:

  • (Boolean)


117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ncore/attributes.rb', line 117

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