Class: NestedRecord::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Dirty, ActiveModel::Model, Macro
Defined in:
lib/nested_record/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil) ⇒ Base

Returns a new instance of Base.



158
159
160
161
# File 'lib/nested_record/base.rb', line 158

def initialize(attributes = nil)
  super
  self.type = self.class.instance_type if self.class.deep_inherited?
end

Class Method Details

.attributes_builderObject

:nodoc:



10
11
12
13
14
15
# File 'lib/nested_record/base.rb', line 10

def attributes_builder # :nodoc:
  unless defined?(@attributes_builder) && @attributes_builder
    @attributes_builder = ActiveModel::AttributeSet::Builder.new(attribute_types, _default_attributes)
  end
  @attributes_builder
end

.collection_classObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nested_record/base.rb', line 57

def collection_class
  return const_get(collection_class_name, false) if const_defined?(collection_class_name, false)
  record_class = self
  collection_superclass = deep_inherited? ? superclass.collection_class : NestedRecord::Collection
  const_set(
    collection_class_name,
    Class.new(collection_superclass) do
      @record_class = record_class
    end
  )
end

.collection_class_nameObject



53
54
55
# File 'lib/nested_record/base.rb', line 53

def collection_class_name
  :NestedRecord_Collection
end

.collection_methods(&block) ⇒ Object



69
70
71
# File 'lib/nested_record/base.rb', line 69

def collection_methods(&block)
  collection_class.class_eval(&block)
end

.deep_inherited?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/nested_record/base.rb', line 27

def deep_inherited?
  @deep_inherted == true
end

.inherited(klass) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/nested_record/base.rb', line 17

def inherited(klass)
  if self < NestedRecord::Base
    klass.class_eval do
      attribute :type, :string
      @deep_inherted = true
    end
  end
  super
end

.inherited_types(options) ⇒ Object

Raises:

  • (ArgumentError)


73
74
75
76
77
78
79
# File 'lib/nested_record/base.rb', line 73

def inherited_types(options)
  raise ArgumentError, '.inherited_types is supported only for base classes' if deep_inherited?
  if options[:full] && options[:namespace]
    raise ArgumentError, ':full and :namespace options cannot be used at the same time'
  end
  @inherited_types_options = options
end

.instance_typeObject



81
82
83
84
85
86
87
88
# File 'lib/nested_record/base.rb', line 81

def instance_type
  @instance_type ||=
    if inherited_types_underscored?
      type_const.underscore
    else
      type_const.dup
    end
end

.instantiate(attributes) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/nested_record/base.rb', line 45

def instantiate(attributes)
  klass = find_instance_class(attributes['type'])
  attributes = klass.attributes_builder.build_from_database(attributes)
  klass.allocate.tap do |instance|
    instance.instance_variable_set(:@attributes, attributes)
  end
end

.new(attributes = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/nested_record/base.rb', line 31

def new(attributes = nil)
  if attributes
    attributes = attributes.stringify_keys
    klass = find_instance_class(attributes['type'])
  else
    klass = self
  end
  if self == klass
    super(attributes)
  else
    klass.new(attributes)
  end
end

Instance Method Details

#==(other) ⇒ Object



163
164
165
# File 'lib/nested_record/base.rb', line 163

def ==(other)
  attributes == other.attributes
end

#as_jsonObject



167
168
169
# File 'lib/nested_record/base.rb', line 167

def as_json
  attributes.transform_values(&:as_json)
end

#inspectObject



171
172
173
174
# File 'lib/nested_record/base.rb', line 171

def inspect
  as = attributes.except('type').map { |k,v| "#{k}: #{v.inspect}" }
  "#<#{self.class.name} #{as.join(', ')}>"
end

#match?(attrs) ⇒ Boolean

Returns:

  • (Boolean)


180
181
182
183
184
185
186
187
188
189
# File 'lib/nested_record/base.rb', line 180

def match?(attrs)
  attrs.all? do |attr, others|
    ours = read_attribute(attr)
    if others.is_a? Array
      others.include? ours
    else
      others == ours
    end
  end
end

#read_attribute(attr) ⇒ Object



176
177
178
# File 'lib/nested_record/base.rb', line 176

def read_attribute(attr)
  @attributes.fetch_value(attr.to_s)
end