Class: Holotype::Attribute::Definition
- Inherits:
-
Object
- Object
- Holotype::Attribute::Definition
show all
- Defined in:
- lib/holotype/attribute/definition.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
Defined Under Namespace
Classes: DefaultConflictError, NoCollectionClassError, NoValueClassError, RequiredConflictError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, **options, &default_block) ⇒ Definition
Returns a new instance of Definition.
15
16
17
18
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/attribute/definition.rb', line 15
def initialize name, **options, &default_block
@collection = options.fetch :collection, false
@immutable = options.fetch :immutable, false
@name = name
@read_only = options.fetch :read_only, false
@required = options.fetch :required, false
if options.key? :collection_class
@collection = true
@has_collection_class = true
@collection_class = options[:collection_class]
else
if collection?
@has_collection_class = true
@collection_class = Array
else
@has_collection_class = false
end
end
if options.key? :value_class
@has_value_class = true
@value_class = options[:value_class]
else
@has_value_class = false
end
if default_block
raise DefaultConflictError.new if options.key? :default
raise RequiredConflictError.new if @required
@default = default_block
@default_type = :dynamic
elsif options.key? :default
raise RequiredConflictError.new if @required
@default = options[:default]
@default_type = :constant
end
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
13
14
15
|
# File 'lib/holotype/attribute/definition.rb', line 13
def name
@name
end
|
Instance Method Details
#collection? ⇒ Boolean
85
86
87
|
# File 'lib/holotype/attribute/definition.rb', line 85
def collection?
!!@collection
end
|
#collection_class ⇒ Object
93
94
95
96
|
# File 'lib/holotype/attribute/definition.rb', line 93
def collection_class
return @collection_class if has_collection_class?
raise NoCollectionClassError.new self
end
|
#default(receiver) ⇒ Object
56
57
58
59
60
61
62
|
# File 'lib/holotype/attribute/definition.rb', line 56
def default receiver
case @default_type
when :constant then @default
when :dynamic then receiver.instance_exec(&@default).freeze
else nil
end
end
|
#has_collection_class? ⇒ Boolean
89
90
91
|
# File 'lib/holotype/attribute/definition.rb', line 89
def has_collection_class?
@has_collection_class
end
|
#has_value_class? ⇒ Boolean
76
77
78
|
# File 'lib/holotype/attribute/definition.rb', line 76
def has_value_class?
@has_value_class
end
|
#immutable? ⇒ Boolean
102
103
104
|
# File 'lib/holotype/attribute/definition.rb', line 102
def immutable?
!!@immutable
end
|
#normalize(value) ⇒ Object
64
65
66
67
68
69
70
|
# File 'lib/holotype/attribute/definition.rb', line 64
def normalize value
if collection?
normalize_collection value
else
normalize_single value
end
end
|
#read_only? ⇒ Boolean
98
99
100
|
# File 'lib/holotype/attribute/definition.rb', line 98
def read_only?
!!@read_only
end
|
#required? ⇒ Boolean
72
73
74
|
# File 'lib/holotype/attribute/definition.rb', line 72
def required?
!!@required
end
|
#value_class ⇒ Object
80
81
82
83
|
# File 'lib/holotype/attribute/definition.rb', line 80
def value_class
raise NoValueClassError.new self unless has_value_class?
@value_class
end
|