Module: Hyperender::Action

Defined in:
lib/hyperender.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/hyperender.rb', line 90

def self.included(base)
  base.class_eval do
    def hateoas_data *args
      case args.count
      when 0 then @hateoas_data_render ||= {}
      when 1 then @hateoas_data_render = args[0]
      else        @hateoas_data_render = args
      end
      @hateoas_data_render
    end

    def hateoas_error *args
      case args.count
      when 0 then @errors ||= {}
      when 1 then @errors = args[0]
      else        @errors = args
      end
      @errors
    end

    def hateoas_params *args
      case args.count
      when 0 then @params ||= request.query_parameters
      when 1 then @params = args[0]
      else        @params = args
      end
      params_class = @params.class.to_s rescue nil
      unless params_class.in? ["Hash","Array","String"]
        @params = (@params.as_json rescue (@params.to_s rescue "not renderable"))
      end
      @params
    end

    def hateoas_message
      @message ||= HATEOAS_MSG[response.status]
      @message
    end

    def hateoas_status
      @status ||= response.status
      @status
    end

    def hateoas_request
      @request ||= request
      @request
    end

    def hateoas_render
      Hyperender.render (hateoas_data), (hateoas_error), (hateoas_params), (hateoas_message), (hateoas_status), (hateoas_request)
    end

    def hateoas_rendering
      render json: hateoas_render
    end
  end
end