Class: SmashTheState::Operation::State
- Inherits:
-
Object
- Object
- SmashTheState::Operation::State
- Includes:
- ActiveModel::Model, ActiveModelAttributes
- Defined in:
- lib/smash_the_state/operation/state.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Invalid
Class Attribute Summary collapse
-
.representer ⇒ Object
Returns the value of attribute representer.
Instance Attribute Summary collapse
-
#current_user ⇒ Object
Returns the value of attribute current_user.
Class Method Summary collapse
- .build(params = nil, &block) ⇒ Object
-
.eval_custom_validator_block(state, original_state = nil) {|state, original_state| ... } ⇒ Object
for non-ActiveModel states we will just evaluate the block as a validator.
-
.eval_validation_directives_block(state, &block) ⇒ Object
for ActiveModel states we will treat the block as a collection of ActiveModel validator directives.
- .extend_validation_directives_block(state, &block) ⇒ Object
- .model_name ⇒ Object
-
.schema(key, options = {}, &block) ⇒ Object
defines a nested schema inside of a state.
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(attributes = {}) ⇒ State
constructor
A new instance of State.
Constructor Details
#initialize(attributes = {}) ⇒ State
Returns a new instance of State.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/smash_the_state/operation/state.rb', line 82 def initialize(attributes = {}) @current_user = attributes.delete(:current_user) || attributes.delete("current_user") indifferent_whitelisted_attributes = self. class. attributes_registry. with_indifferent_access # ActiveModel will raise an ActiveRecord::UnknownAttributeError if any unexpected # attributes are passed in. since operations are meant to replace strong # parameters and enforce against arbitrary mass assignment, we should filter the # params to inclide only the whitelisted attributes. # TODO: what about nested attributes? whitelisted_attributes = attributes.select do |attribute| indifferent_whitelisted_attributes.key? attribute end super(whitelisted_attributes) end |
Class Attribute Details
.representer ⇒ Object
Returns the value of attribute representer.
18 19 20 |
# File 'lib/smash_the_state/operation/state.rb', line 18 def representer @representer end |
Instance Attribute Details
#current_user ⇒ Object
Returns the value of attribute current_user.
80 81 82 |
# File 'lib/smash_the_state/operation/state.rb', line 80 def current_user @current_user end |
Class Method Details
.build(params = nil, &block) ⇒ Object
20 21 22 23 24 |
# File 'lib/smash_the_state/operation/state.rb', line 20 def build(params = nil, &block) Class.new(self).tap do |k| k.class_exec(params, &block) end end |
.eval_custom_validator_block(state, original_state = nil) {|state, original_state| ... } ⇒ Object
for non-ActiveModel states we will just evaluate the block as a validator
58 59 60 61 62 |
# File 'lib/smash_the_state/operation/state.rb', line 58 def eval_custom_validator_block(state, original_state = nil) yield(state, original_state) invalid!(state) if state.errors.present? state end |
.eval_validation_directives_block(state, &block) ⇒ Object
for ActiveModel states we will treat the block as a collection of ActiveModel validator directives
41 42 43 44 45 46 47 48 49 |
# File 'lib/smash_the_state/operation/state.rb', line 41 def eval_validation_directives_block(state, &block) state.tap do |s| # each validate block should be a "fresh start" and not interfere with the # previous blocks s.class.clear_validators! s.class_eval(&block) s.validate || invalid!(s) end end |
.extend_validation_directives_block(state, &block) ⇒ Object
51 52 53 54 55 |
# File 'lib/smash_the_state/operation/state.rb', line 51 def extend_validation_directives_block(state, &block) state.tap do |s| s.class_eval(&block) end end |
.model_name ⇒ Object
64 65 66 |
# File 'lib/smash_the_state/operation/state.rb', line 64 def model_name ActiveModel::Name.new(self, nil, "State") end |
.schema(key, options = {}, &block) ⇒ Object
defines a nested schema inside of a state. can be nested arbitrarily deep. schemas may be described inline via a block or can be a reference to a definition
29 30 31 32 33 34 35 36 37 |
# File 'lib/smash_the_state/operation/state.rb', line 29 def schema(key, = {}, &block) attribute key, :state_for_smashing, .merge( # allow for schemas to be provided inline *or* as a reference to a # type definition schema: () || block ) end |
Instance Method Details
#as_json ⇒ Object
103 104 105 106 107 |
# File 'lib/smash_the_state/operation/state.rb', line 103 def as_json Hash[self.class.attributes_registry.keys.map do |key| [key, send(key).as_json] end] end |