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, ... }}
148 149 150 151 |
# File 'lib/duck_record/core.rb', line 148 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.
156 157 158 159 |
# File 'lib/duck_record/core.rb', line 156 def freeze @attributes = @attributes.clone.freeze self end |
#frozen? ⇒ Boolean
Returns true if the attributes hash has been frozen.
162 163 164 |
# File 'lib/duck_record/core.rb', line 162 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'
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/duck_record/core.rb', line 85 def init_with(coder) coder = LegacyYamlAdapter.convert(self.class, coder) @attributes = self.class.yaml_encoder.decode(coder) init_internals self.class.define_attribute_methods yield self if block_given? _run_find_callbacks _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')
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/duck_record/core.rb', line 58 def initialize(attributes = nil) self.class.define_attribute_methods @attributes = self.class._default_attributes.deep_dup init_internals initialize_internals_callback assign_attributes(attributes) if attributes yield self if block_given? _run_initialize_callbacks end |
#initialize_dup(other) ⇒ Object
:nodoc:
128 129 130 131 132 133 134 |
# File 'lib/duck_record/core.rb', line 128 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.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/duck_record/core.rb', line 178 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.
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/duck_record/core.rb', line 196 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.
173 174 175 |
# File 'lib/duck_record/core.rb', line 173 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.
168 169 170 |
# File 'lib/duck_record/core.rb', line 168 def readonly? @readonly end |
#slice(*methods) ⇒ Object
Returns a hash of the given methods with their names as keys and returned values as values.
218 219 220 |
# File 'lib/duck_record/core.rb', line 218 def slice(*methods) Hash[methods.flatten.map! { |method| [method, public_send(method)] }].with_indifferent_access end |