Class: Literal::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/literal/attribute.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, type:, special:, reader:, writer:, positional:, default:, coercion:) ⇒ Attribute

Returns a new instance of Attribute.



6
7
8
9
10
11
12
13
14
15
# File 'lib/literal/attribute.rb', line 6

def initialize(name:, type:, special:, reader:, writer:, positional:, default:, coercion:)
	@name = name
	@type = type
	@special = special
	@reader = reader
	@writer = writer
	@positional = positional
	@default = default
	@coercion = coercion
end

Instance Attribute Details

#coercionObject (readonly)

Returns the value of attribute coercion.



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

def coercion
  @coercion
end

#defaultObject (readonly)

Returns the value of attribute default.



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

def default
  @default
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#positionalObject (readonly)

Returns the value of attribute positional.



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

def positional
  @positional
end

#readerObject (readonly)

Returns the value of attribute reader.



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

def reader
  @reader
end

#specialObject (readonly)

Returns the value of attribute special.



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

def special
  @special
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

#writerObject (readonly)

Returns the value of attribute writer.



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

def writer
  @writer
end

Instance Method Details

#coerce(value, context:) ⇒ Object



39
40
41
# File 'lib/literal/attribute.rb', line 39

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

#coercion?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/literal/attribute.rb', line 35

def coercion?
	!!@coercion
end

#default?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/literal/attribute.rb', line 27

def default?
	nil != @default
end

#default_valueObject



51
52
53
54
55
56
# File 'lib/literal/attribute.rb', line 51

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

#escape?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/literal/attribute.rb', line 43

def escape?
	!!RUBY_KEYWORDS[@name]
end

#escapedObject



47
48
49
# File 'lib/literal/attribute.rb', line 47

def escaped
	RUBY_KEYWORDS[@name] || @name
end

#positional?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/literal/attribute.rb', line 31

def positional?
	!!@positional
end

#reader?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/literal/attribute.rb', line 19

def reader?
	!!@reader
end

#writer?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/literal/attribute.rb', line 23

def writer?
	!!@writer
end