Module: DuckRecord::Core
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#encode_with(coder) ⇒ Object
Populate
coderwith 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
trueif 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 recordwhen pp is required. -
#readonly! ⇒ Object
Marks this record as read only.
-
#readonly? ⇒ Boolean
Returns
trueif 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, ... }}
152 153 154 155 |
# File 'lib/duck_record/core.rb', line 152 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.
160 161 162 163 |
# File 'lib/duck_record/core.rb', line 160 def freeze @attributes = @attributes.clone.freeze self end |
#frozen? ⇒ Boolean
Returns true if the attributes hash has been frozen.
166 167 168 |
# File 'lib/duck_record/core.rb', line 166 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'
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/duck_record/core.rb', line 91 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')
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/duck_record/core.rb', line 61 def initialize(attributes = nil) self.class.define_attribute_methods @attributes = self.class._default_attributes.deep_dup init_internals initialize_internals_callback if attributes assign_attributes(attributes, force_write_readonly: true) clear_changes_information end yield self if block_given? _run_initialize_callbacks end |
#initialize_dup(other) ⇒ Object
:nodoc:
132 133 134 135 136 137 138 |
# File 'lib/duck_record/core.rb', line 132 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.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/duck_record/core.rb', line 182 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.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/duck_record/core.rb', line 200 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.
177 178 179 |
# File 'lib/duck_record/core.rb', line 177 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.
172 173 174 |
# File 'lib/duck_record/core.rb', line 172 def readonly? @readonly end |
#slice(*methods) ⇒ Object
Returns a hash of the given methods with their names as keys and returned values as values.
222 223 224 |
# File 'lib/duck_record/core.rb', line 222 def slice(*methods) Hash[methods.flatten.map! { |method| [method, public_send(method)] }].with_indifferent_access end |