Module: Strobe::Resource::Base

Extended by:
ActiveSupport::Concern
Includes:
Validations
Included in:
Collection, Singleton
Defined in:
lib/strobe/resource/base.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validations

#read_attribute_for_validation, #valid_attribute?, #valid_for_given_attributes?

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response


86
87
88
# File 'lib/strobe/resource/base.rb', line 86

def response
  @response
end

Instance Method Details

#[](key) ⇒ Object


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/strobe/resource/base.rb', line 118

def [](key)
  if Array === key
    parts = key
  else
    parts = key.to_s.split('.')
  end

  if self.class.associations.key?(parts.first.to_sym)
    return unless inst = send(parts.first)
    return inst if parts.length == 1
    inst[parts[1..-1]]
  else
    parts.inject(params) do |param, key|
      return unless param
      param[key]
    end
  end
end

#[]=(key, val) ⇒ Object


137
138
139
140
141
# File 'lib/strobe/resource/base.rb', line 137

def []=(key, val)
  merge!(key.to_s.split('.').reverse.inject(val) do |param, key|
    { key => param }
  end)
end

#destroyObject


176
177
178
179
180
181
# File 'lib/strobe/resource/base.rb', line 176

def destroy
  if request { delete }
    IdentityMap.forget(self.class, key)
    true
  end
end

#initialize(params = {}) ⇒ Object


88
89
90
91
92
# File 'lib/strobe/resource/base.rb', line 88

def initialize(params = {})
  @response   = nil
  @params     = with_indifferent_access({})
  self.params = params || {}
end

#key?(key) ⇒ Boolean

TODO: Make this legit

Returns:

  • (Boolean)

99
100
101
# File 'lib/strobe/resource/base.rb', line 99

def key?(key)
  params.key?(key)
end

#merge!(params) ⇒ Object


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/strobe/resource/base.rb', line 143

def merge!(params)
  params = with_indifferent_access(params || {})

  self.class.associations.each do |key, assoc|
    next unless params.key?(key)
    value = params.delete(key)

    if assoc.collection? || Base === value
      send("#{key}=", value)
    elsif inst = send(key)
      inst.merge!(value)
    else
      send("#{key}=", value)
    end
  end

  @params.deep_merge! params
end

#paramsObject


103
104
105
106
107
108
109
110
# File 'lib/strobe/resource/base.rb', line 103

def params
  @params.dup.tap do |params|
    self.class.associations.each do |name, assoc|
      next unless assoc.nested? and inst = send(name)
      params.deep_merge! name => inst.params
    end
  end
end

#params=(val) ⇒ Object


112
113
114
115
116
# File 'lib/strobe/resource/base.rb', line 112

def params=(val)
  @params = with_indifferent_access({})
  merge!(val)
  val
end

#persisted?Boolean

Returns:

  • (Boolean)

94
95
96
# File 'lib/strobe/resource/base.rb', line 94

def persisted?
  self[:id]
end

#saveObject


166
167
168
169
170
# File 'lib/strobe/resource/base.rb', line 166

def save
  if valid?
    request { self["id"] ? update : create }
  end
end

#save!Object


172
173
174
# File 'lib/strobe/resource/base.rb', line 172

def save!
  save or raise ValidationError, "could not validate: #{errors.full_messages}"
end