Class: Taksi::Components::Field
- Inherits:
-
Object
- Object
- Taksi::Components::Field
- Defined in:
- lib/taksi/components/field.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#skeleton ⇒ Object
readonly
Returns the value of attribute skeleton.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#as_json ⇒ Object
Turns the field into his json representation The returned hash is compatible with the skeleton json specification.
- #dynamic(name) ⇒ Object
- #dynamic? ⇒ Boolean
-
#fetch_from(data) ⇒ Object
Fetches the data for in ‘data` for the current field.
- #field(name, *args, &block) ⇒ Object
-
#fields ⇒ Object
Builds up a interator over all fields included nested ones.
-
#initialize(skeleton, name, *args, parent: nil, &block) ⇒ Field
constructor
A new instance of Field.
- #key ⇒ Object
- #nested(name, &block) ⇒ Object
- #nested? ⇒ Boolean
- #root? ⇒ Boolean
- #static(name, value) ⇒ Object
Constructor Details
#initialize(skeleton, name, *args, parent: nil, &block) ⇒ Field
Returns a new instance of Field.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/taksi/components/field.rb', line 8 def initialize(skeleton, name, *args, parent: nil, &block) @skeleton = skeleton @name = name.to_sym @parent = parent raise <<~MESSAGE unless args.size.positive? || block_given? You must provide a value or a block definition to build field MESSAGE @value = args.shift.new(skeleton, name, *args) if args.size.positive? @nested_fields = [] instance_exec(&block) if block_given? end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/taksi/components/field.rb', line 6 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
6 7 8 |
# File 'lib/taksi/components/field.rb', line 6 def parent @parent end |
#skeleton ⇒ Object (readonly)
Returns the value of attribute skeleton.
6 7 8 |
# File 'lib/taksi/components/field.rb', line 6 def skeleton @skeleton end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
6 7 8 |
# File 'lib/taksi/components/field.rb', line 6 def value @value end |
Instance Method Details
#as_json ⇒ Object
Turns the field into his json representation The returned hash is compatible with the skeleton json specification
44 45 46 47 48 |
# File 'lib/taksi/components/field.rb', line 44 def as_json return {name => @nested_fields.map(&:as_json).inject({}, &:merge)} if nested? {name => value.as_json} end |
#dynamic(name) ⇒ Object
92 93 94 |
# File 'lib/taksi/components/field.rb', line 92 def dynamic(name) field(name, ::Taksi::Values::Dynamic) end |
#dynamic? ⇒ Boolean
72 73 74 75 76 |
# File 'lib/taksi/components/field.rb', line 72 def dynamic? return @nested_fields.any?(&:dynamic?) if @value.nil? @value.dynamic? end |
#fetch_from(data) ⇒ Object
Fetches the data for in ‘data` for the current field
31 32 33 34 35 36 37 38 39 |
# File 'lib/taksi/components/field.rb', line 31 def fetch_from(data) return value.as_json if value&.static? return data.fetch(name) if parent.nil? || parent.root? parent.fetch_from(data).fetch(name) rescue ::KeyError raise ::KeyError, "Couldn't fetch #{key.inspect} from data: #{data.inspect}" end |
#field(name, *args, &block) ⇒ Object
78 79 80 81 82 |
# File 'lib/taksi/components/field.rb', line 78 def field(name, *args, &block) self.class.new(skeleton, name, *args, parent: self, &block).tap do |new_field| @nested_fields << new_field end end |
#fields ⇒ Object
Builds up a interator over all fields included nested ones
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/taksi/components/field.rb', line 52 def fields Enumerator.new do |yielder| @nested_fields.each do |field| if field.nested? field.fields.each(&yielder) else yielder << field end end end end |
#key ⇒ Object
23 24 25 26 27 |
# File 'lib/taksi/components/field.rb', line 23 def key return name if parent.nil? || parent.root? "#{parent.key}.#{name}" end |
#nested(name, &block) ⇒ Object
84 85 86 |
# File 'lib/taksi/components/field.rb', line 84 def nested(name, &block) field(name, &block) end |
#nested? ⇒ Boolean
64 65 66 |
# File 'lib/taksi/components/field.rb', line 64 def nested? @value.nil? end |
#root? ⇒ Boolean
68 69 70 |
# File 'lib/taksi/components/field.rb', line 68 def root? @parent.nil? end |