Class: Holotype

Inherits:
Object
  • Object
show all
Defined in:
lib/holotype.rb,
lib/holotype/version.rb,
lib/holotype/attribute.rb,
lib/holotype/value_normalizer.rb,
lib/holotype/attribute/definition.rb,
lib/holotype/collection_normalizer.rb,
lib/holotype/attribute/read_only_error.rb,
lib/holotype/inheritance_disallowed_error.rb,
lib/holotype/attribute/immutable_value_error.rb,
lib/holotype/attributes_already_defined_error.rb,
lib/holotype/missing_required_attributes_error.rb,
lib/holotype/attribute/frozen_modification_error.rb,
lib/holotype/attribute/definition/no_value_class_error.rb,
lib/holotype/attribute/definition/default_conflict_error.rb,
lib/holotype/attribute/definition/required_conflict_error.rb,
lib/holotype/attribute/definition/no_collection_class_error.rb,
lib/holotype/collection_normalizer/expected_hash_like_collection_error.rb,
lib/holotype/collection_normalizer/expected_array_like_collection_error.rb

Defined Under Namespace

Classes: Attribute, AttributesAlreadyDefinedError, CollectionNormalizer, InheritanceDisallowedError, MissingRequiredAttributesError, ValueNormalizer

Constant Summary collapse

VERSION =
'0.15.1'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ Holotype

Returns a new instance of Holotype.



91
92
93
94
# File 'lib/holotype.rb', line 91

def initialize **attributes
  __holotype_check_for_missing_required attributes
  __holotype_store attributes
end

Instance Attribute Details

#attributesObject (readonly)

Instance Definition



89
90
91
# File 'lib/holotype.rb', line 89

def attributes
  @attributes
end

Class Method Details

.attribute(name, **options, &default) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/holotype.rb', line 19

def attribute name, **options, &default
  # symbolize name
  name = name.to_sym

  # prepare options
  processed_options = if immutable?
                        [
                          __default_attribute_options,
                          options,
                          IMMUTABLE_OPTION,
                        ].reduce :merge
                      else
                        [
                          __default_attribute_options,
                          options,
                        ].reduce :merge
                      end

  # create attribute definition
  attribute = Attribute::Definition.new name,
                                        **processed_options,
                                        &default

  # store the attribute definition
  attributes[name] = attribute

  # create an attribute reader
  define_method name do
    self.attributes[name].value
  end

  # create an attribute writer
  define_method "#{name}=" do |value|
    self.attributes[name].value = value
  end
end

.attributesObject



56
57
58
# File 'lib/holotype.rb', line 56

def attributes
  @attributes ||= Hash[]
end

.default_attribute_options(**options) ⇒ Object



74
75
76
# File 'lib/holotype.rb', line 74

def default_attribute_options **options
  @default_attribute_options = options.freeze
end

.immutable?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/holotype.rb', line 70

def immutable?
  !!@immutable
end

.make_immutableObject



60
61
62
63
64
65
66
67
68
# File 'lib/holotype.rb', line 60

def make_immutable
  raise AttributesAlreadyDefinedError.new if attributes.count != 0

  define_singleton_method :inherited do |_|
    raise InheritanceDisallowedError.new
  end

  @immutable = true
end

Instance Method Details

#==(other) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/holotype.rb', line 113

def == other
  return false unless self.class == other.class

  attributes.all? do |name, attribute|
    attribute.value == other.attributes[name].value
  end
end

#frozen?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/holotype.rb', line 96

def frozen?
  true
end

#inspectObject



125
126
127
128
129
130
131
# File 'lib/holotype.rb', line 125

def inspect
  data = to_hash
           .map { |attribute, value| "#{attribute}: #{value.inspect}" }
           .join(', ')

  "#{self.class.name}(#{data})"
end

#to_hashObject



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/holotype.rb', line 100

def to_hash
  Hash[
    attributes
      .map do |key, attribute|
        definition = attribute.definition

        value = __holotype_hashify attribute.value

        [key, value]
      end
  ]
end

#with(**attributes) ⇒ Object



121
122
123
# File 'lib/holotype.rb', line 121

def with **attributes
  self.class.new to_hash.merge attributes
end