-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
dataset.rb
44 lines (32 loc) · 1.12 KB
/
dataset.rb
1
2
3
4
5
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
33
34
35
36
37
38
39
40
41
42
43
44
# frozen_string_literal: true
require "stimulus_reflex/utils/attribute_builder"
class StimulusReflex::Dataset < OpenStruct
include StimulusReflex::AttributeBuilder
attr_accessor :attrs, :data_attrs
def initialize(data = {})
datasets = data["dataset"] || {}
regular_dataset = datasets["dataset"] || {}
@attrs = build_data_attrs(regular_dataset, datasets["datasetAll"] || {})
@data_attrs = @attrs.transform_keys { |key| key.delete_prefix "data-" }
super build_underscored(@data_attrs)
end
def signed
@signed ||= ->(accessor) { GlobalID::Locator.locate_signed(self[accessor]) }
end
def unsigned
@unsigned ||= ->(accessor) { GlobalID::Locator.locate(self[accessor]) }
end
def boolean
@boolean ||= ->(accessor) { cast_boolean(self[accessor]) || self[accessor].blank? }
end
def numeric
@numeric ||= ->(accessor) { Float(self[accessor]) }
end
private
def cast_boolean(value)
((value == "") ? nil : !false_values.include?(value)) unless value.nil?
end
def false_values
@false_values ||= [false, 0, "0", "f", "F", "false", "FALSE", "off", "OFF"].to_set
end
end