Class: Lycra::Attributes::Attribute
- Inherits:
-
Object
- Object
- Lycra::Attributes::Attribute
- Defined in:
- lib/lycra/attributes/attribute.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#resolved ⇒ Object
readonly
Returns the value of attribute resolved.
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
- #description(description = nil) ⇒ Object
-
#initialize(name = nil, type = nil, *args, **opts, &block) ⇒ Attribute
constructor
A new instance of Attribute.
- #mappings(mappings = nil) ⇒ Object (also: #mapping)
- #name(name = nil) ⇒ Object
- #nested? ⇒ Boolean
- #reload ⇒ Object
- #required! ⇒ Object
- #required? ⇒ Boolean
- #resolve!(document, *args, **ctxt) ⇒ Object
- #resolved? ⇒ Boolean
- #resolver ⇒ Object
- #type(type = nil) ⇒ Object
Constructor Details
#initialize(name = nil, type = nil, *args, **opts, &block) ⇒ Attribute
Returns a new instance of Attribute.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/lycra/attributes/attribute.rb', line 6 def initialize(name=nil, type=nil, *args, **opts, &block) @name = name @name ||= opts[:name] @nested_type = type.is_a?(Array) @type = [type].flatten.compact.first @type ||= [opts[:type]].flatten.compact.first if @type.ancestors.include?(Lycra::Attributes) @type = Lycra::Types.custom(@type) end @klass = opts[:klass] @mappings = opts[:mappings] || opts[:mapping] @resolver = args.find { |arg| arg.is_a?(Proc) || arg.is_a?(Symbol) } @resolver = opts[:resolve] if opts.key?(:resolve) @resolver = opts[:resolver] if opts.key?(:resolver) @description = args.find { |arg| arg.is_a?(String) } @description = opts[:description] if opts.key?(:description) @required = opts[:required] || false @cache = opts[:cache] || false instance_exec &block if block_given? end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
4 5 6 |
# File 'lib/lycra/attributes/attribute.rb', line 4 def klass @klass end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
4 5 6 |
# File 'lib/lycra/attributes/attribute.rb', line 4 def required @required end |
#resolved ⇒ Object (readonly)
Returns the value of attribute resolved.
4 5 6 |
# File 'lib/lycra/attributes/attribute.rb', line 4 def resolved @resolved end |
Instance Method Details
#as_json(options = {}) ⇒ Object
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/lycra/attributes/attribute.rb', line 119 def as_json(={}) { name: name, type: type.type, required: required, description: description, mappings: mappings, resolver: resolver.is_a?(Symbol) ? resolver : resolver.to_s } end |
#description(description = nil) ⇒ Object
66 67 68 69 |
# File 'lib/lycra/attributes/attribute.rb', line 66 def description(description=nil) @description = description if description @description end |
#mappings(mappings = nil) ⇒ Object Also known as: mapping
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/lycra/attributes/attribute.rb', line 54 def mappings(mappings=nil) @mappings = mappings if mappings map_type = type.type if map_type.is_a?(Class) && map_type.ancestors.include?(Lycra::Attributes) map_type = @nested_type ? "nested" : "object" end {type: map_type}.merge(@mappings || {}) end |
#name(name = nil) ⇒ Object
34 35 36 37 |
# File 'lib/lycra/attributes/attribute.rb', line 34 def name(name=nil) @name = name if name @name end |
#nested? ⇒ Boolean
50 51 52 |
# File 'lib/lycra/attributes/attribute.rb', line 50 def nested? !!@nested_type end |
#reload ⇒ Object
114 115 116 117 |
# File 'lib/lycra/attributes/attribute.rb', line 114 def reload remove_instance_variable :@resolved self end |
#required! ⇒ Object
75 76 77 |
# File 'lib/lycra/attributes/attribute.rb', line 75 def required! @required = true end |
#required? ⇒ Boolean
79 80 81 |
# File 'lib/lycra/attributes/attribute.rb', line 79 def required? !!@required end |
#resolve!(document, *args, **ctxt) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/lycra/attributes/attribute.rb', line 83 def resolve!(document, *args, **ctxt) @resolved ||= begin # TODO wrap this whole block in cache if caching is enabled if resolver.is_a?(Proc) result = resolver.call(document.subject, args, ctxt) elsif resolver.is_a?(Symbol) if document.methods.include?(resolver) result = document.send(resolver) else result = document.subject.send(resolver) end end rslvd = type.new(result) unless rslvd.valid?(required?, nested?) rslvd_type = rslvd.type rslvd_type = "array[#{rslvd.type}]" if nested? raise Lycra::AttributeError, "Invalid value #{rslvd.value} (#{rslvd.value.class.name}) " + "for type '#{rslvd_type}' in field #{name} on #{document}" end rslvd.transform end end |
#resolved? ⇒ Boolean
110 111 112 |
# File 'lib/lycra/attributes/attribute.rb', line 110 def resolved? instance_variable_defined? :@resolved end |
#resolver ⇒ Object
71 72 73 |
# File 'lib/lycra/attributes/attribute.rb', line 71 def resolver @resolver ||= name.to_sym end |
#type(type = nil) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/lycra/attributes/attribute.rb', line 39 def type(type=nil) if type @nested_type = type.is_a?(Array) @type = [type].flatten.compact.first if @type.ancestors.include?(Lycra::Attributes) @type = Lycra::Types.custom(@type) end end @type end |