Class: ParamsReady::Helpers::Options
- Inherits:
-
Object
- Object
- ParamsReady::Helpers::Options
- Defined in:
- lib/params_ready/helpers/options.rb
Instance Attribute Summary collapse
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#relations ⇒ Object
readonly
Returns the value of attribute relations.
Class Method Summary collapse
Instance Method Summary collapse
- #create_state_for(key) ⇒ Object
- #definitions_for(name, rules) ⇒ Object
- #dup ⇒ Object
-
#initialize ⇒ Options
constructor
A new instance of Options.
- #merge_parameter_rule(rule) ⇒ Object
- #merge_relation_rule(rule) ⇒ Object
- #parameter_definitions_for(name) ⇒ Object
- #parameter_rules ⇒ Object
- #relation_definitions_for(name) ⇒ Object
- #relation_rules ⇒ Object
- #reset_memo!(*args) ⇒ Object
- #use_parameter(param, rule_args = :all) ⇒ Object
- #use_relation(relation, rule_args = :all) ⇒ Object
Constructor Details
#initialize ⇒ Options
Returns a new instance of Options.
9 10 11 12 13 |
# File 'lib/params_ready/helpers/options.rb', line 9 def initialize @parameter_rules = Hash.new @relation_rules = Hash.new @memo = { definitions: {} } end |
Instance Attribute Details
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
7 8 9 |
# File 'lib/params_ready/helpers/options.rb', line 7 def parameters @parameters end |
#relations ⇒ Object (readonly)
Returns the value of attribute relations.
7 8 9 |
# File 'lib/params_ready/helpers/options.rb', line 7 def relations @relations end |
Class Method Details
.merge_rule(rule, rules) ⇒ Object
99 100 101 102 103 104 |
# File 'lib/params_ready/helpers/options.rb', line 99 def self.merge_rule(rule, rules) existing = rules[rule.name] merged = rule.merge(existing) rules[rule.name] = merged rules end |
Instance Method Details
#create_state_for(key) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/params_ready/helpers/options.rb', line 86 def create_state_for(key) @memo[:definitions][key] ||= begin builder = Parameter::StateBuilder.instance parameter_definitions_for(key).each do |definition| builder.add definition end relation_definitions_for(key).each do |definition| builder.relation definition end builder.build end end |
#definitions_for(name, rules) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/params_ready/helpers/options.rb', line 40 def definitions_for(name, rules) rules.each_with_object([]) do |(_, rule), result| next unless rule.valid_for?(name) result << rule.parameter_definition end end |
#dup ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/params_ready/helpers/options.rb', line 21 def dup duplicate = Options.new @parameter_rules.each do |_, rule| duplicate.merge_parameter_rule(rule) end @relation_rules.each do |_, rule| duplicate.merge_relation_rule(rule) end duplicate end |
#merge_parameter_rule(rule) ⇒ Object
53 54 55 56 |
# File 'lib/params_ready/helpers/options.rb', line 53 def merge_parameter_rule(rule) reset_memo!(:definitions) @parameter_rules = self.class.merge_rule(rule, @parameter_rules) end |
#merge_relation_rule(rule) ⇒ Object
63 64 65 66 |
# File 'lib/params_ready/helpers/options.rb', line 63 def merge_relation_rule(rule) reset_memo!(:definitions) @relation_rules = self.class.merge_rule(rule, @relation_rules) end |
#parameter_definitions_for(name) ⇒ Object
36 37 38 |
# File 'lib/params_ready/helpers/options.rb', line 36 def parameter_definitions_for(name) definitions_for(name, parameter_rules) end |
#parameter_rules ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/params_ready/helpers/options.rb', line 68 def parameter_rules if block_given? @parameter_rules.each_value do |rule| yield rule end end @parameter_rules end |
#relation_definitions_for(name) ⇒ Object
32 33 34 |
# File 'lib/params_ready/helpers/options.rb', line 32 def relation_definitions_for(name) definitions_for(name, relation_rules) end |
#relation_rules ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/params_ready/helpers/options.rb', line 77 def relation_rules if block_given? @relation_rules.each_value do |rule| yield rule end end @relation_rules end |
#reset_memo!(*args) ⇒ Object
15 16 17 18 19 |
# File 'lib/params_ready/helpers/options.rb', line 15 def reset_memo!(*args) args.each do |key| @memo[key].clear end end |