AWS services or capabilities described in AWS Documentation may vary by region/location. Click Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

Class: AwsRecord::Generators::ActiveModel

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/aws_record/active_model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ ActiveModel

Returns a new instance of ActiveModel.



19
20
21
# File 'lib/generators/aws_record/active_model.rb', line 19

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/generators/aws_record/active_model.rb', line 17

def name
  @name
end

Class Method Details

.all(klass) ⇒ Object

GET index



24
25
26
# File 'lib/generators/aws_record/active_model.rb', line 24

def self.all(klass)
  "#{klass}.scan"
end

.build(klass, params = nil) ⇒ Object

GET new POST create



49
50
51
52
53
54
55
# File 'lib/generators/aws_record/active_model.rb', line 49

def self.build(klass, params = nil)
  if params
    "#{klass}.new(#{params})"
  else
    "#{klass}.new"
  end
end

.find(klass, attrs) ⇒ Object

GET show GET edit PATCH/PUT update DELETE destroy



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/generators/aws_record/active_model.rb', line 32

def self.find(klass, attrs)
  hkey = attrs.select{|attr| attr.options[:hash_key]}[0]
  rkey = attrs.select{|attr| attr.options[:range_key]}
  rkey = !rkey.empty? ? rkey[0] : nil

  if rkey
    """lambda {
      id = params[:id].split('&').map{ |param| CGI.unescape(param) }
      #{klass}.find(#{hkey.name}: id[0], #{rkey.name}: id[1])
    }.call()"""
  else
    "#{klass}.find(#{hkey.name}: CGI.unescape(params[:id]))"
  end
end

Instance Method Details

#destroyObject

DELETE destroy



74
75
76
# File 'lib/generators/aws_record/active_model.rb', line 74

def destroy
  "#{name}.delete!"
end

#errorsObject

POST create PATCH/PUT update



69
70
71
# File 'lib/generators/aws_record/active_model.rb', line 69

def errors
  "#{name}.errors"
end

#saveObject

POST create



58
59
60
# File 'lib/generators/aws_record/active_model.rb', line 58

def save
  "#{name}.save"
end

#update(params = nil) ⇒ Object

PATCH/PUT update



63
64
65
# File 'lib/generators/aws_record/active_model.rb', line 63

def update(params = nil)
  "#{name}.update(#{params})"
end