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

#response ⇒ Object (readonly)

Returns the value of attribute response.



70
71
72
# File 'lib/strobe/resource/base.rb', line 70

def response
  @response
end

Instance Method Details

#[](key) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/strobe/resource/base.rb', line 106

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|
      param[key]
    end
  end
end

#[]=(key, val) ⇒ Object



124
125
126
127
128
# File 'lib/strobe/resource/base.rb', line 124

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

#destroy ⇒ Object

Raises:

  • (NotImplemented)


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

def destroy
  raise NotImplemented
end

#initialize(params = {}) ⇒ Object



72
73
74
75
# File 'lib/strobe/resource/base.rb', line 72

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

#key?(key) ⇒ Boolean

TODO: Make this legit

Returns:

  • (Boolean)


82
83
84
# File 'lib/strobe/resource/base.rb', line 82

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

#merge!(params) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/strobe/resource/base.rb', line 130

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

  extract_on_associations(params).each do |k, v|
    unless inst = send(k)
      inst = send("#{k}=", {})
    end
    inst.merge! v
  end

  @params.deep_merge! params
end

#params ⇒ Object



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

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

#params=(val) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/strobe/resource/base.rb', line 95

def params=(val)
  params = with_indifferent_access(val)

  extract_on_associations(params).each do |k, v|
    send("#{k}=", v)
  end

  @params = params
  val
end

#persisted? ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/strobe/resource/base.rb', line 77

def persisted?
  self[:id]
end

#save ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/strobe/resource/base.rb', line 147

def save
  if valid?
    root      = self.class.singular_resource_name
    @response = self["id"] ? update : create

    if @response.success?
      merge! @response.body[root]
      return true
    elsif @response.status == 422
      errors = @response.body['errors']
      errors = errors[root] if errors
      handle_errors(errors) if errors
    else
      raise "Something went wrong"
    end

    false
  end
end