Class: PropInitializer::Property

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/prop_initializer/property.rb

Constant Summary collapse

ORDER =
{ :positional => 0, :* => 1, :keyword => 2, :** => 3, :& => 4 }.freeze
RUBY_KEYWORDS =
%i[alias and begin break case class def do else elsif end ensure false for if in module next nil not or redo rescue retry return self super then true undef unless until when while yield].to_h { |k| [k, "__#{k}__"] }.freeze
VISIBILITY_OPTIONS =
Set[false, :private, :protected, :public].freeze
KIND_OPTIONS =
Set[:positional, :*, :keyword, :**, :&].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, kind:, reader:, writer:, predicate:, default:, coercion:) ⇒ Property



12
13
14
15
16
17
18
19
20
# File 'lib/prop_initializer/property.rb', line 12

def initialize(name:, kind:, reader:, writer:, predicate:, default:, coercion:)
	@name = name
	@kind = kind
	@reader = reader
	@writer = writer
	@predicate = predicate
	@default = default
	@coercion = coercion
end

Instance Attribute Details

#coercionObject (readonly)

Returns the value of attribute coercion.



22
23
24
# File 'lib/prop_initializer/property.rb', line 22

def coercion
  @coercion
end

#defaultObject (readonly)

Returns the value of attribute default.



22
23
24
# File 'lib/prop_initializer/property.rb', line 22

def default
  @default
end

#kindObject (readonly)

Returns the value of attribute kind.



22
23
24
# File 'lib/prop_initializer/property.rb', line 22

def kind
  @kind
end

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/prop_initializer/property.rb', line 22

def name
  @name
end

#predicateObject (readonly)

Returns the value of attribute predicate.



22
23
24
# File 'lib/prop_initializer/property.rb', line 22

def predicate
  @predicate
end

#readerObject (readonly)

Returns the value of attribute reader.



22
23
24
# File 'lib/prop_initializer/property.rb', line 22

def reader
  @reader
end

#writerObject (readonly)

Returns the value of attribute writer.



22
23
24
# File 'lib/prop_initializer/property.rb', line 22

def writer
  @writer
end

Instance Method Details

#<=>(other) ⇒ Object



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

def <=>(other)
	ORDER[@kind] <=> ORDER[other.kind]
end

#block?Boolean



48
49
50
# File 'lib/prop_initializer/property.rb', line 48

def block?
	@kind == :&
end

#coerce(value, context:) ⇒ Object



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

def coerce(value, context:)
	context.instance_exec(value, &@coercion)
end

#default?Boolean



52
53
54
# File 'lib/prop_initializer/property.rb', line 52

def default?
	nil != @default
end

#default_valueObject



89
90
91
92
93
94
# File 'lib/prop_initializer/property.rb', line 89

def default_value
	case @default
		when Proc then @default.call
		else @default
	end
end

#double_splat?Boolean



44
45
46
# File 'lib/prop_initializer/property.rb', line 44

def double_splat?
	@kind == :**
end

#escaped_nameObject



85
86
87
# File 'lib/prop_initializer/property.rb', line 85

def escaped_name
	RUBY_KEYWORDS[@name] || @name.name
end

#generate_initializer_handle_property(buffer = +"")) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/prop_initializer/property.rb', line 127

def generate_initializer_handle_property(buffer = +"")
	buffer << "  # " << @name.name << "\n" <<
		"  property = properties[:" << @name.name << "]\n"

	if @kind == :keyword && ruby_keyword?
		generate_initializer_escape_keyword(buffer)
	end

	if default?
		generate_initializer_assign_default(buffer)
	end

	if @coercion
		generate_initializer_coerce_property(buffer)
	end

	generate_initializer_assign_value(buffer)
end

#generate_predicate_method(buffer = +"")) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/prop_initializer/property.rb', line 115

def generate_predicate_method(buffer = +"")
	buffer <<
		(@predicate ? @predicate.name : "public") <<
		" def " <<
		@name.name <<
		"?\n" <<
		"  !!@" <<
		@name.name <<
		"\n" <<
		"end\n"
end

#generate_reader_method(buffer = +"")) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/prop_initializer/property.rb', line 96

def generate_reader_method(buffer = +"")
	buffer <<
		(@reader ? @reader.name : "public") <<
		"\ndef " <<
		@name.name <<
		"\n  value = @" <<
		@name.name <<
		"\n  value\nend\n"
end

#generate_writer_method(buffer = +"")) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/prop_initializer/property.rb', line 106

def generate_writer_method(buffer = +"")
	buffer <<
		(@writer ? @writer.name : "public") <<
		" def " <<
		@name.name <<
		"=(value)\n" <<
		"  @#{@name.name} = value\nend\n"
end

#keyword?Boolean



32
33
34
# File 'lib/prop_initializer/property.rb', line 32

def keyword?
	@kind == :keyword
end

#optional?Boolean



24
25
26
# File 'lib/prop_initializer/property.rb', line 24

def optional?
	default?
end

#paramObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/prop_initializer/property.rb', line 56

def param
	case @kind
	when :*
		"*#{escaped_name}"
	when :**
		"**#{escaped_name}"
	when :&
		"&#{escaped_name}"
	when :positional
		escaped_name
	when :keyword
		"#{@name.name}:"
	else
		raise "You should never see this error."
	end
end

#positional?Boolean



36
37
38
# File 'lib/prop_initializer/property.rb', line 36

def positional?
	@kind == :positional
end

#required?Boolean



28
29
30
# File 'lib/prop_initializer/property.rb', line 28

def required?
	!optional?
end

#ruby_keyword?Boolean



81
82
83
# File 'lib/prop_initializer/property.rb', line 81

def ruby_keyword?
	!!RUBY_KEYWORDS[@name]
end

#splat?Boolean



40
41
42
# File 'lib/prop_initializer/property.rb', line 40

def splat?
	@kind == :*
end