Module: DuckRecord::Core
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#encode_with(coder) ⇒ Object
Populate
coder
with attributes about this record that should be serialized. -
#freeze ⇒ Object
Clone and freeze the attributes hash such that associations are still accessible, even on destroyed records, but cloned models will not be frozen.
-
#frozen? ⇒ Boolean
Returns
true
if the attributes hash has been frozen. -
#init_with(coder) {|_self| ... } ⇒ Object
Initialize an empty model object from
coder
. -
#initialize(attributes = nil) {|_self| ... } ⇒ Object
New objects can be instantiated as either empty (pass no construction parameter) or pre-set with attributes but not yet saved (pass a hash with key names matching the associated table column names).
-
#initialize_dup(other) ⇒ Object
:nodoc:.
-
#inspect ⇒ Object
Returns the contents of the record as a nicely formatted string.
-
#pretty_print(pp) ⇒ Object
Takes a PP and prettily prints this record to it, allowing you to get a nice result from
pp record
when pp is required. -
#readonly! ⇒ Object
Marks this record as read only.
-
#readonly? ⇒ Boolean
Returns
true
if the record is read only. -
#slice(*methods) ⇒ Object
Returns a hash of the given methods with their names as keys and returned values as values.
Instance Method Details
#encode_with(coder) ⇒ Object
Populate coder
with attributes about this record that should be serialized. The structure of coder
defined in this method is guaranteed to match the structure of coder
passed to the #init_with method.
Example:
class Post < DuckRecord::Base
end
coder = {}
Post.new.encode_with(coder)
coder # => {"attributes" => {"id" => nil, ... }}
157 158 159 160 |
# File 'lib/duck_record/core.rb', line 157 def encode_with(coder) self.class.yaml_encoder.encode(@attributes, coder) coder["duck_record_yaml_version"] = 2 end |
#freeze ⇒ Object
Clone and freeze the attributes hash such that associations are still accessible, even on destroyed records, but cloned models will not be frozen.
165 166 167 168 |
# File 'lib/duck_record/core.rb', line 165 def freeze @attributes = @attributes.clone.freeze self end |
#frozen? ⇒ Boolean
Returns true
if the attributes hash has been frozen.
171 172 173 |
# File 'lib/duck_record/core.rb', line 171 def frozen? @attributes.frozen? end |
#init_with(coder) {|_self| ... } ⇒ Object
Initialize an empty model object from coder
. coder
should be the result of previously encoding an Active Record model, using #encode_with.
class Post < DuckRecord::Base
end
old_post = Post.new(title: "hello world")
coder = {}
old_post.encode_with(coder)
post = Post.allocate
post.init_with(coder)
post.title # => 'hello world'
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/duck_record/core.rb', line 96 def init_with(coder) @attributes = self.class.yaml_encoder.decode(coder) init_internals self.class.define_attribute_methods yield self if block_given? _run_initialize_callbacks self end |
#initialize(attributes = nil) {|_self| ... } ⇒ Object
New objects can be instantiated as either empty (pass no construction parameter) or pre-set with attributes but not yet saved (pass a hash with key names matching the associated table column names). In both instances, valid attribute keys are determined by the column names of the associated table – hence you can’t have attributes that aren’t part of the table columns.
Example:
# Instantiates a single new object
User.new(first_name: 'Jamie')
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/duck_record/core.rb', line 62 def initialize(attributes = nil) self.class.define_attribute_methods @attributes = self.class._default_attributes.deep_dup disable_attr_readonly! init_internals initialize_internals_callback if attributes assign_attributes(attributes) clear_changes_information end yield self if block_given? _run_initialize_callbacks enable_attr_readonly! end |
#initialize_dup(other) ⇒ Object
:nodoc:
137 138 139 140 141 142 143 |
# File 'lib/duck_record/core.rb', line 137 def initialize_dup(other) # :nodoc: @attributes = @attributes.deep_dup _run_initialize_callbacks super end |
#inspect ⇒ Object
Returns the contents of the record as a nicely formatted string.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/duck_record/core.rb', line 187 def inspect # We check defined?(@attributes) not to issue warnings if the object is # allocated but not initialized. inspection = if defined?(@attributes) && @attributes self.class.attribute_names.collect do |name| if has_attribute?(name) "#{name}: #{attribute_for_inspect(name)}" end end.compact.join(", ") else "not initialized" end "#<#{self.class} #{inspection}>" end |
#pretty_print(pp) ⇒ Object
Takes a PP and prettily prints this record to it, allowing you to get a nice result from pp record
when pp is required.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/duck_record/core.rb', line 205 def pretty_print(pp) return super if custom_inspect_method_defined? pp.object_address_group(self) do if defined?(@attributes) && @attributes pp.seplist(self.class.attribute_names, proc { pp.text "," }) do |attribute_name| attribute_value = read_attribute(attribute_name) pp.breakable " " pp.group(1) do pp.text attribute_name pp.text ":" pp.breakable pp.pp attribute_value end end else pp.breakable " " pp.text "not initialized" end end end |
#readonly! ⇒ Object
Marks this record as read only.
182 183 184 |
# File 'lib/duck_record/core.rb', line 182 def readonly! @readonly = true end |
#readonly? ⇒ Boolean
Returns true
if the record is read only. Records loaded through joins with piggy-back attributes will be marked as read only since they cannot be saved.
177 178 179 |
# File 'lib/duck_record/core.rb', line 177 def readonly? @readonly end |
#slice(*methods) ⇒ Object
Returns a hash of the given methods with their names as keys and returned values as values.
227 228 229 |
# File 'lib/duck_record/core.rb', line 227 def slice(*methods) Hash[methods.flatten.map! { |method| [method, public_send(method)] }].with_indifferent_access end |