Class: Glossarist::Model
- Inherits:
-
Object
show all
- Defined in:
- lib/glossarist/model.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ Model
Returns a new instance of Model.
14
15
16
|
# File 'lib/glossarist/model.rb', line 14
def initialize(attributes = {})
attributes.each_pair { |k, v| set_attribute(k, v) }
end
|
Class Method Details
.from_h(hash) ⇒ Object
32
33
34
|
# File 'lib/glossarist/model.rb', line 32
def self.from_h(hash)
new(hash)
end
|
.new(params = {}) ⇒ Object
8
9
10
11
12
|
# File 'lib/glossarist/model.rb', line 8
def self.new(params = {})
return params if params.is_a?(self)
super
end
|
Instance Method Details
#set_attribute(name, value) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/glossarist/model.rb', line 18
def set_attribute(name, value)
public_send("#{name}=", value)
rescue NoMethodError
if Config.extension_attributes.include?(name)
extension_attributes[name] = value
elsif name.match?(/[A-Z]/) name = snake_case(name.to_s).to_sym
retry
else
raise ArgumentError, "#{self.class.name} does not have " +
"attribute #{name} defined or the attribute is read only."
end
end
|
#snake_case(str) ⇒ Object
36
37
38
|
# File 'lib/glossarist/model.rb', line 36
def snake_case(str)
str.gsub(/([A-Z])/) { "_#{$1.downcase}" }
end
|