Class: SonJay::ObjectModel::Content::Abstract

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/son_jay/object_model/content/abstract.rb

Direct Known Subclasses

ContentWithExtra, ContentWithoutExtra

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(property_definitions) ⇒ Abstract

Returns a new instance of Abstract.



13
14
15
16
17
18
19
20
# File 'lib/son_jay/object_model/content/abstract.rb', line 13

def initialize(property_definitions)
  @property_definitions = property_definitions
  @data = ObjectModel::ContentData.new
  @model_properties = Set.new

  init_properties \
    *property_definitions.partition { |md| !! md.model_class }
end

Instance Attribute Details

#model_propertiesObject (readonly)

Returns the value of attribute model_properties.



11
12
13
# File 'lib/son_jay/object_model/content/abstract.rb', line 11

def model_properties
  @model_properties
end

Instance Method Details

#[](name) ⇒ Object



27
28
29
30
# File 'lib/son_jay/object_model/content/abstract.rb', line 27

def [](name)
  name = property_definitions.name_from(name)
  @data[name]
end

#[]=(name, value) ⇒ Object

Raises:



39
40
41
42
43
# File 'lib/son_jay/object_model/content/abstract.rb', line 39

def []=(name, value)
  name = property_definitions.name_from(name)
  raise PropertyNameError.new(name) unless @data.has_key?(name)
  @data[name] = value
end

#cloneObject



86
87
88
89
90
91
92
93
# File 'lib/son_jay/object_model/content/abstract.rb', line 86

def clone
  new_copy = super
  unless new_copy.frozen?
    new_copy.instance_variable_set :@data,             @data.clone
    new_copy.instance_variable_set :@model_properties, @model_properties.clone
  end
  new_copy
end

#dupObject



79
80
81
82
83
84
# File 'lib/son_jay/object_model/content/abstract.rb', line 79

def dup
  new_copy = super
  new_copy.instance_variable_set :@data,             @data.dup
  new_copy.instance_variable_set :@model_properties, @model_properties.dup
  new_copy
end

#eachObject

Raises:

  • (NotImplementedError)


69
70
71
# File 'lib/son_jay/object_model/content/abstract.rb', line 69

def each
  raise NotImplementedError, "Subclass responsibility"
end

#extraObject

Raises:

  • (NotImplementedError)


60
61
62
# File 'lib/son_jay/object_model/content/abstract.rb', line 60

def extra
  raise NotImplementedError, "Subclass responsibility"
end

#fetch(name) ⇒ Object



32
33
34
35
36
37
# File 'lib/son_jay/object_model/content/abstract.rb', line 32

def fetch(name)
  name = property_definitions.name_from(name)
  @data.fetch(name)
rescue KeyError
  raise PropertyNameError.new(name)
end

#freezeObject



73
74
75
76
77
# File 'lib/son_jay/object_model/content/abstract.rb', line 73

def freeze
  super
  @data.freeze
  self
end

#load_data(data) ⇒ Object



45
46
47
48
49
# File 'lib/son_jay/object_model/content/abstract.rb', line 45

def load_data(data)
  data.each_pair do |name, value|
    load_property name, value
  end
end

#load_property(name, value) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/son_jay/object_model/content/abstract.rb', line 51

def load_property(name, value)
  name = property_definitions.name_from(name)
  if @data.has_key?( name )
    load_defined_property name, value
  else
    load_extra_property name, value
  end
end

#to_json(options = ::JSON::State.new) ⇒ Object



64
65
66
67
# File 'lib/son_jay/object_model/content/abstract.rb', line 64

def to_json(options = ::JSON::State.new)
  options = ::JSON::State.new(options) unless options.kind_of?(::JSON::State)
  hash_for_json.to_json( options )
end