Module: LucidPropDeclaration::Mixin

Defined in:
lib/lucid_prop_declaration/mixin.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



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
# File 'lib/lucid_prop_declaration/mixin.rb', line 4

def self.extended(base)
  def prop(prop_name, validate_hash = { required: true })
    validate_hash = validate_hash.to_h if validate_hash.class == Isomorfeus::Props::ValidateHashProxy
    if validate_hash.key?(:default)
      %x{
        if (base.lucid_preact_component) {
          let preact_prop_name = Opal.Preact.lower_camelize(prop_name);
          #{value = validate_hash[:default]}
          if (!base.lucid_preact_component.defaultProps) { base.lucid_preact_component.defaultProps = {}; }
          base.lucid_preact_component.defaultProps[preact_prop_name] = value;
          if (!base.lucid_preact_component.propTypes) { base.lucid_preact_component.propTypes = {}; }
          base.lucid_preact_component.propTypes[preact_prop_name] = base.lucid_preact_component.prototype.validateProp;
        } else if (base.preact_component) {
          let preact_prop_name = Opal.Preact.lower_camelize(prop_name);
          #{value = validate_hash[:default]}
          if (!base.preact_component.defaultProps) { base.preact_component.defaultProps = {}; }
          base.preact_component.defaultProps[preact_prop_name] = value;
          if (!base.preact_component.propTypes) { base.preact_component.propTypes = {}; }
          base.preact_component.propTypes[preact_prop_name] = base.preact_component.prototype.validateProp;
        }
      }
    end
    declared_props[prop_name.to_sym] = validate_hash
  end
end

Instance Method Details

#declared_propsObject



61
62
63
# File 'lib/lucid_prop_declaration/mixin.rb', line 61

def declared_props
  @declared_props ||= {}
end

#prop(prop_name, validate_hash = { required: true }) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lucid_prop_declaration/mixin.rb', line 5

def prop(prop_name, validate_hash = { required: true })
  validate_hash = validate_hash.to_h if validate_hash.class == Isomorfeus::Props::ValidateHashProxy
  if validate_hash.key?(:default)
    %x{
      if (base.lucid_preact_component) {
        let preact_prop_name = Opal.Preact.lower_camelize(prop_name);
        #{value = validate_hash[:default]}
        if (!base.lucid_preact_component.defaultProps) { base.lucid_preact_component.defaultProps = {}; }
        base.lucid_preact_component.defaultProps[preact_prop_name] = value;
        if (!base.lucid_preact_component.propTypes) { base.lucid_preact_component.propTypes = {}; }
        base.lucid_preact_component.propTypes[preact_prop_name] = base.lucid_preact_component.prototype.validateProp;
      } else if (base.preact_component) {
        let preact_prop_name = Opal.Preact.lower_camelize(prop_name);
        #{value = validate_hash[:default]}
        if (!base.preact_component.defaultProps) { base.preact_component.defaultProps = {}; }
        base.preact_component.defaultProps[preact_prop_name] = value;
        if (!base.preact_component.propTypes) { base.preact_component.propTypes = {}; }
        base.preact_component.propTypes[preact_prop_name] = base.preact_component.prototype.validateProp;
      }
    }
  end
  declared_props[prop_name.to_sym] = validate_hash
end

#valid_prop?(prop, value) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
# File 'lib/lucid_prop_declaration/mixin.rb', line 65

def valid_prop?(prop, value)
  validate_prop(prop, value)
rescue
  false
end

#valid_props?(props) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
# File 'lib/lucid_prop_declaration/mixin.rb', line 71

def valid_props?(props)
  validate_props(props)
rescue
  false
end

#validateObject



77
78
79
# File 'lib/lucid_prop_declaration/mixin.rb', line 77

def validate
  Isomorfeus::Props::ValidateHashProxy.new
end

#validate_functionObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lucid_prop_declaration/mixin.rb', line 30

def validate_function
  %x{
    if (typeof self.validate_function === 'undefined') {
      self.validate_function = function(props_object) {
        try { self.$validate_props(Opal.Hash.$new(props_object)) }
        catch (e) { return e.message; }
      }
    }
    return self.validate_function;
  }
end

#validate_prop(prop, value) ⇒ Object



81
82
83
84
85
86
# File 'lib/lucid_prop_declaration/mixin.rb', line 81

def validate_prop(prop, value)
  return false unless declared_props.key?(prop)
  validator = Isomorfeus::Props::Validator.new(self, prop, value, declared_props[prop])
  validator.validate!
  true
end

#validate_prop_function(prop) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lucid_prop_declaration/mixin.rb', line 42

def validate_prop_function(prop)
  function_name = "validate_#{prop}_function"
  %x{
    if (typeof self[function_name] === 'undefined') {
      self[function_name] = function(value) {
        try { self.$validate_prop(prop, value); }
        catch (e) { return e.message; }
      }
    }
    return self[function_name];
  }
end

#validate_props(props) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/lucid_prop_declaration/mixin.rb', line 88

def validate_props(props)
  props = {} unless props
  declared_props.each_key do |prop|
    if declared_props[prop].key?(:required) && declared_props[prop][:required] && !props.key?(prop)
      Isomorfeus.raise_error(message: "Required prop '#{prop}' not given!")
    end
  end
  result = true
  props.each do |p, v|
    r = validate_prop(p, v)
    result = false unless r
  end
  result
end

#validated_prop(prop, value) ⇒ Object



103
104
105
106
107
# File 'lib/lucid_prop_declaration/mixin.rb', line 103

def validated_prop(prop, value)
  Isomorfeus.raise_error(message: "No such prop '#{prop}' declared!") unless declared_props.key?(prop)
  validator = Isomorfeus::Props::Validator.new(self, prop, value, declared_props[prop])
  validator.validated_value
end

#validated_props(props) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/lucid_prop_declaration/mixin.rb', line 109

def validated_props(props)
  props = {} unless props

  declared_props.each_key do |prop|
    if declared_props[prop].key?(:required) && declared_props[prop][:required] && !props.key?(prop)
      Isomorfeus.raise_error(message: "Required prop '#{prop}' not given!")
    end
    props[prop] = nil unless props.key?(prop) # let validator handle value
  end

  result = {}
  props.each do |p, v|
    result[p] = validated_prop(p, v)
  end
  result
end