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)



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ncore/attributes.rb', line 110

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.



57
58
59
# File 'lib/ncore/attributes.rb', line 57

def api_creds
  @api_creds
end

#errorsObject

Returns the value of attribute errors.



58
59
60
# File 'lib/ncore/attributes.rb', line 58

def errors
  @errors
end

#metadataObject

Returns the value of attribute metadata.



58
59
60
# File 'lib/ncore/attributes.rb', line 58

def 
  
end

Instance Method Details

#[](attr) ⇒ Object



84
85
86
# File 'lib/ncore/attributes.rb', line 84

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

#attributesObject



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

def attributes
  Util.deep_clone(@attribs)
end

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ncore/attributes.rb', line 61

def initialize(attribs={}, api_creds=nil)
  @attribs = {}.with_indifferent_access
  @api_creds = api_creds

  attribs = attribs.dup.with_indifferent_access
  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

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



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ncore/attributes.rb', line 94

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