Module: Holotype

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

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

Constant Summary collapse

VERSION =
"1.0.0".freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



17
18
19
# File 'lib/holotype.rb', line 17

def extended base
  base.send :include, InstanceMethods
end

Instance Method Details

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



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
55
56
57
# File 'lib/holotype.rb', line 22

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



59
60
61
# File 'lib/holotype.rb', line 59

def attributes
  @attributes ||= Hash[]
end

#default_attribute_options(**options) ⇒ Object



77
78
79
# File 'lib/holotype.rb', line 77

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

#immutable?Boolean

Returns:

  • (Boolean)


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

def immutable?
  !!@immutable
end

#make_immutableObject



63
64
65
66
67
68
69
70
71
# File 'lib/holotype.rb', line 63

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

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

  @immutable = true
end