Module: Her::Model::Parse::ClassMethods

Defined in:
lib/her/model/parse.rb

Instance Method Summary collapse

Instance Method Details

#include_root_in_json(value, options = {}) ⇒ Object

Return or change the value of `include_root_in_json`

Examples:

class User
  include Her::Model
  include_root_in_json true
end


81
82
83
84
# File 'lib/her/model/parse.rb', line 81

def include_root_in_json(value, options = {})
  @_her_include_root_in_json = value
  @_her_include_root_in_json_format = options[:format]
end

#parse_root_in_json(value, options = {}) ⇒ Object

Return or change the value of `parse_root_in_json`

Examples:

class User
  include Her::Model
  parse_root_in_json true
end

class User
  include Her::Model
  parse_root_in_json true, format: :active_model_serializers
end

class User
  include Her::Model
  parse_root_in_json true, format: :json_api
end


103
104
105
106
# File 'lib/her/model/parse.rb', line 103

def parse_root_in_json(value, options = {})
  @_her_parse_root_in_json = value
  @_her_parse_root_in_json_format = options[:format]
end

#request_new_object_on_build(value = nil) ⇒ Object

Return or change the value of `request_new_object_on_build`

Examples:

class User
  include Her::Model
  request_new_object_on_build true
end


115
116
117
# File 'lib/her/model/parse.rb', line 115

def request_new_object_on_build(value = nil)
  @_her_request_new_object_on_build = value
end

#root_element(value = nil) ⇒ Object

Return or change the value of `root_element`. Always defaults to the base name of the class.

Examples:

class User
  include Her::Model
  parse_root_in_json true
  root_element :huh
end

user = User.find(1) # { :huh => { :id => 1, :name => "Tobias" } }
user.name # => "Tobias"


130
131
132
133
134
135
136
137
138
139
140
# File 'lib/her/model/parse.rb', line 130

def root_element(value = nil)
  if value.nil?
    @_her_root_element ||= if json_api_format?
                             name.split("::").last.pluralize.underscore.to_sym
                           else
                             name.split("::").last.underscore.to_sym
                           end
  else
    @_her_root_element = value.to_sym
  end
end