Class: Taksi::Components::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/taksi/components/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/taksi/components/field.rb', line 6

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/taksi/components/field.rb', line 6

def parent
  @parent
end

#skeletonObject (readonly)

Returns the value of attribute skeleton.



6
7
8
# File 'lib/taksi/components/field.rb', line 6

def skeleton
  @skeleton
end

#valueObject (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_jsonObject

Turns the field into his json representation The returned hash is compatible with the skeleton json specification

Returns:

  • Hash



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

Returns:

  • (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

Returns:

  • any



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

#fieldsObject

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

#keyObject



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

Returns:

  • (Boolean)


64
65
66
# File 'lib/taksi/components/field.rb', line 64

def nested?
  @value.nil?
end

#root?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/taksi/components/field.rb', line 68

def root?
  @parent.nil?
end

#static(name, value) ⇒ Object



88
89
90
# File 'lib/taksi/components/field.rb', line 88

def static(name, value)
  field(name, ::Taksi::Values::Static, value)
end