Class: ObjectState::State
- Inherits:
-
Object
- Object
- ObjectState::State
show all
- Includes:
- ActiveModel::Model, ActiveSupport::Rescuable
- Defined in:
- lib/object_state/state.rb
Defined Under Namespace
Classes: UnsupportedAttributeType
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(model, attrs = {}) ⇒ State
Returns a new instance of State.
69
70
71
72
|
# File 'lib/object_state/state.rb', line 69
def initialize(model, attrs = {})
self.model = model
super attributes_from_model.merge(attrs)
end
|
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
12
13
14
|
# File 'lib/object_state/state.rb', line 12
def model
@model
end
|
Class Method Details
.attribute_names ⇒ Object
16
17
18
|
# File 'lib/object_state/state.rb', line 16
def self.attribute_names
attribute_set.map(&:name)
end
|
.setup_attributes(owner_class, &block) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/object_state/state.rb', line 20
def self.setup_attributes(owner_class, &block)
temp_class = Class.new(Object)
temp_class.define_singleton_method :attr_accessor do |*names|
@attr_accessor_names ||= []
@attr_accessor_names.concat Array(names)
super(*names)
end
temp_class.define_singleton_method :attr_accessor_names do
Array(@attr_accessor_names)
end
is_mongoid_document = !!defined?(::Mongoid::Document) && owner_class.ancestors.include?(::Mongoid::Document)
is_virtus_model = owner_class.respond_to?(:attribute_set)
temp_class.class_eval do
include ::Mongoid::Document if is_mongoid_document
include Virtus.model if is_virtus_model
instance_eval(&block)
end
excluded_attr_accessor_names = i(validation_context _index before_callback_halted)
(temp_class.attr_accessor_names - excluded_attr_accessor_names).each do |name|
next if attribute_set.map(&:name).include?(name.to_sym)
attribute name
end
if is_mongoid_document
temp_class.fields.except(*%w(_id)).each do |name, field|
next if attribute_set.map(&:name).include?(name.to_sym)
attribute name, field.type
end
end
if is_virtus_model
temp_class.attribute_set.each do |att|
next if attribute_set.map(&:name).include?(att.name.to_sym)
attribute att.name, att.type
end
end
owner_class.class_eval(&block)
end
|
Instance Method Details
#cache_key ⇒ Object
91
92
93
|
# File 'lib/object_state/state.rb', line 91
def cache_key
attributes.values.flatten.map(&:to_s).reject(&:blank?).join('/')
end
|
#to_hash(attrs = {}) ⇒ Object
82
83
84
85
86
87
88
89
|
# File 'lib/object_state/state.rb', line 82
def to_hash(attrs = {})
return unless model.present?
key = model.respond_to?(:model_name) ? model.model_name.singular : model.class.to_s.underscore
value = super()
value = value.merge(id: model.id) if model.respond_to?(:id)
value = value.merge(attrs)
{ key => value }
end
|
#update_model!(options = {}) ⇒ Object
74
75
76
77
78
79
80
|
# File 'lib/object_state/state.rb', line 74
def update_model!(options = {})
return unless model.present?
return unless valid? || options[:skip_validations] == true
attributes.except(:id).each do |name, value|
model.send("#{name}=", value) if model.respond_to?(name)
end
end
|