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.



8
9
10
# File 'lib/generators/aws_record/active_model.rb', line 8

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/generators/aws_record/active_model.rb', line 6

def name
  @name
end

Class Method Details

.all(klass) ⇒ Object

GET index



13
14
15
# File 'lib/generators/aws_record/active_model.rb', line 13

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

.build(klass, params = nil) ⇒ Object

GET new POST create



38
39
40
41
42
43
44
# File 'lib/generators/aws_record/active_model.rb', line 38

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/generators/aws_record/active_model.rb', line 21

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? ? nil : rkey[0]

  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



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

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

#errorsObject

POST create PATCH/PUT update



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

def errors
  "#{name}.errors"
end

#saveObject

POST create



47
48
49
# File 'lib/generators/aws_record/active_model.rb', line 47

def save
  "#{name}.save"
end

#update(params = nil) ⇒ Object

PATCH/PUT update



52
53
54
# File 'lib/generators/aws_record/active_model.rb', line 52

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