Class: Literal::Attribute
- Inherits:
-
Object
- Object
- Literal::Attribute
- 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
-
#coercion ⇒ Object
readonly
Returns the value of attribute coercion.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#positional ⇒ Object
readonly
Returns the value of attribute positional.
-
#reader ⇒ Object
readonly
Returns the value of attribute reader.
-
#special ⇒ Object
readonly
Returns the value of attribute special.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#writer ⇒ Object
readonly
Returns the value of attribute writer.
Instance Method Summary collapse
- #coerce(value, context:) ⇒ Object
- #coercion? ⇒ Boolean
- #default? ⇒ Boolean
- #default_value ⇒ Object
- #escape? ⇒ Boolean
- #escaped ⇒ Object
-
#initialize(name:, type:, special:, reader:, writer:, positional:, default:, coercion:) ⇒ Attribute
constructor
A new instance of Attribute.
- #positional? ⇒ Boolean
- #reader? ⇒ Boolean
- #writer? ⇒ Boolean
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
#coercion ⇒ Object (readonly)
Returns the value of attribute coercion.
17 18 19 |
# File 'lib/literal/attribute.rb', line 17 def coercion @coercion end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
17 18 19 |
# File 'lib/literal/attribute.rb', line 17 def default @default end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
17 18 19 |
# File 'lib/literal/attribute.rb', line 17 def name @name end |
#positional ⇒ Object (readonly)
Returns the value of attribute positional.
17 18 19 |
# File 'lib/literal/attribute.rb', line 17 def positional @positional end |
#reader ⇒ Object (readonly)
Returns the value of attribute reader.
17 18 19 |
# File 'lib/literal/attribute.rb', line 17 def reader @reader end |
#special ⇒ Object (readonly)
Returns the value of attribute special.
17 18 19 |
# File 'lib/literal/attribute.rb', line 17 def special @special end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
17 18 19 |
# File 'lib/literal/attribute.rb', line 17 def type @type end |
#writer ⇒ Object (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
35 36 37 |
# File 'lib/literal/attribute.rb', line 35 def coercion? !!@coercion end |
#default? ⇒ Boolean
27 28 29 |
# File 'lib/literal/attribute.rb', line 27 def default? nil != @default end |
#default_value ⇒ Object
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
43 44 45 |
# File 'lib/literal/attribute.rb', line 43 def escape? !!RUBY_KEYWORDS[@name] end |
#escaped ⇒ Object
47 48 49 |
# File 'lib/literal/attribute.rb', line 47 def escaped RUBY_KEYWORDS[@name] || @name end |
#positional? ⇒ Boolean
31 32 33 |
# File 'lib/literal/attribute.rb', line 31 def positional? !!@positional end |
#reader? ⇒ Boolean
19 20 21 |
# File 'lib/literal/attribute.rb', line 19 def reader? !!@reader end |
#writer? ⇒ Boolean
23 24 25 |
# File 'lib/literal/attribute.rb', line 23 def writer? !!@writer end |