Module: FmRest::Spyke::Model::Attributes
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveModel::Dirty, ActiveModel::ForbiddenAttributesProtection, Orm
- Included in:
- FmRest::Spyke::Model
- Defined in:
- lib/fmrest/spyke/model/attributes.rb
Class Method Summary collapse
-
.attributes(*attrs) ⇒ Object
Similar to Spyke::Base.attributes, but allows defining attribute methods that map to FM attributes with different names.
Instance Method Summary collapse
-
#attributes ⇒ Object
ActiveModel::Dirty since version 5.2 assumes that if there's an we override the instance variable name used by Spyke to avoid issues.
-
#attributes=(new_attributes) ⇒ Object
In addition to the comments above on
attributes, this also adds support for forbidden attributes. - #id=(value) ⇒ Object
- #reload(*args) ⇒ Object
- #save(*args) ⇒ Object
Methods included from Orm
all, create!, #destroy, execute_script, fetch, #save!, #update!, #validate!
Class Method Details
.attributes(*attrs) ⇒ Object
Similar to Spyke::Base.attributes, but allows defining attribute methods that map to FM attributes with different names.
Example:
class Person < Spyke::Base include FmRest::Spyke::Model
attributes first_name: "FstName", last_name: "LstName"
end
p = Person.new p.first_name = "Jojo" p.attributes # => { "FstName" => "Jojo" }
54 55 56 57 58 59 60 |
# File 'lib/fmrest/spyke/model/attributes.rb', line 54 def attributes(*attrs) if attrs.length == 1 && attrs.first.kind_of?(Hash) attrs.first.each { |from, to| _fmrest_define_attribute(from, to) } else attrs.each { |attr| _fmrest_define_attribute(attr, attr) } end end |
Instance Method Details
#attributes ⇒ Object
ActiveModel::Dirty since version 5.2 assumes that if there's an we override the instance variable name used by Spyke to avoid issues.
TODO: Submit a pull request to Spyke so this isn't needed
134 135 136 |
# File 'lib/fmrest/spyke/model/attributes.rb', line 134 def attributes @_spyke_attributes end |
#attributes=(new_attributes) ⇒ Object
In addition to the comments above on attributes, this also adds
support for forbidden attributes
141 142 143 144 |
# File 'lib/fmrest/spyke/model/attributes.rb', line 141 def attributes=(new_attributes) @_spyke_attributes ||= ::Spyke::Attributes.new(scope.params) use_setters(sanitize_for_mass_assignment(new_attributes)) if new_attributes && !new_attributes.empty? end |
#id=(value) ⇒ Object
115 116 117 118 |
# File 'lib/fmrest/spyke/model/attributes.rb', line 115 def id=(value) id_will_change! unless value == id super end |
#reload(*args) ⇒ Object
120 121 122 |
# File 'lib/fmrest/spyke/model/attributes.rb', line 120 def reload(*args) super.tap { |r| clear_changes_information } end |
#save(*args) ⇒ Object
124 125 126 |
# File 'lib/fmrest/spyke/model/attributes.rb', line 124 def save(*args) super.tap { |r| changes_applied_after_save if r } end |