Module: LucidPropDeclaration::Mixin
- Defined in:
- lib/lucid_prop_declaration/mixin.rb
Class Method Summary collapse
Instance Method Summary collapse
- #declared_props ⇒ Object
- #prop(prop_name, validate_hash = { required: true }) ⇒ Object
- #validate ⇒ Object
- #validate_function ⇒ Object
- #validate_prop(prop, value) ⇒ Object
- #validate_prop_function(prop) ⇒ Object
- #validate_props(props) ⇒ Object
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 29 |
# 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_react_component) { let react_prop_name = Opal.React.lower_camelize(prop_name); #{value = validate_hash[:default]} if (!base.lucid_react_component.defaultProps) { base.lucid_react_component.defaultProps = {}; } base.lucid_react_component.defaultProps[react_prop_name] = value; if (!base.lucid_react_component.propTypes) { base.lucid_react_component.propTypes = {}; } base.lucid_react_component.propTypes[react_prop_name] = base.lucid_react_component.prototype.validateProp; } else if (base.react_component) { let react_prop_name = Opal.React.lower_camelize(prop_name); #{value = validate_hash[:default]} if (!base.react_component.defaultProps) { base.react_component.defaultProps = {}; } base.react_component.defaultProps[react_prop_name] = value; if (!base.react_component.propTypes) { base.react_component.propTypes = {}; } base.react_component.propTypes[react_prop_name] = base.react_component.prototype.validateProp; } } end declared_props[prop_name.to_sym] = validate_hash end end |
Instance Method Details
#declared_props ⇒ Object
62 63 64 |
# File 'lib/lucid_prop_declaration/mixin.rb', line 62 def declared_props @declared_props ||= {} end |
#prop(prop_name, validate_hash = { required: true }) ⇒ Object
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 6 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_react_component) { let react_prop_name = Opal.React.lower_camelize(prop_name); #{value = validate_hash[:default]} if (!base.lucid_react_component.defaultProps) { base.lucid_react_component.defaultProps = {}; } base.lucid_react_component.defaultProps[react_prop_name] = value; if (!base.lucid_react_component.propTypes) { base.lucid_react_component.propTypes = {}; } base.lucid_react_component.propTypes[react_prop_name] = base.lucid_react_component.prototype.validateProp; } else if (base.react_component) { let react_prop_name = Opal.React.lower_camelize(prop_name); #{value = validate_hash[:default]} if (!base.react_component.defaultProps) { base.react_component.defaultProps = {}; } base.react_component.defaultProps[react_prop_name] = value; if (!base.react_component.propTypes) { base.react_component.propTypes = {}; } base.react_component.propTypes[react_prop_name] = base.react_component.prototype.validateProp; } } end declared_props[prop_name.to_sym] = validate_hash end |
#validate ⇒ Object
66 67 68 |
# File 'lib/lucid_prop_declaration/mixin.rb', line 66 def validate Isomorfeus::Props::ValidateHashProxy.new end |
#validate_function ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/lucid_prop_declaration/mixin.rb', line 31 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
70 71 72 73 74 75 |
# File 'lib/lucid_prop_declaration/mixin.rb', line 70 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
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/lucid_prop_declaration/mixin.rb', line 43 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
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/lucid_prop_declaration/mixin.rb', line 77 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) raise "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 |